validator: check utf-8 string-length

This commit is contained in:
Patrick Boettcher 2016-12-29 08:50:06 +01:00
parent ce68fba2d3
commit 0e5a5d9b56
2 changed files with 15 additions and 8 deletions

View File

@ -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

View File

@ -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<std::string>().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<std::string>().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() << ")";