Skip to content

Commit 01dfc0b

Browse files
author
Anderson Ferreira
committed
Implementing IF-THEN-ELSE Conditional (Draft 7)
1 parent 3bcab62 commit 01dfc0b

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/main/java/com/networknt/schema/JsonSchema.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ private Map<String, JsonValidator> read(JsonNode schemaNode) {
145145
Iterator<String> pnames = schemaNode.fieldNames();
146146
while (pnames.hasNext()) {
147147
String pname = pnames.next();
148-
JsonNode n = schemaNode.get(pname);
148+
JsonNode nodeToUse = pname.equals("if") ? schemaNode : schemaNode.get(pname);
149149

150-
JsonValidator validator = validationContext.newValidator(getSchemaPath(), pname, n, this);
150+
JsonValidator validator = validationContext.newValidator(getSchemaPath(), pname, nodeToUse, this);
151151
if (validator != null) {
152152
validators.put(getSchemaPath() + "/" + pname, validator);
153153

154-
if(pname.equals("required"))
154+
if (pname.equals("required"))
155155
requiredValidator = validator;
156156
}
157157

src/main/java/com/networknt/schema/ValidatorTypeCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
6565
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat("{0}: the items in the array must be unique"), UniqueItemsValidator.class),
6666
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}"), null),
6767
UUID("uuid", "1035", new MessageFormat("{0}: {1} is an invalid {2}"), null),
68-
ID("id", "1036", new MessageFormat("{0}: {1} is an invalid segment for URI {2}"), null);
68+
ID("id", "1036", new MessageFormat("{0}: {1} is an invalid segment for URI {2}"), null),
69+
IF_THEN_ELSE("if", "1037", null, IfValidator.class);
6970

7071
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7172

src/test/java/com/networknt/schema/JsonSchemaTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,16 @@ public void testSchemaFromClasspath() throws Exception {
320320
runTestFile("tests/classpath/schema.json");
321321
}
322322

323-
324-
325323
@Test
326324
public void testUUIDValidator() throws Exception {
327325
runTestFile("tests/uuid.json");
328326
}
329327

328+
@Test
329+
public void testIfValidator() throws Exception {
330+
runTestFile("tests/if.json");
331+
}
332+
330333
/**
331334
* Although, the data file has three errors, but only on is reported
332335
*/

0 commit comments

Comments
 (0)