validator: check utf-8 string-length
This commit is contained in:
parent
ce68fba2d3
commit
0e5a5d9b56
10
README.md
10
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
|
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).
|
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)*
|
**12** optional tests of **305** total (required + optional) tests are failing:
|
||||||
- and 4 bugs
|
|
||||||
- unicode string length (2x)
|
|
||||||
- big numbers are not working (2) *(optional)*
|
|
||||||
|
|
||||||
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
|
# Additional features
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
void json_validator::validate_string(json &instance, const json &schema, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_type(schema, "string", name);
|
validate_type(schema, "string", name);
|
||||||
@ -665,7 +674,7 @@ void json_validator::validate_string(json &instance, const json &schema, const s
|
|||||||
// minLength
|
// minLength
|
||||||
auto attr = schema.find("minLength");
|
auto attr = schema.find("minLength");
|
||||||
if (attr != schema.end())
|
if (attr != schema.end())
|
||||||
if (instance.get<std::string>().size() < attr.value()) {
|
if (utf8_length( instance ) < attr.value()) {
|
||||||
std::ostringstream s;
|
std::ostringstream s;
|
||||||
s << "'" << name << "' of value '" << instance << "' is too short as per minLength ("
|
s << "'" << name << "' of value '" << instance << "' is too short as per minLength ("
|
||||||
<< attr.value() << ")";
|
<< attr.value() << ")";
|
||||||
@ -675,7 +684,7 @@ void json_validator::validate_string(json &instance, const json &schema, const s
|
|||||||
// maxLength
|
// maxLength
|
||||||
attr = schema.find("maxLength");
|
attr = schema.find("maxLength");
|
||||||
if (attr != schema.end())
|
if (attr != schema.end())
|
||||||
if (instance.get<std::string>().size() > attr.value()) {
|
if (utf8_length(instance) > attr.value()) {
|
||||||
std::ostringstream s;
|
std::ostringstream s;
|
||||||
s << "'" << name << "' of value '" << instance << "' is too long as per maxLength ("
|
s << "'" << name << "' of value '" << instance << "' is too long as per maxLength ("
|
||||||
<< attr.value() << ")";
|
<< attr.value() << ")";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user