Skip to content

Applicators error messages part 2 #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/main/java/dev/harrel/jsonschema/Applicators.java
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
.filter(i -> ctx.resolveInternalRefAndValidate(schemaRef, array.get(i)))
.boxed()
.collect(Collectors.toList()));
return minContainsZero || !indices.isEmpty() ? Result.success(indices) : Result.failure();
return minContainsZero || !indices.isEmpty() ? Result.success(indices) : Result.failure("Array contains no matching items");
}
}

@@ -265,13 +265,14 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
.stream()
.filter(dependentSchemas::containsKey)
.collect(Collectors.toList());

boolean result = fields.stream()
.map(dependentSchemas::get)
.filter(ref -> ctx.resolveInternalRefAndValidate(ref, node))
.count() == fields.size();

return result ? Result.success() : Result.failure();
List<String> failedFields = fields.stream()
.filter(field -> !ctx.resolveInternalRefAndValidate(dependentSchemas.get(field), node))
.collect(Collectors.toList());
if (failedFields.isEmpty()) {
return Result.success();
} else {
return Result.failure(String.format("Object does not match dependent schemas for some properties %s", failedFields));
}
}
}

25 changes: 18 additions & 7 deletions src/test/java/dev/harrel/jsonschema/ExhaustiveEvaluationTest.java
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@@ -269,38 +268,50 @@ void dependentSchemas() {
{
"dependentSchemas": {
"a": {
"type": "string"
"type": "object"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
}
}
}""";
String instance = """
{
"a": true,
"b": null
"a": 1,
"b": null,
"c": null
}""";
Validator.Result result = new ValidatorFactory().validate(schema, instance);
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(2);
assertThat(errors).hasSize(3);
assertError(
errors.get(0),
"/dependentSchemas/a/type",
"/dependentSchemas/b/type",
"https://harrel.dev/",
"",
"type",
"Value is [object] but should be [string]"
);
assertError(
errors.get(1),
"/dependentSchemas/b/type",
"/dependentSchemas/c/type",
"https://harrel.dev/",
"",
"type",
"Value is [object] but should be [string]"
);
assertError(
errors.get(2),
"/dependentSchemas",
"https://harrel.dev/",
"",
"dependentSchemas",
"Object does not match dependent schemas for some properties [b, c]"
);
}

@Test
129 changes: 128 additions & 1 deletion src/test/java/dev/harrel/jsonschema/ValidatorResultTest.java
Original file line number Diff line number Diff line change
@@ -10,6 +10,133 @@

class ValidatorResultTest {

@Test
void returnsErrorMessageWhenContainsFails() {
String schema = """
{
"contains": {
"type": "null"
}
}""";
String instance = "[0, 1]";
Validator.Result result = new ValidatorFactory().validate(schema, instance);
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(3);
assertError(
errors.get(0),
"/contains/type",
"https://harrel.dev/",
"/0",
"type",
"Value is [integer] but should be [null]"
);
assertError(
errors.get(1),
"/contains/type",
"https://harrel.dev/",
"/1",
"type",
"Value is [integer] but should be [null]"
);
assertError(
errors.get(2),
"/contains",
"https://harrel.dev/",
"",
"contains",
"Array contains no matching items"
);
}

@Test
void returnsErrorMessageWhenMinContainsFails() {
String schema = """
{
"contains": {
"type": "null"
},
"minContains": 2
}""";
String instance = "[0, 1, null]";
Validator.Result result = new ValidatorFactory().validate(schema, instance);
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertError(
errors.get(0),
"/minContains",
"https://harrel.dev/",
"",
"minContains",
"Array contains less than 2 matching items"
);
}

@Test
void returnsErrorMessageWhenMaxContainsFails() {
String schema = """
{
"contains": {
"type": "null"
},
"maxContains": 2
}""";
String instance = "[null, null, null]";
Validator.Result result = new ValidatorFactory().validate(schema, instance);
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertError(
errors.get(0),
"/maxContains",
"https://harrel.dev/",
"",
"maxContains",
"Array contains more than 2 matching items"
);
}

@Test
void returnsErrorMessageWhenDependentSchemasFails() {
String schema = """
{
"dependentSchemas": {
"a": {
"type": "object"
},
"b": {
"type": "string"
}
}
}""";
String instance = """
{
"a": 1,
"b": null
}""";
Validator.Result result = new ValidatorFactory().validate(schema, instance);
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(2);
assertError(
errors.get(0),
"/dependentSchemas/b/type",
"https://harrel.dev/",
"",
"type",
"Value is [object] but should be [string]"
);
assertError(
errors.get(1),
"/dependentSchemas",
"https://harrel.dev/",
"",
"dependentSchemas",
"Object does not match dependent schemas for some properties [b]"
);
}

@Test
void returnsErrorMessageWhenIfThenFails() {
String schema = """
@@ -183,7 +310,7 @@ void returnsOnlyDirectErrors() {
}

@Test
void discardAnnotationCorrectly() {
void discardsAnnotationCorrectly() {
String schema = """
{
"anyOf": [true, {