Skip to content

Commit 11d028e

Browse files
Merge pull request #4 from geometryprocessing/dzint/null_as_input
Add the option to input null for a float or int type.
2 parents d0fcd4f + d90076b commit 11d028e

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

Diff for: src/jse/jse.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ namespace jse
367367
{
368368
assert(rule.at("type") == "float");
369369

370-
if (!input.is_number())
370+
if (!input.is_number() && !input.is_null())
371371
return false;
372372

373373
if (rule.contains("min") && input < rule["min"])
@@ -383,7 +383,7 @@ namespace jse
383383
{
384384
assert(rule.at("type") == "int");
385385

386-
if (!input.is_number_integer())
386+
if (!input.is_number_integer() && !input.is_null())
387387
return false;
388388

389389
if (rule.contains("min") && input < rule["min"])

Diff for: tests/test_validator.cpp

+46-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ TEST_CASE("include_rule", "[validator]")
250250
REQUIRE(new_rules == matching);
251251
}
252252

253-
254253
TEST_CASE("file_01", "[validator]")
255254
{
256255
std::ifstream ifs1(root_path + "/input_01.json");
@@ -611,3 +610,49 @@ TEST_CASE("polymorphism", "[inject]")
611610
INFO(return_json);
612611
REQUIRE(return_json == output);
613612
}
613+
614+
TEST_CASE("null_as_nan", "[validator][inject]")
615+
{
616+
nlohmann::json rules = R"(
617+
[
618+
{
619+
"pointer": "/",
620+
"type": "object",
621+
"required": ["f1"],
622+
"optional": ["f2"]
623+
},
624+
{
625+
"pointer": "/f1",
626+
"type": "float"
627+
},
628+
{
629+
"pointer": "/f2",
630+
"type": "int",
631+
"default": null
632+
}
633+
]
634+
)"_json;
635+
636+
nlohmann::json input = R"(
637+
{
638+
"f1": null,
639+
"f2": null
640+
}
641+
)"_json;
642+
643+
JSE jse;
644+
jse.strict = true;
645+
646+
bool r = jse.verify_json(input, rules);
647+
REQUIRE(r);
648+
649+
input.erase("f2");
650+
r = jse.verify_json(input, rules);
651+
REQUIRE(r);
652+
653+
const json return_json = jse.inject_defaults(input, rules);
654+
CHECK(return_json["f1"].is_null());
655+
CHECK(return_json["f2"].is_null());
656+
657+
input["f2"] = std::nan("");
658+
}

0 commit comments

Comments
 (0)