Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilsOzolins committed Sep 8, 2023
1 parent 1b935fc commit bbdd122
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/main/java/dev/harrel/jsonschema/Applicators.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,13 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
.map(ref -> ctx.resolveInternalRefAndValidate(ref, node))
.orElse(true);

return valid ? Result.success() : Result.failure("Object matched against If but failed matching against Then schema");
return valid ? Result.success() : Result.failure("Value matches against schema from 'if' but does not match against schema from 'then'");
} else {
boolean valid = elseRef
.map(ref -> ctx.resolveInternalRefAndValidate(ref, node))
.orElse(true);

return valid ? Result.success() : Result.failure("Object did not match against If but failed matching against Else schema");
return valid ? Result.success() : Result.failure("Value does not match against schema from 'if' and 'else'");
}
}
}
Expand Down Expand Up @@ -370,7 +370,7 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
return Result.success();
}

return Result.failure(String.format("Object didn't match against all schemas. Unmatched schema indexes %s", unmatchedIndexes));
return Result.failure(String.format("Value does not match against the schemas at indexes %s", unmatchedIndexes));
}
}

Expand All @@ -395,7 +395,7 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
.filter(pointer -> ctx.resolveInternalRefAndValidate(pointer, node))
.count() > 0;

return valid ? Result.success() : Result.failure("Expected object to match at least against one schema. None matched");
return valid ? Result.success() : Result.failure("Value does not match against any of the schemas");
}
}

Expand Down Expand Up @@ -425,7 +425,11 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
return Result.success();
}

return Result.failure(String.format("Object matched against more than one schema. Matched schema indexes %s", matchedIndexes));
if (matchedIndexes.isEmpty()) {
return Result.failure("Value does not match against any of the schemas");
}

return Result.failure(String.format("Value matches against more than one schema. Matched schema indexes %s", matchedIndexes));
}
}

Expand All @@ -447,7 +451,7 @@ public Set<String> getVocabularies() {
@Override
public Result evaluate(EvaluationContext ctx, JsonNode node) {
boolean valid = !ctx.resolveInternalRefAndValidate(schemaUri, node);
return valid ? Result.success() : Result.failure("Object matched against given schema but must not");
return valid ? Result.success() : Result.failure("Value matches against given schema but it must not");
}
}

Expand Down
61 changes: 57 additions & 4 deletions src/test/java/dev/harrel/jsonschema/ValidatorResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,59 @@

class ValidatorResultTest {

@Test
void returnsErrorMessageWhenIfThenFails() {
String schema = """
{
"if": true,
"then": false,
"else": false
}
""";

Validator.Result result = new ValidatorFactory().validate(schema, "null");

assertThat(result.isValid()).isFalse();
assertThat(result.getAnnotations()).isEmpty();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(2);
assertError(
errors.get(1),
"/if",
"https://harrel.dev/",
"",
"if",
"Value matches against schema from 'if' but does not match against schema from 'then'"
);
}

@Test
void returnsErrorMessageWhenIfElseFails() {
String schema = """
{
"if": false,
"then": false,
"else": false
}
""";

Validator.Result result = new ValidatorFactory().validate(schema, "null");

assertThat(result.isValid()).isFalse();
assertThat(result.getAnnotations()).isEmpty();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(3);
assertError(
errors.get(2),
"/if",
"https://harrel.dev/",
"",
"if",
"Value does not match against schema from 'if' and 'else'"
);
}


@Test
void returnsErrorMessageWhenAnyOfFails() {
String schema = """
Expand All @@ -30,7 +83,7 @@ void returnsErrorMessageWhenAnyOfFails() {
"https://harrel.dev/",
"",
"anyOf",
"Expected object to match at least against one schema. None matched"
"Value does not match against any of the schemas"
);
}

Expand All @@ -55,7 +108,7 @@ void returnsErrorMessageWhenAllOfFails() {
"https://harrel.dev/",
"",
"allOf",
"Object didn't match against all schemas. Unmatched schema indexes [1, 2]"
"Value does not match against the schemas at indexes [1, 2]"
);
}

Expand All @@ -79,7 +132,7 @@ void returnsErrorMessageWhenNotFails() {
"https://harrel.dev/",
"",
"not",
"Object matched against given schema but must not"
"Value matches against given schema but it must not"
);
}

Expand All @@ -103,7 +156,7 @@ void returnsErrorMessageWhenOneOfFails() {
"https://harrel.dev/",
"",
"oneOf",
"Object matched against more than one schema. Matched schema indexes [0, 1]"
"Value matches against more than one schema. Matched schema indexes [0, 1]"
);
}

Expand Down

0 comments on commit bbdd122

Please sign in to comment.