diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 8fc6f6f..9c5fc26 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -720,6 +720,10 @@ public: sch.erase(attr); } + if (std::get<0>(content_) == true && root_->content_check() == nullptr) { + throw std::invalid_argument{"schema contains contentEncoding/contentMediaType but content checker was not set"}; + } + #ifndef NO_STD_REGEX attr = sch.find("pattern"); if (attr != sch.end()) { diff --git a/test/binary-validation.cpp b/test/binary-validation.cpp index fda5afa..01392cf 100644 --- a/test/binary-validation.cpp +++ b/test/binary-validation.cpp @@ -14,6 +14,19 @@ static int error_count = 0; } \ } while (0) +#define EXPECT_THROW(foo) \ + { \ + bool ok = false; \ + try { \ + foo; \ + } catch (std::exception &) { \ + ok = true; \ + } \ + if (ok == false) { \ + error_count++; \ + } \ + } + using json = nlohmann::json; using validator = nlohmann::json_schema::json_validator; @@ -153,29 +166,10 @@ int main() EXPECT_EQ(err.failed_pointers[0], "/something"); err.reset(); - // check without content-callback everything fails + // check that without content callback you get exception with schema with contentEncoding or contentMeditType validator val_no_content; - ///////////////////////////////////// - val_no_content.set_root_schema(bson_schema); - - // all right - val_no_content.validate({{"standard_string", "some string"}, {"binary_data", binary}}, err); - EXPECT_EQ(err.failed_pointers.size(), 1); - err.reset(); - - // invalid binary data - val_no_content.validate({{"binary_data", "string, but expect binary data"}}, err); - EXPECT_EQ(err.failed_pointers.size(), 1); - EXPECT_EQ(err.failed_pointers[0].to_string(), "/binary_data"); - err.reset(); - - // also check that simple string not accept binary data - val_no_content.validate({{"standard_string", binary}, {"binary_data", binary}}, err); - EXPECT_EQ(err.failed_pointers.size(), 2); - EXPECT_EQ(err.failed_pointers[0].to_string(), "/binary_data"); - EXPECT_EQ(err.failed_pointers[1].to_string(), "/standard_string"); - err.reset(); + EXPECT_THROW(val_no_content.set_root_schema(bson_schema)); return error_count; }