Skip to content
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

Fix/exhaustive evaluation #50

Merged
merged 11 commits into from
Sep 6, 2023
55 changes: 38 additions & 17 deletions src/main/java/dev/harrel/jsonschema/Applicators.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
}

List<JsonNode> elements = node.asArray();
boolean valid = IntStream.range(0, elements.size())
.limit(prefixRefs.size())
int size = Math.min(prefixRefs.size(), elements.size());
boolean valid = IntStream.range(0, size)
.boxed()
.allMatch(idx -> ctx.resolveInternalRefAndValidate(prefixRefs.get(idx), elements.get(idx)));
.filter(idx -> ctx.resolveInternalRefAndValidate(prefixRefs.get(idx), elements.get(idx)))
.count() == size;
return valid ? Result.success(prefixRefs.size()) : Result.failure();
}
}
Expand All @@ -62,10 +63,13 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
if (!node.isArray()) {
return Result.success();
}
Integer prefixItemsSize = ctx.getSiblingAnnotation(Keyword.PREFIX_ITEMS, Integer.class).orElse(0);
boolean valid = node.asArray().stream()
List<JsonNode> array = node.asArray();
int prefixItemsSize = ctx.getSiblingAnnotation(Keyword.PREFIX_ITEMS, Integer.class).orElse(0);
int size = Math.max(array.size() - prefixItemsSize, 0);
boolean valid = array.stream()
.skip(prefixItemsSize)
.allMatch(element -> ctx.resolveInternalRefAndValidate(schemaRef, element));
.filter(element -> ctx.resolveInternalRefAndValidate(schemaRef, element))
.count() == size;
return valid ? Result.success(true) : Result.failure();
}

Expand Down Expand Up @@ -140,7 +144,9 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
.filter(e -> !props.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

boolean valid = filtered.values().stream().allMatch(prop -> ctx.resolveInternalRefAndValidate(schemaRef, prop));
boolean valid = filtered.values().stream()
.filter(prop -> ctx.resolveInternalRefAndValidate(schemaRef, prop))
.count() == filtered.size();
return valid ? Result.success(unmodifiableSet(filtered.keySet())) : Result.failure();
}
@Override
Expand Down Expand Up @@ -183,7 +189,8 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
.entrySet()
.stream()
.map(e -> new AbstractMap.SimpleEntry<>(schemaRefs.get(e.getKey()), e.getValue()))
.allMatch(e -> ctx.resolveInternalRefAndValidate(e.getKey(), e.getValue()));
.filter(e -> ctx.resolveInternalRefAndValidate(e.getKey(), e.getValue()))
.count() == filtered.size();
return valid ? Result.success(unmodifiableSet(filtered.keySet())) : Result.failure();
}
}
Expand Down Expand Up @@ -220,7 +227,9 @@ public Result evaluate(EvaluationContext ctx, JsonNode node) {
if (!schemaRefs.isEmpty()) {
processed.add(entry.getKey());
}
valid = schemaRefs.stream().allMatch(ref -> ctx.resolveInternalRefAndValidate(ref, entry.getValue())) && valid;
valid = schemaRefs.stream()
.filter(ref -> ctx.resolveInternalRefAndValidate(ref, entry.getValue()))
.count() == schemaRefs.size() && valid;
}
return valid ? Result.success(unmodifiableSet(processed)) : Result.failure();
}
Expand All @@ -244,11 +253,15 @@ public boolean apply(EvaluationContext ctx, JsonNode node) {
return true;
}

return node.asObject().keySet()

List<String> fields = node.asObject().keySet()
.stream()
.filter(dependentSchemas::containsKey)
.collect(Collectors.toList());
return fields.stream()
.map(dependentSchemas::get)
.allMatch(ref -> ctx.resolveInternalRefAndValidate(ref, node));
.filter(ref -> ctx.resolveInternalRefAndValidate(ref, node))
.count() == fields.size();
}
}

Expand All @@ -268,8 +281,10 @@ public boolean apply(EvaluationContext ctx, JsonNode node) {
return true;
}

return node.asObject().keySet().stream()
.allMatch(propName -> ctx.resolveInternalRefAndValidate(schemaRef, new StringNode(propName, node.getJsonPointer())));
Map<String, JsonNode> object = node.asObject();
return object.keySet().stream()
.filter(propName -> ctx.resolveInternalRefAndValidate(schemaRef, new StringNode(propName, node.getJsonPointer())))
.count() == object.size();
}
}

Expand Down Expand Up @@ -401,10 +416,13 @@ public boolean apply(EvaluationContext ctx, JsonNode node) {
List<EvaluationItem> evaluationItems = unmodifiableList(ctx.getAnnotations().stream()
.filter(a -> getSchemaPath(a).startsWith(parentPath))
.collect(Collectors.toList()));
return node.asArray()
List<JsonNode> array = node.asArray()
.stream()
.filter(arrayNode -> evaluationItems.stream().noneMatch(a -> a.getInstanceLocation().startsWith(arrayNode.getJsonPointer())))
.allMatch(arrayNode -> ctx.resolveInternalRefAndValidate(schemaRef, arrayNode));
.collect(Collectors.toList());
return array.stream()
.filter(arrayNode -> ctx.resolveInternalRefAndValidate(schemaRef, arrayNode))
.count() == array.size();
}

@Override
Expand Down Expand Up @@ -444,11 +462,14 @@ public boolean apply(EvaluationContext ctx, JsonNode node) {
List<EvaluationItem> evaluationItems = unmodifiableList(ctx.getAnnotations().stream()
.filter(a -> getSchemaPath(a).startsWith(parentPath))
.collect(Collectors.toList()));
return node.asObject()
List<JsonNode> array = node.asObject()
.values()
.stream()
.filter(propertyNode -> evaluationItems.stream().noneMatch(a -> a.getInstanceLocation().startsWith(propertyNode.getJsonPointer())))
.allMatch(propertyNode -> ctx.resolveInternalRefAndValidate(schemaRef, propertyNode));
.collect(Collectors.toList());
return array.stream()
.filter(propertyNode -> ctx.resolveInternalRefAndValidate(schemaRef, propertyNode))
.count() == array.size();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/harrel/jsonschema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class Schema {
private static final Evaluator TRUE_EVALUATOR = (ctx, node) -> Result.success();
private static final Evaluator FALSE_EVALUATOR = (ctx, node) -> Result.failure("False schema always fails.");
private static final Evaluator FALSE_EVALUATOR = (ctx, node) -> Result.failure("False schema always fails");

private final URI parentUri;
private final String schemaLocation;
Expand Down
87 changes: 52 additions & 35 deletions src/test/java/dev/harrel/jsonschema/EvaluationPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URI;
import java.util.List;

import static dev.harrel.jsonschema.TestUtil.assertError;
import static dev.harrel.jsonschema.TestUtil.readResource;
import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -28,11 +29,13 @@ void withProperties() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/properties/foo/$ref/properties/bar/$ref/type");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:test#/$defs/reffed2");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/foo/bar");
assertThat(errors.get(0).getKeyword()).isEqualTo("type");
assertThat(errors.get(0).getError()).isNotNull();
assertError(
errors.get(0),
"/properties/foo/$ref/properties/bar/$ref/type",
"urn:test#/$defs/reffed2",
"/foo/bar",
"type"
);
}

@Test
Expand All @@ -43,11 +46,13 @@ void withAnchor() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/properties/foo/$ref/properties/bar/$ref/type");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:test#/$defs/reffed2");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/foo/bar");
assertThat(errors.get(0).getKeyword()).isEqualTo("type");
assertThat(errors.get(0).getError()).isNotNull();
assertError(
errors.get(0),
"/properties/foo/$ref/properties/bar/$ref/type",
"urn:test#/$defs/reffed2",
"/foo/bar",
"type"
);
}

@Test
Expand All @@ -58,11 +63,13 @@ void withDynamicAnchor() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/properties/foo/$ref/properties/bar/$ref/type");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:test#/$defs/reffed2");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/foo/bar");
assertThat(errors.get(0).getKeyword()).isEqualTo("type");
assertThat(errors.get(0).getError()).isNotNull();
assertError(
errors.get(0),
"/properties/foo/$ref/properties/bar/$ref/type",
"urn:test#/$defs/reffed2",
"/foo/bar",
"type"
);
}

@Test
Expand All @@ -73,11 +80,13 @@ void withId() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/properties/foo/$ref/properties/bar/$ref/type");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:test#/$defs/reffed2");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/foo/bar");
assertThat(errors.get(0).getKeyword()).isEqualTo("type");
assertThat(errors.get(0).getError()).isNotNull();
assertError(
errors.get(0),
"/properties/foo/$ref/properties/bar/$ref/type",
"urn:test#/$defs/reffed2",
"/foo/bar",
"type"
);
}

@Test
Expand All @@ -89,11 +98,13 @@ void withRemote() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/properties/foo/$ref/properties/bar/$ref/type");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:anchor#/$defs/reffed2");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/foo/bar");
assertThat(errors.get(0).getKeyword()).isEqualTo("type");
assertThat(errors.get(0).getError()).isNotNull();
assertError(
errors.get(0),
"/properties/foo/$ref/properties/bar/$ref/type",
"urn:anchor#/$defs/reffed2",
"/foo/bar",
"type"
);
}

@Test
Expand All @@ -104,11 +115,14 @@ void withFlat() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/properties/foo/$ref/properties/bar/$ref/properties/baz");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:test#/properties/baz");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/foo/bar/baz");
assertThat(errors.get(0).getKeyword()).isNull();
assertThat(errors.get(0).getError()).isEqualTo("False schema always fails.");
assertError(
errors.get(0),
"/properties/foo/$ref/properties/bar/$ref/properties/baz",
"urn:test#/properties/baz",
"/foo/bar/baz",
null,
"False schema always fails"
);
}

@Test
Expand All @@ -119,10 +133,13 @@ void withItems() {
assertThat(result.isValid()).isFalse();
List<Error> errors = result.getErrors();
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getEvaluationPath()).isEqualTo("/items/$ref/items/$ref");
assertThat(errors.get(0).getSchemaLocation()).isEqualTo("urn:test#/custom/2/0/1");
assertThat(errors.get(0).getInstanceLocation()).isEqualTo("/0/0");
assertThat(errors.get(0).getKeyword()).isNull();
assertThat(errors.get(0).getError()).isEqualTo("False schema always fails.");
assertError(
errors.get(0),
"/items/$ref/items/$ref",
"urn:test#/custom/2/0/1",
"/0/0",
null,
"False schema always fails"
);
}
}
Loading