|
15 | 15 | */
|
16 | 16 | package org.everit.json.schema;
|
17 | 17 |
|
| 18 | +import org.everit.json.schema.internal.JSONPrinter; |
| 19 | + |
18 | 20 | import java.util.ArrayList;
|
19 | 21 | import java.util.Collection;
|
20 | 22 | import java.util.List;
|
21 | 23 | import java.util.Objects;
|
22 | 24 | import java.util.stream.Collectors;
|
23 | 25 |
|
| 26 | +import static java.lang.String.format; |
| 27 | + |
24 | 28 | /**
|
25 | 29 | * Validator for {@code allOf}, {@code oneOf}, {@code anyOf} schemas.
|
26 | 30 | */
|
@@ -78,33 +82,63 @@ public interface ValidationCriterion {
|
78 | 82 | /**
|
79 | 83 | * Validation criterion for {@code allOf} schemas.
|
80 | 84 | */
|
81 |
| - public static final ValidationCriterion ALL_CRITERION = (subschemaCount, matchingCount) -> { |
82 |
| - if (matchingCount < subschemaCount) { |
83 |
| - throw new ValidationException(null, String.format("only %d subschema matches out of %d", |
84 |
| - matchingCount, subschemaCount), "allOf"); |
| 85 | + public static final ValidationCriterion ALL_CRITERION = new ValidationCriterion() { |
| 86 | + |
| 87 | + @Override |
| 88 | + public void validate(int subschemaCount, int matchingCount) { |
| 89 | + if (matchingCount < subschemaCount) { |
| 90 | + throw new ValidationException(null, |
| 91 | + format("only %d subschema matches out of %d", matchingCount, subschemaCount), |
| 92 | + "allOf" |
| 93 | + ); |
| 94 | + } |
85 | 95 | }
|
| 96 | + |
| 97 | + @Override |
| 98 | + public String toString() { |
| 99 | + return "allOf"; |
| 100 | + } |
| 101 | + |
86 | 102 | };
|
87 | 103 |
|
88 | 104 | /**
|
89 | 105 | * Validation criterion for {@code anyOf} schemas.
|
90 | 106 | */
|
91 |
| - public static final ValidationCriterion ANY_CRITERION = (subschemaCount, matchingCount) -> { |
92 |
| - if (matchingCount == 0) { |
93 |
| - throw new ValidationException(null, String.format( |
94 |
| - "no subschema matched out of the total %d subschemas", |
95 |
| - subschemaCount), "anyOf"); |
| 107 | + public static final ValidationCriterion ANY_CRITERION = new ValidationCriterion() { |
| 108 | + |
| 109 | + @Override |
| 110 | + public void validate(int subschemaCount, int matchingCount) { |
| 111 | + if (matchingCount == 0) { |
| 112 | + throw new ValidationException(null, format( |
| 113 | + "no subschema matched out of the total %d subschemas", |
| 114 | + subschemaCount), "anyOf"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String toString() { |
| 120 | + return "anyOf"; |
96 | 121 | }
|
97 | 122 | };
|
98 | 123 |
|
99 | 124 | /**
|
100 | 125 | * Validation criterion for {@code oneOf} schemas.
|
101 | 126 | */
|
102 |
| - public static final ValidationCriterion ONE_CRITERION = (subschemaCount, matchingCount) -> { |
103 |
| - if (matchingCount != 1) { |
104 |
| - throw new ValidationException(null, String.format("%d subschemas matched instead of one", |
105 |
| - matchingCount), "oneOf"); |
106 |
| - } |
107 |
| - }; |
| 127 | + public static final ValidationCriterion ONE_CRITERION = |
| 128 | + new ValidationCriterion() { |
| 129 | + |
| 130 | + @Override |
| 131 | + public void validate(int subschemaCount, int matchingCount) { |
| 132 | + if (matchingCount != 1) { |
| 133 | + throw new ValidationException(null, format("%d subschemas matched instead of one", |
| 134 | + matchingCount), "oneOf"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override public String toString() { |
| 139 | + return "oneOf"; |
| 140 | + } |
| 141 | + }; |
108 | 142 |
|
109 | 143 | public static Builder allOf(final Collection<Schema> schemas) {
|
110 | 144 | return builder(schemas).criterion(ALL_CRITERION);
|
@@ -204,6 +238,14 @@ public boolean equals(Object o) {
|
204 | 238 | }
|
205 | 239 | }
|
206 | 240 |
|
| 241 | + @Override |
| 242 | + void describePropertiesTo(JSONPrinter writer) { |
| 243 | + writer.key(criterion.toString()); |
| 244 | + writer.array(); |
| 245 | + subschemas.forEach(subschema -> subschema.describeTo(writer)); |
| 246 | + writer.endArray(); |
| 247 | + } |
| 248 | + |
207 | 249 | @Override
|
208 | 250 | public int hashCode() {
|
209 | 251 | return Objects.hash(super.hashCode(), subschemas, criterion);
|
|
0 commit comments