diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 47b16f5..30cf007 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -33,7 +33,7 @@ using namespace nlohmann::json_schema; namespace { -static const json EmptyDefault{}; +static const json EmptyDefault = nullptr; class schema { @@ -402,7 +402,7 @@ bool logical_combination::is_validate_complete(const json &instance, cons class type_schema : public schema { - json defaultValue_{}; + json defaultValue_ = EmptyDefault; std::vector> type_; std::pair enum_, const_; std::vector> logic_; @@ -882,7 +882,7 @@ class object : public schema const auto finding = instance.find(prop.first); if (instance.end() == finding) { // if the prop is not in the instance const auto &defaultValue = prop.second->defaultValue(ptr, instance, e); - if (!defaultValue.empty()) { // if default value is available + if (!defaultValue.is_null()) { // if default value is available patch.add((ptr / prop.first), defaultValue); } } diff --git a/test/issue-25-default-values.cpp b/test/issue-25-default-values.cpp index f775281..8c11453 100644 --- a/test/issue-25-default-values.cpp +++ b/test/issue-25-default-values.cpp @@ -19,14 +19,15 @@ static const json person_schema = R"( "minimum": 2, "maximum": 200 }, - "address":{ - "type": "object", - "properties":{ - "street":{ - "type": "string", - "default": "Abbey Road" + "address": { + "type": "object", + "default": {}, + "properties": { + "street": { + "type": "string", + "default": "Abbey Road" + } } - } } }, "required": [ @@ -40,61 +41,114 @@ static const json person_schema = R"( int main(void) { json_validator validator{}; - - // add address which is optional that should generate a diff containing a default street - json person_missing_address = R"({ - "name": "Hans", - "age": 69, - "address": {} -})"_json; - validator.set_root_schema(person_schema); - const auto default_patch = validator.validate(person_missing_address); + { + // add address which is optional that should generate a diff containing a default street + json person_emtpy_address = R"({ + "name": "Hans", + "age": 69, + "address": {} + })"_json; - if (!default_patch.is_array()) { - std::cerr << "Patch with defaults is expected to be an array" << std::endl; - return 1; + const auto default_patch = validator.validate(person_emtpy_address); + + if (!default_patch.is_array()) { + std::cerr << "Patch with defaults is expected to be an array" << std::endl; + return 1; + } + + if (default_patch.size() != 1) { + std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl; + return 1; + } + + const auto &single_op = default_patch[0]; + + if (!single_op.contains("op")) { + std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl; + return 1; + } + + if (single_op["op"].get() != "add") { + std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl; + return 1; + } + + if (!single_op.contains("path")) { + std::cerr << "Patch with defaults is expected to contain a path" << std::endl; + return 1; + } + + const auto &readPath = single_op["path"].get(); + if (readPath != "/address/street") { + std::cerr << "Patch with defaults contains wrong path. It is " << readPath << " and should be " + << "/address/street" << std::endl; + return 1; + } + + if (!single_op.contains("value")) { + std::cerr << "Patch with defaults is expected to contain a value" << std::endl; + return 1; + } + + if (single_op["value"].get() != "Abbey Road") { + std::cerr << "Patch with defaults contains wrong value" << std::endl; + return 1; + } } + { + // add address which is optional that should generate a diff containing a default street + json person_missing_address = R"({ + "name": "Hans", + "age": 69 + })"_json; - if (default_patch.size() != 1) { - std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl; - return 1; + const auto default_patch = validator.validate(person_missing_address); + + if (!default_patch.is_array()) { + std::cerr << "Patch with defaults is expected to be an array" << std::endl; + return 1; + } + + if (default_patch.size() != 1) { + std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl; + return 1; + } + + const auto &single_op = default_patch[0]; + + if (!single_op.contains("op")) { + std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl; + return 1; + } + + if (single_op["op"].get() != "add") { + std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl; + return 1; + } + + if (!single_op.contains("path")) { + std::cerr << "Patch with defaults is expected to contain a path" << std::endl; + return 1; + } + + const auto &readPath = single_op["path"].get(); + if (readPath != "/address") { + std::cerr << "Patch with defaults contains wrong path. It is " << readPath << " and should be " + << "/address" << std::endl; + return 1; + } + + if (!single_op.contains("value")) { + std::cerr << "Patch with defaults is expected to contain a value" << std::endl; + return 1; + } + + if ( !single_op["value"].is_object() || !single_op["value"].empty()) { + std::cerr << "Patch with defaults contains wrong value" << std::endl; + return 1; + } } - - const auto &single_op = default_patch[0]; - - if (!single_op.contains("op")) { - std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl; - return 1; - } - - if (single_op["op"].get() != "add") { - std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl; - return 1; - } - - if (!single_op.contains("path")) { - std::cerr << "Patch with defaults is expected to contain a path" << std::endl; - return 1; - } - - const auto &readPath = single_op["path"].get(); - if (readPath != "/address/street") { - std::cerr << "Patch with defaults contains wrong path. It is " << readPath << " and should be " - << "/address/street" << std::endl; - return 1; - } - - if (!single_op.contains("value")) { - std::cerr << "Patch with defaults is expected to contain a value" << std::endl; - return 1; - } - - if (single_op["value"].get() != "Abbey Road") { - std::cerr << "Patch with defaults contains wrong value" << std::endl; - return 1; - } - return 0; }