Skip to content

Commit 89d8aa1

Browse files
committed
implemented CombinedSchema#describePropertiesTo()
1 parent b33aca8 commit 89d8aa1

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

core/src/main/java/org/everit/json/schema/CombinedSchema.java

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
*/
1616
package org.everit.json.schema;
1717

18+
import org.everit.json.schema.internal.JSONPrinter;
19+
1820
import java.util.ArrayList;
1921
import java.util.Collection;
2022
import java.util.List;
2123
import java.util.Objects;
2224
import java.util.stream.Collectors;
2325

26+
import static java.lang.String.format;
27+
2428
/**
2529
* Validator for {@code allOf}, {@code oneOf}, {@code anyOf} schemas.
2630
*/
@@ -78,33 +82,63 @@ public interface ValidationCriterion {
7882
/**
7983
* Validation criterion for {@code allOf} schemas.
8084
*/
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+
}
8595
}
96+
97+
@Override
98+
public String toString() {
99+
return "allOf";
100+
}
101+
86102
};
87103

88104
/**
89105
* Validation criterion for {@code anyOf} schemas.
90106
*/
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";
96121
}
97122
};
98123

99124
/**
100125
* Validation criterion for {@code oneOf} schemas.
101126
*/
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+
};
108142

109143
public static Builder allOf(final Collection<Schema> schemas) {
110144
return builder(schemas).criterion(ALL_CRITERION);
@@ -204,6 +238,14 @@ public boolean equals(Object o) {
204238
}
205239
}
206240

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+
207249
@Override
208250
public int hashCode() {
209251
return Objects.hash(super.hashCode(), subschemas, criterion);

core/src/test/java/org/everit/json/schema/CombinedSchemaTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717

1818
import nl.jqno.equalsverifier.EqualsVerifier;
1919
import nl.jqno.equalsverifier.Warning;
20+
import org.json.JSONObject;
2021
import org.junit.Assert;
2122
import org.junit.Test;
2223

2324
import java.util.Arrays;
2425
import java.util.List;
2526

27+
import static java.util.Arrays.asList;
28+
import static org.junit.Assert.assertTrue;
29+
import static org.junit.Assert.assertEquals;
30+
2631
public class CombinedSchemaTest {
2732

28-
private static final List<Schema> SUBSCHEMAS = Arrays.asList(
33+
private static final List<Schema> SUBSCHEMAS = asList(
2934
NumberSchema.builder().multipleOf(10).build(),
3035
NumberSchema.builder().multipleOf(3).build());
3136

@@ -51,17 +56,17 @@ public void anyCriterionSuccess() {
5156

5257
@Test(expected = ValidationException.class)
5358
public void anyOfInvalid() {
54-
CombinedSchema.anyOf(Arrays.asList(
59+
CombinedSchema.anyOf(asList(
5560
StringSchema.builder().maxLength(2).build(),
5661
StringSchema.builder().minLength(4).build()))
5762
.build().validate("foo");
5863
}
5964

6065
@Test
6166
public void factories() {
62-
CombinedSchema.allOf(Arrays.asList(BooleanSchema.INSTANCE));
63-
CombinedSchema.anyOf(Arrays.asList(BooleanSchema.INSTANCE));
64-
CombinedSchema.oneOf(Arrays.asList(BooleanSchema.INSTANCE));
67+
CombinedSchema.allOf(asList(BooleanSchema.INSTANCE));
68+
CombinedSchema.anyOf(asList(BooleanSchema.INSTANCE));
69+
CombinedSchema.oneOf(asList(BooleanSchema.INSTANCE));
6570
}
6671

6772
@Test(expected = ValidationException.class)
@@ -104,7 +109,7 @@ public void reportCauses() {
104109
CombinedSchema.allOf(SUBSCHEMAS).build().validate(24);
105110
Assert.fail("did not throw exception");
106111
} catch (ValidationException e) {
107-
Assert.assertEquals(1, e.getCausingExceptions().size());
112+
assertEquals(1, e.getCausingExceptions().size());
108113
}
109114
}
110115

@@ -116,4 +121,17 @@ public void equalsVerifier() {
116121
.verify();
117122
}
118123

124+
@Test
125+
public void toStringTest() {
126+
CombinedSchema subject = CombinedSchema
127+
.allOf(asList(BooleanSchema.INSTANCE, NullSchema.INSTANCE))
128+
.build();
129+
JSONObject actual = new JSONObject(subject.toString());
130+
assertTrue(ObjectComparator.deepEquals(new JSONObject("{\"allOf\":["
131+
+ BooleanSchema.INSTANCE.toString()
132+
+ ", "
133+
+ NullSchema.INSTANCE
134+
+ "]}"), actual));
135+
}
136+
119137
}

0 commit comments

Comments
 (0)