|
@@ -2,9 +2,13 @@ package org.leumasjaffe.json.schema.tester;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
import org.leumasjaffe.json.JsonHelper;
|
|
import org.leumasjaffe.json.JsonHelper;
|
|
|
import org.leumasjaffe.json.schema.Tester;
|
|
import org.leumasjaffe.json.schema.Tester;
|
|
|
|
|
+import org.leumasjaffe.json.schema.ValidationException;
|
|
|
import org.leumasjaffe.json.schema.ArrayTester;
|
|
import org.leumasjaffe.json.schema.ArrayTester;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
@@ -21,6 +25,14 @@ public class ItemsTester implements ArrayTester {
|
|
|
public ItemsTester(Tester...testers) {
|
|
public ItemsTester(Tester...testers) {
|
|
|
this(Arrays.asList(testers));
|
|
this(Arrays.asList(testers));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void validate(final JsonNode node) {
|
|
|
|
|
+ final List<ValidationException> exceptions = this.new Helper(node).validate();
|
|
|
|
|
+ if (!exceptions.isEmpty()) {
|
|
|
|
|
+ throw new ValidationException("items", "items did not match", exceptions);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean accepts(JsonNode node) {
|
|
public boolean accepts(JsonNode node) {
|
|
@@ -44,4 +56,27 @@ public class ItemsTester implements ArrayTester {
|
|
|
return out;
|
|
return out;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @RequiredArgsConstructor
|
|
|
|
|
+ @FieldDefaults(makeFinal=true)
|
|
|
|
|
+ private class Helper {
|
|
|
|
|
+ JsonNode node;
|
|
|
|
|
+
|
|
|
|
|
+ List<ValidationException> validate() {
|
|
|
|
|
+ final int elems = Math.min(schemas.size(), node.size());
|
|
|
|
|
+ return IntStream.range(0, elems).mapToObj(this::validate)
|
|
|
|
|
+ .filter(Optional::isPresent).map(Optional::get)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Optional<ValidationException> validate(int i) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ schemas.get(i).validate(node.get(i));
|
|
|
|
|
+ } catch (ValidationException ve) {
|
|
|
|
|
+ return Optional.of(new ValidationException(i,
|
|
|
|
|
+ "failed to match schema", ve));
|
|
|
|
|
+ }
|
|
|
|
|
+ return Optional.empty();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|