root-schema: allow re-setting a new root-schema

Fixes #70
This commit is contained in:
Patrick Boettcher 2019-10-14 11:43:22 +02:00
parent e14d2896ff
commit 5617eeec4e
4 changed files with 52 additions and 3 deletions

View File

@ -153,7 +153,7 @@ public:
~json_validator();
json_validator &operator=(json_validator &&);
// insert and set thea root-schema
// insert and set the root-schema
void set_root_schema(const json &);
// validate a json-document based on the root-schema

View File

@ -171,6 +171,7 @@ public:
void set_root_schema(json schema)
{
files_.clear();
root_ = schema::make(schema, this, {}, {{"#"}});
// load all files which have not yet been loaded
@ -225,7 +226,7 @@ public:
json instance_;
std::string message_;
void error(const json::json_pointer & ptr, const json & instance, const std::string & message) override
void error(const json::json_pointer &ptr, const json &instance, const std::string &message) override
{
if (*this)
return;

View File

@ -21,8 +21,12 @@ endforeach()
add_executable(uri uri.cpp)
target_link_libraries(uri json-schema-validator)
add_test(NAME uri COMMAND uri)
add_executable(errors errors.cpp)
target_link_libraries(errors json-schema-validator)
add_test(NAME errors COMMAND errors)
add_test(NAME uri COMMAND uri)
add_executable(issue-70 issue-70.cpp)
target_link_libraries(issue-70 json-schema-validator)
add_test(NAME issue-70 COMMAND issue-70)

44
test/issue-70.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <json-schema.hpp>
using nlohmann::json;
using nlohmann::json_schema::json_validator;
static const json person_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "A person",
"properties": {
"name": {
"description": "Name",
"type": "string"
},
"age": {
"description": "Age of the person",
"type": "number",
"minimum": 2,
"maximum": 200
},
"phones": {
"type": "array",
"items": {
"type": "number"
}
}
},
"required": [
"name",
"age"
],
"additionalProperties": false,
"type": "object"
})"_json;
int main(void)
{
json_validator validator;
validator.set_root_schema(person_schema);
validator.set_root_schema(person_schema);
return 0;
}