Skip to content

Commit 9f8febd

Browse files
committed
fixes #54 add boolean schema validator to resolve some of the test cases
1 parent cc0c136 commit 9f8febd

File tree

7 files changed

+72
-33
lines changed

7 files changed

+72
-33
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Collections;
8+
import java.util.LinkedHashSet;
9+
import java.util.Set;
10+
11+
public class FalseValidator extends BaseJsonValidator implements JsonValidator {
12+
private static final Logger logger = LoggerFactory.getLogger(FalseValidator.class);
13+
14+
public FalseValidator(String schemaPath, final JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
15+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FALSE, validationContext);
16+
}
17+
18+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
19+
debug(logger, node, rootNode, at);
20+
// For the false validator, it is always not valid
21+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
22+
errors.add(buildValidationMessage(at));
23+
return Collections.unmodifiableSet(errors);
24+
}
25+
}

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,29 @@ public JsonSchema findAncestor() {
142142

143143
private Map<String, JsonValidator> read(JsonNode schemaNode) {
144144
Map<String, JsonValidator> validators = new HashMap<String, JsonValidator>();
145-
Iterator<String> pnames = schemaNode.fieldNames();
146-
while (pnames.hasNext()) {
147-
String pname = pnames.next();
148-
JsonNode nodeToUse = pname.equals("if") ? schemaNode : schemaNode.get(pname);
149-
150-
JsonValidator validator = validationContext.newValidator(getSchemaPath(), pname, nodeToUse, this);
151-
if (validator != null) {
152-
validators.put(getSchemaPath() + "/" + pname, validator);
153-
154-
if (pname.equals("required"))
155-
requiredValidator = validator;
145+
if(schemaNode.isBoolean()) {
146+
if(schemaNode.booleanValue()) {
147+
JsonValidator validator = validationContext.newValidator(getSchemaPath(), "true", schemaNode, this);
148+
validators.put(getSchemaPath() + "/true", validator);
149+
} else {
150+
JsonValidator validator = validationContext.newValidator(getSchemaPath(), "false", schemaNode, this);
151+
validators.put(getSchemaPath() + "/false", validator);
156152
}
153+
} else {
154+
Iterator<String> pnames = schemaNode.fieldNames();
155+
while (pnames.hasNext()) {
156+
String pname = pnames.next();
157+
JsonNode nodeToUse = pname.equals("if") ? schemaNode : schemaNode.get(pname);
158+
159+
JsonValidator validator = validationContext.newValidator(getSchemaPath(), pname, nodeToUse, this);
160+
if (validator != null) {
161+
validators.put(getSchemaPath() + "/" + pname, validator);
162+
163+
if (pname.equals("required"))
164+
requiredValidator = validator;
165+
}
157166

167+
}
158168
}
159169
return validators;
160170
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Collections;
8+
import java.util.Set;
9+
10+
public class TrueValidator extends BaseJsonValidator implements JsonValidator {
11+
private static final Logger logger = LoggerFactory.getLogger(TrueValidator.class);
12+
13+
public TrueValidator(String schemaPath, final JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
14+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.TRUE, validationContext);
15+
}
16+
17+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
18+
debug(logger, node, rootNode, at);
19+
// For the true validator, it is always valid which means there is no ValidationMessage.
20+
return Collections.emptySet();
21+
}
22+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
6868
ID("id", "1036", new MessageFormat("{0}: {1} is an invalid segment for URI {2}"), null, 15),
6969
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909
7070
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
71-
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat("{0}: must have a exclusive minimum value of {1}"), ExclusiveMinimumValidator.class, 14);
71+
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat("{0}: must have a exclusive minimum value of {1}"), ExclusiveMinimumValidator.class, 14),
72+
TRUE("true", "1040", null, TrueValidator.class, 14),
73+
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14);
7274

7375
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7476
private static SpecVersion specVersion = new SpecVersion();

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ public void testAdditionalPropertiesValidator() throws Exception {
240240
}
241241

242242
@Test
243-
@Ignore
244243
public void testAllOfValidator() throws Exception {
245244
runTestFile("draft2019-09/allOf.json");
246245
}
@@ -252,13 +251,11 @@ public void testAnchorValidator() throws Exception {
252251
}
253252

254253
@Test
255-
@Ignore
256254
public void testAnyOfValidator() throws Exception {
257255
runTestFile("draft2019-09/anyOf.json");
258256
}
259257

260258
@Test
261-
@Ignore
262259
public void testBooleanSchemaValidator() throws Exception {
263260
runTestFile("draft2019-09/boolean_schema.json");
264261
}
@@ -376,13 +373,11 @@ public void testMultipleOfValidator() throws Exception {
376373
}
377374

378375
@Test
379-
@Ignore
380376
public void testNotValidator() throws Exception {
381377
runTestFile("draft2019-09/not.json");
382378
}
383379

384380
@Test
385-
@Ignore
386381
public void testOneOfValidator() throws Exception {
387382
runTestFile("draft2019-09/oneOf.json");
388383
}
@@ -393,13 +388,11 @@ public void testPatternValidator() throws Exception {
393388
}
394389

395390
@Test
396-
@Ignore
397391
public void testPatternPropertiesValidator() throws Exception {
398392
runTestFile("draft2019-09/patternProperties.json");
399393
}
400394

401395
@Test
402-
@Ignore
403396
public void testPropertiesValidator() throws Exception {
404397
runTestFile("draft2019-09/properties.json");
405398
}
@@ -438,3 +431,4 @@ public void testUniqueItemsValidator() throws Exception {
438431
}
439432

440433
}
434+

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,16 @@ public void testAdditionalPropertiesValidator() throws Exception {
140140
}
141141

142142
@Test
143-
@Ignore
144143
public void testAllOfValidator() throws Exception {
145144
runTestFile("draft6/allOf.json");
146145
}
147146

148147
@Test
149-
@Ignore
150148
public void testAnyOfValidator() throws Exception {
151149
runTestFile("draft6/anyOf.json");
152150
}
153151

154152
@Test
155-
@Ignore
156153
public void testBooleanSchemaValidator() throws Exception {
157154
runTestFile("draft6/boolean_schema.json");
158155
}
@@ -258,13 +255,11 @@ public void testMultipleOfValidator() throws Exception {
258255
}
259256

260257
@Test
261-
@Ignore
262258
public void testNotValidator() throws Exception {
263259
runTestFile("draft6/not.json");
264260
}
265261

266262
@Test
267-
@Ignore
268263
public void testOneOfValidator() throws Exception {
269264
runTestFile("draft6/oneOf.json");
270265
}
@@ -275,13 +270,11 @@ public void testPatternValidator() throws Exception {
275270
}
276271

277272
@Test
278-
@Ignore
279273
public void testPatternPropertiesValidator() throws Exception {
280274
runTestFile("draft6/patternProperties.json");
281275
}
282276

283277
@Test
284-
@Ignore
285278
public void testPropertiesValidator() throws Exception {
286279
runTestFile("draft6/properties.json");
287280
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ public void testAdditionalPropertiesValidator() throws Exception {
236236
}
237237

238238
@Test
239-
@Ignore
240239
public void testAllOfValidator() throws Exception {
241240
runTestFile("draft7/allOf.json");
242241
}
@@ -248,13 +247,11 @@ public void testAnchorValidator() throws Exception {
248247
}
249248

250249
@Test
251-
@Ignore
252250
public void testAnyOfValidator() throws Exception {
253251
runTestFile("draft7/anyOf.json");
254252
}
255253

256254
@Test
257-
@Ignore
258255
public void testBooleanSchemaValidator() throws Exception {
259256
runTestFile("draft7/boolean_schema.json");
260257
}
@@ -372,13 +369,11 @@ public void testMultipleOfValidator() throws Exception {
372369
}
373370

374371
@Test
375-
@Ignore
376372
public void testNotValidator() throws Exception {
377373
runTestFile("draft7/not.json");
378374
}
379375

380376
@Test
381-
@Ignore
382377
public void testOneOfValidator() throws Exception {
383378
runTestFile("draft7/oneOf.json");
384379
}
@@ -389,13 +384,11 @@ public void testPatternValidator() throws Exception {
389384
}
390385

391386
@Test
392-
@Ignore
393387
public void testPatternPropertiesValidator() throws Exception {
394388
runTestFile("draft7/patternProperties.json");
395389
}
396390

397391
@Test
398-
@Ignore
399392
public void testPropertiesValidator() throws Exception {
400393
runTestFile("draft7/properties.json");
401394
}

0 commit comments

Comments
 (0)