diff --git a/src/jse/jse.cpp b/src/jse/jse.cpp index 5807490..78c86a2 100644 --- a/src/jse/jse.cpp +++ b/src/jse/jse.cpp @@ -367,7 +367,7 @@ namespace jse { assert(rule.at("type") == "float"); - if (!input.is_number()) + if (!input.is_number() && !input.is_null()) return false; if (rule.contains("min") && input < rule["min"]) @@ -383,7 +383,7 @@ namespace jse { assert(rule.at("type") == "int"); - if (!input.is_number_integer()) + if (!input.is_number_integer() && !input.is_null()) return false; if (rule.contains("min") && input < rule["min"]) diff --git a/tests/test_validator.cpp b/tests/test_validator.cpp index 04193d7..beb7ed6 100644 --- a/tests/test_validator.cpp +++ b/tests/test_validator.cpp @@ -250,7 +250,6 @@ TEST_CASE("include_rule", "[validator]") REQUIRE(new_rules == matching); } - TEST_CASE("file_01", "[validator]") { std::ifstream ifs1(root_path + "/input_01.json"); @@ -611,3 +610,49 @@ TEST_CASE("polymorphism", "[inject]") INFO(return_json); REQUIRE(return_json == output); } + +TEST_CASE("null_as_nan", "[validator][inject]") +{ + nlohmann::json rules = R"( + [ + { + "pointer": "/", + "type": "object", + "required": ["f1"], + "optional": ["f2"] + }, + { + "pointer": "/f1", + "type": "float" + }, + { + "pointer": "/f2", + "type": "int", + "default": null + } + ] + )"_json; + + nlohmann::json input = R"( + { + "f1": null, + "f2": null + } + )"_json; + + JSE jse; + jse.strict = true; + + bool r = jse.verify_json(input, rules); + REQUIRE(r); + + input.erase("f2"); + r = jse.verify_json(input, rules); + REQUIRE(r); + + const json return_json = jse.inject_defaults(input, rules); + CHECK(return_json["f1"].is_null()); + CHECK(return_json["f2"].is_null()); + + input["f2"] = std::nan(""); +} \ No newline at end of file