Skip to content

Commit

Permalink
Merge pull request #4 from geometryprocessing/dzint/null_as_input
Browse files Browse the repository at this point in the history
Add the option to input null for a float or int type.
  • Loading branch information
danielepanozzo committed Jun 18, 2024
2 parents d0fcd4f + d90076b commit 11d028e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/jse/jse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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"])
Expand Down
47 changes: 46 additions & 1 deletion tests/test_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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("");
}

0 comments on commit 11d028e

Please sign in to comment.