diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3371cb9..600aed7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -39,6 +39,10 @@ add_executable(issue-25-default-values issue-25-default-values.cpp) target_link_libraries(issue-25-default-values nlohmann_json_schema_validator) add_test(NAME issue-25-default-values COMMAND issue-25-default-values) +add_executable(issue-xx-test-with-defaults issue-xx.cpp) +target_link_libraries(issue-xx-test-with-defaults nlohmann_json_schema_validator) +add_test(NAME issue-xx-test-with-defaults COMMAND issue-xx-test-with-defaults) + # Unit test for string format checks add_executable("string-format-check-test" "string-format-check-test.cpp") target_include_directories("string-format-check-test" PRIVATE "${PROJECT_SOURCE_DIR}/src/") diff --git a/test/issue-xx.cpp b/test/issue-xx.cpp new file mode 100644 index 0000000..ac9e943 --- /dev/null +++ b/test/issue-xx.cpp @@ -0,0 +1,119 @@ +#include +#include + +using nlohmann::json; +using nlohmann::json_schema::json_validator; + +static const json address_schema = R"( +{ + "type": "object", + "properties": { + "street_address": { + "type": "string" + }, + "country": { + "enum": ["United States of America", "Canada"], + "default": "Canada" + } + }, + "if": { + "properties": { "country": { "const": "United States of America" } } + }, + "then": { + "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } } + }, + "else": { + "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } } + } +})"_json; + +class expected_error_handler : public nlohmann::json_schema::basic_error_handler +{ + void error(const nlohmann::json_pointer> &pointer, const json &instance, + const std::string &message) override + { + hit_ = true; + } + + bool hit_{false}; + +public: + bool isHit() const { return hit_; } +}; + +class custom_error_handler : public nlohmann::json_schema::basic_error_handler +{ + void error(const nlohmann::json_pointer> &pointer, const json &instance, + const std::string &message) override + { + nlohmann::json_schema::basic_error_handler::error(pointer, instance, message); + std::cerr << "ERROR: '" << pointer << "' - '" << instance << "': " << message << "\n"; + hit_ = true; + } + + bool hit_{false}; + +public: + bool isHit() const { return hit_; } +}; + +int main(void) +{ + json_validator validator{}; + validator.set_root_schema(address_schema); + + { + custom_error_handler customErr{}; + const auto good_withCanada = R"({ + "street_address": "24 Sussex Drive", + "country": "Canada", + "postal_code": "K1M 1M4" + })"_json; + validator.validate(good_withCanada, customErr); + if (customErr.isHit()) { + return 1; + } + } + + { + custom_error_handler customErr{}; + const auto good_withDefaultCanada = R"({ + "street_address": "24 Sussex Drive", + "postal_code": "K1M 1M4" + })"_json; + validator.validate(good_withDefaultCanada, customErr); + if (customErr.isHit()) { + return 1; + } + } + + { + custom_error_handler customErr{}; + const auto good_Usa = R"({ + "street_address": "1600 Pennsylvania Avenue NW", + "country": "United States of America", + "postal_code": "20500" + })"_json; + validator.validate(good_Usa, customErr); + if (customErr.isHit()) { + return 1; + } + } + + { + expected_error_handler expErr{}; + const auto bad_Canada = R"({ + "street_address": "24 Sussex Drive", + "country": "Canada", + "postal_code": "10000" + })"_json; + + validator.validate(bad_Canada, expErr); + if (!expErr.isHit()) { + std::cerr << "Should have failed validating postal code" << std::endl; + return 1; + } + } + + return 0; +}