Skip to content

Commit

Permalink
Merge 9bb81e2 into 13dfc96
Browse files Browse the repository at this point in the history
  • Loading branch information
smhdfdl committed Feb 12, 2021
2 parents 13dfc96 + 9bb81e2 commit 0cb4571
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
68 changes: 36 additions & 32 deletions include/rapidjson/schema.h
Expand Up @@ -733,6 +733,7 @@ class Schema {
}

RAPIDJSON_FORCEINLINE bool EndValue(Context& context) const {
// Only check pattern properties if we have validators
if (context.patternPropertiesValidatorCount > 0) {
bool otherValid = false;
SizeType count = context.patternPropertiesValidatorCount;
Expand Down Expand Up @@ -775,41 +776,44 @@ class Schema {
foundEnum:;
}

if (allOf_.schemas)
for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++)
if (!context.validators[i]->IsValid()) {
context.error_handler.NotAllOf(&context.validators[allOf_.begin], allOf_.count);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAllOf);
}

if (anyOf_.schemas) {
for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++)
if (context.validators[i]->IsValid())
goto foundAny;
context.error_handler.NoneOf(&context.validators[anyOf_.begin], anyOf_.count);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAnyOf);
foundAny:;
}

if (oneOf_.schemas) {
bool oneValid = false;
for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++)
if (context.validators[i]->IsValid()) {
if (oneValid) {
context.error_handler.NotOneOf(&context.validators[oneOf_.begin], oneOf_.count, true);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorOneOfMatch);
} else
oneValid = true;
// Only check allOf etc if we have validators
if (context.validatorCount > 0) {
if (allOf_.schemas)
for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++)
if (!context.validators[i]->IsValid()) {
context.error_handler.NotAllOf(&context.validators[allOf_.begin], allOf_.count);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAllOf);
}

if (anyOf_.schemas) {
for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++)
if (context.validators[i]->IsValid())
goto foundAny;
context.error_handler.NoneOf(&context.validators[anyOf_.begin], anyOf_.count);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorAnyOf);
foundAny:;
}

if (oneOf_.schemas) {
bool oneValid = false;
for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++)
if (context.validators[i]->IsValid()) {
if (oneValid) {
context.error_handler.NotOneOf(&context.validators[oneOf_.begin], oneOf_.count, true);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorOneOfMatch);
} else
oneValid = true;
}
if (!oneValid) {
context.error_handler.NotOneOf(&context.validators[oneOf_.begin], oneOf_.count, false);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorOneOf);
}
if (!oneValid) {
context.error_handler.NotOneOf(&context.validators[oneOf_.begin], oneOf_.count, false);
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorOneOf);
}
}

if (not_ && context.validators[notValidatorIndex_]->IsValid()) {
context.error_handler.Disallowed();
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorNot);
if (not_ && context.validators[notValidatorIndex_]->IsValid()) {
context.error_handler.Disallowed();
RAPIDJSON_INVALID_KEYWORD_RETURN(kValidateErrorNot);
}
}

return true;
Expand Down
28 changes: 28 additions & 0 deletions test/unittest/schematest.cpp
Expand Up @@ -2550,6 +2550,34 @@ TEST(SchemaValidator, ContinueOnErrors_RogueString) {
CrtAllocator::Free(schema);
}

// Test that when kValidateContinueOnErrorFlag is set, an incorrect simple type with a sub-schema is handled correctly.
// This tests that we don't blow up when there is a type mismatch but there is a sub-schema present
TEST(SchemaValidator, ContinueOnErrors_Issue2) {
Document sd;
sd.Parse("{\"type\":\"string\", \"anyOf\":[{\"maxLength\":2}]}");
ASSERT_FALSE(sd.HasParseError());
SchemaDocument s(sd);
VALIDATE(s, "\"AB\"", true);
INVALIDATE_(s, "\"ABC\"", "#", "errors", "#",
"{ \"anyOf\": {"
" \"errors\": [{"
" \"maxLength\": {"
" \"errorCode\": 6, \"instanceRef\": \"#\", \"schemaRef\": \"#/anyOf/0\", \"expected\": 2, \"actual\": \"ABC\""
" }"
" }],"
" \"errorCode\": 24, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
" }"
"}",
kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
// Invalid type
INVALIDATE_(s, "333", "#", "errors", "#",
"{ \"type\": {"
" \"errorCode\": 20, \"instanceRef\": \"#\", \"schemaRef\": \"#\", \"expected\": [\"string\"], \"actual\": \"integer\""
" }"
"}",
kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
}

TEST(SchemaValidator, Schema_UnknownError) {
ASSERT_TRUE(SchemaValidator::SchemaType::GetValidateErrorKeyword(kValidateErrors).GetString() == std::string("null"));
}
Expand Down

0 comments on commit 0cb4571

Please sign in to comment.