Skip to content

Commit c83d02f

Browse files
committed
initial implementation of nullable support during validation
1 parent 448967f commit c83d02f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ private static boolean isNull(Object obj) {
2424
this.failureReporter = new CollectingFailureReporter(schema);
2525
}
2626

27+
@Override
28+
void visit(Schema schema) {
29+
if (schema.isNullable() == Boolean.FALSE && isNull(subject)) {
30+
failureReporter.failure("value cannot be null", "nullable");
31+
}
32+
super.visit(schema);
33+
}
34+
2735
ValidatingVisitor(Object subject, ValidationFailureReporter failureReporter) {
2836
this.subject = subject;
2937
this.failureReporter = failureReporter;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.everit.json.schema;
2+
3+
import org.json.JSONObject;
4+
import org.junit.Test;
5+
6+
public class NullableValidationTest {
7+
8+
@Test
9+
public void testNullableFalse_JSONNull() {
10+
TestSupport.failureOf(StringSchema.builder().requiresString(false).nullable(false))
11+
.input(JSONObject.NULL)
12+
.expectedKeyword("nullable")
13+
.expectedMessageFragment("value cannot be null")
14+
.expect();
15+
}
16+
17+
@Test
18+
public void testNullableFalse_nullReference() {
19+
TestSupport.failureOf(StringSchema.builder().requiresString(false).nullable(false))
20+
.input(null)
21+
.expectedKeyword("nullable")
22+
.expectedMessageFragment("value cannot be null")
23+
.expect();
24+
}
25+
26+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static org.junit.Assert.assertTrue;
2929

3030
public class StringSchemaTest {
31-
31+
3232
private static Schema loadWithNullableSupport(JSONObject rawSchemaJson) {
3333
return SchemaLoader.builder().nullableSupport(true).schemaJson(rawSchemaJson).build().load().build();
3434
}

0 commit comments

Comments
 (0)