validator: uniqueItems implemented for array
This commit is contained in:
parent
615b4a1fef
commit
6c344f9dcf
@ -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 **120** tests are still failing, because simply not all keywords and
|
Currently **107** of ~**308** 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)
|
||||||
|
|
||||||
|
|||||||
@ -58,7 +58,7 @@ int main(void)
|
|||||||
total += group_total;
|
total += group_total;
|
||||||
std::cout << "Group RESULT: " << test_group["description"] << " "
|
std::cout << "Group RESULT: " << test_group["description"] << " "
|
||||||
<< (group_total - group_failed) << " of " << group_total
|
<< (group_total - group_failed) << " of " << group_total
|
||||||
<< " have succeeded - " << group_failed << " failed\n";
|
<< " have succeeded - " << group_failed << " failed\n";
|
||||||
std::cout << "-------------\n";
|
std::cout << "-------------\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
|
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
// make yourself a home - welcome to nlohmann's namespace
|
// make yourself a home - welcome to nlohmann's namespace
|
||||||
namespace nlohmann
|
namespace nlohmann
|
||||||
@ -87,6 +88,7 @@ class json_validator
|
|||||||
|
|
||||||
validate_type(schema, "string", name);
|
validate_type(schema, "string", name);
|
||||||
|
|
||||||
|
// 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 (instance.get<std::string>().size() < attr.value()) {
|
||||||
@ -96,6 +98,7 @@ class json_validator
|
|||||||
throw std::out_of_range(s.str());
|
throw std::out_of_range(s.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 (instance.get<std::string>().size() > attr.value()) {
|
||||||
@ -174,10 +177,11 @@ class json_validator
|
|||||||
|
|
||||||
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, "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");
|
||||||
|
|
||||||
|
validate_type(schema, "array", name);
|
||||||
|
|
||||||
// maxItems
|
// maxItems
|
||||||
const auto &maxItems = schema.find("maxItems");
|
const auto &maxItems = schema.find("maxItems");
|
||||||
if (maxItems != schema.end())
|
if (maxItems != schema.end())
|
||||||
@ -190,7 +194,18 @@ class json_validator
|
|||||||
if (instance.size() < minItems.value())
|
if (instance.size() < minItems.value())
|
||||||
throw std::out_of_range(name + " has too many items.");
|
throw std::out_of_range(name + " has too many items.");
|
||||||
|
|
||||||
validate_type(schema, "array", name);
|
// uniqueItems
|
||||||
|
const auto &uniqueItems = schema.find("uniqueItems");
|
||||||
|
if (uniqueItems != schema.end())
|
||||||
|
if (uniqueItems.value() == true) {
|
||||||
|
std::unordered_set<json> array_to_set;
|
||||||
|
for (auto v : instance) {
|
||||||
|
array_to_set.insert(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instance.size() != array_to_set.size())
|
||||||
|
throw std::out_of_range(name + " should have only unique items.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void validate_object(json &instance, const json &schema, const std::string &name)
|
void validate_object(json &instance, const json &schema, const std::string &name)
|
||||||
@ -320,7 +335,7 @@ class json_validator
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case json::value_t::array:
|
case json::value_t::array:
|
||||||
for (const auto &prop: dep.value())
|
for (const auto &prop : dep.value())
|
||||||
if (instance.find(prop) == instance.end())
|
if (instance.find(prop) == instance.end())
|
||||||
throw std::invalid_argument("failed dependency for " + sub_name + ". Need property " + prop.get<std::string>());
|
throw std::invalid_argument("failed dependency for " + sub_name + ". Need property " + prop.get<std::string>());
|
||||||
break;
|
break;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user