Skip to content

Commit 48cdd3b

Browse files
committed
adding ValidatingVisitor#passesTypeCheck()
This method handles the possible combinations of the Schema#requires* methods and the nullable flag It is currently only used by ObjectSchema Also removing unused ValidatingVisitor ctor.
1 parent c83d02f commit 48cdd3b

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public ObjectSchemaValidatingVisitor(Object subject, ValidatingVisitor owner) {
2828
}
2929

3030
@Override void visitObjectSchema(ObjectSchema objectSchema) {
31-
if (!(subject instanceof JSONObject)) {
32-
if (objectSchema.requiresObject()) {
33-
owner.failure(JSONObject.class, subject);
34-
}
35-
} else {
31+
if (owner.passesTypeCheck(JSONObject.class, objectSchema.requiresObject(), objectSchema.isNullable())) {
3632
objSubject = (JSONObject) subject;
3733
objectSize = objSubject.length();
3834
this.schema = objectSchema;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ public StringSchemaValidatingVisitor(Object subject, ValidationFailureReporter f
6161
failureReporter.failure(failure.get(), "format");
6262
}
6363
}
64+
6465
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ private static boolean isNull(Object obj) {
1919

2020
private ValidationFailureReporter failureReporter;
2121

22-
ValidatingVisitor(Schema schema, Object subject) {
23-
this.subject = subject;
24-
this.failureReporter = new CollectingFailureReporter(schema);
25-
}
26-
2722
@Override
2823
void visit(Schema schema) {
2924
if (schema.isNullable() == Boolean.FALSE && isNull(subject)) {
@@ -156,4 +151,19 @@ void failure(ValidationException exc) {
156151
failureReporter.failure(exc);
157152
}
158153

154+
boolean passesTypeCheck(Class<?> expectedType, boolean schemaRequiresType, Boolean nullable) {
155+
if (isNull(subject)) {
156+
if (schemaRequiresType && nullable != Boolean.TRUE) {
157+
failureReporter.failure(expectedType, subject);
158+
}
159+
return false;
160+
}
161+
if (expectedType.isAssignableFrom(subject.getClass())) {
162+
return true;
163+
}
164+
if (schemaRequiresType) {
165+
failureReporter.failure(expectedType, subject);
166+
}
167+
return false;
168+
}
159169
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,9 @@ public void emptyObjectPropertyNamesSchema() {
439439

440440
subject.validate(new JSONObject("{}"));
441441
}
442+
443+
@Test
444+
public void requiresObject_stillNullable() {
445+
ObjectSchema.builder().requiresObject(true).nullable(true).build().validate(JSONObject.NULL);
446+
}
442447
}

0 commit comments

Comments
 (0)