validator: implement minItems and maxItems for arrays

This commit is contained in:
Patrick Boettcher 2016-12-23 14:41:18 +01:00
parent 7daee089ca
commit 615b4a1fef
2 changed files with 14 additions and 4 deletions

View File

@ -84,7 +84,7 @@ int main(void)
There is an application which can be used for testing the validator with the There is an application which can be used for testing the validator with the
[JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite). [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
Currently **145** tests are still failing, because simply not all keywords and Currently **120** tests are still failing, because simply not all keywords and
their functionalities have been implemented. Some of the missing feature will their functionalities have been implemented. Some of the missing feature will
require a rework. Some will only work with external libraries. (remote references) require a rework. Some will only work with external libraries. (remote references)

View File

@ -172,14 +172,24 @@ class json_validator
validate_type(schema, "null", name); validate_type(schema, "null", name);
} }
void validate_array(json & /*instance*/, const json &schema, const std::string &name) void validate_array(json &instance, const json &schema, const std::string &name)
{ {
not_yet_implemented(schema, "maxItems", "array");
not_yet_implemented(schema, "minItems", "array");
not_yet_implemented(schema, "uniqueItems", "array"); not_yet_implemented(schema, "uniqueItems", "array");
not_yet_implemented(schema, "items", "array"); not_yet_implemented(schema, "items", "array");
not_yet_implemented(schema, "additionalItems", "array"); not_yet_implemented(schema, "additionalItems", "array");
// maxItems
const auto &maxItems = schema.find("maxItems");
if (maxItems != schema.end())
if (instance.size() > maxItems.value())
throw std::out_of_range(name + " has too many items.");
// minItems
const auto &minItems = schema.find("minItems");
if (minItems != schema.end())
if (instance.size() < minItems.value())
throw std::out_of_range(name + " has too many items.");
validate_type(schema, "array", name); validate_type(schema, "array", name);
} }