fix default is empty object (#123)
* fix default is empty object * add counter check with no default or null
This commit is contained in:
parent
ef05be7efa
commit
32af07ce19
@ -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<oneOf>::is_validate_complete(const json &instance, cons
|
||||
|
||||
class type_schema : public schema
|
||||
{
|
||||
json defaultValue_{};
|
||||
json defaultValue_ = EmptyDefault;
|
||||
std::vector<std::shared_ptr<schema>> type_;
|
||||
std::pair<bool, json> enum_, const_;
|
||||
std::vector<std::shared_ptr<schema>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,14 +19,34 @@ 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"
|
||||
}
|
||||
}
|
||||
},
|
||||
"work address": {
|
||||
"type": "object",
|
||||
"default": null,
|
||||
"properties": {
|
||||
"street": {
|
||||
"type": "string",
|
||||
"default": "Abbey Road"
|
||||
}
|
||||
}
|
||||
},
|
||||
"other address": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"street": {
|
||||
"type": "string",
|
||||
"default": "Abbey Road"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
@ -40,61 +60,115 @@ 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<std::string>() != "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<std::string>();
|
||||
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<std::string>() != "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 empty object
|
||||
// but not work address which is null or other address which has no default
|
||||
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<std::string>() != "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<std::string>();
|
||||
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<std::string>() != "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<std::string>();
|
||||
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<std::string>() != "Abbey Road") {
|
||||
std::cerr << "Patch with defaults contains wrong value" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user