diff --git a/README.md b/README.md index 3ff0924..33b1440 100644 --- a/README.md +++ b/README.md @@ -121,14 +121,12 @@ There is an application which can be used for testing the validator with the If you have cloned this repository providing a path the repository-root via the cmake-variable `JSON_SCHEMA_TEST_SUITE_PATH` will enable the test-target(s). -Currently **14** of **305** tests are failing: +All required tests are **OK**. -- 10 of them are `format`-strings which are not supported. *(optional)* -- and 4 bugs - - unicode string length (2x) - - big numbers are not working (2) *(optional)* +**12** optional tests of **305** total (required + optional) tests are failing: -In other word **2** tests required tests are failing +- 10 of them are `format`-strings which are not supported. +- big numbers are not working (2) # Additional features diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 84f6e21..3944062 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -658,6 +658,15 @@ void json_validator::validate_object(json &instance, const json &schema, const s } } +static std::size_t utf8_length(const std::string &s) +{ + size_t len = 0; + for (const unsigned char &c: s) + if ((c & 0xc0) != 0x80) + len++; + return len; +} + void json_validator::validate_string(json &instance, const json &schema, const std::string &name) { validate_type(schema, "string", name); @@ -665,7 +674,7 @@ void json_validator::validate_string(json &instance, const json &schema, const s // minLength auto attr = schema.find("minLength"); if (attr != schema.end()) - if (instance.get().size() < attr.value()) { + if (utf8_length( instance ) < attr.value()) { std::ostringstream s; s << "'" << name << "' of value '" << instance << "' is too short as per minLength (" << attr.value() << ")"; @@ -675,7 +684,7 @@ void json_validator::validate_string(json &instance, const json &schema, const s // maxLength attr = schema.find("maxLength"); if (attr != schema.end()) - if (instance.get().size() > attr.value()) { + if (utf8_length(instance) > attr.value()) { std::ostringstream s; s << "'" << name << "' of value '" << instance << "' is too long as per maxLength (" << attr.value() << ")";