Skip to content

Commit 2135906

Browse files
authored
Merge pull request #184 from networknt/issue183
Issue183
2 parents aaa9e18 + 3b21a47 commit 2135906

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.fasterxml.jackson.databind.node.NullNode;
21+
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
2224

@@ -49,6 +51,17 @@ public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSc
4951
separator = ", ";
5052
}
5153

54+
// check if the parent schema declares the fields as nullable
55+
if(config.isHandleNullableField()) {
56+
JsonNode nullable = parentSchema.getSchemaNode().get("nullable");
57+
if (nullable != null && nullable.asBoolean()) {
58+
nodes.add(NullNode.getInstance());
59+
separator = ", ";
60+
sb.append(separator);
61+
sb.append("null");
62+
}
63+
}
64+
//
5265
sb.append(']');
5366

5467
error = sb.toString();

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public class SchemaValidatorsConfig {
3838
*/
3939
private Map<String, String> uriMappings = new HashMap<String, String>();
4040

41+
/**
42+
* When a field is set as nullable in the OpenAPI specification, the schema validator validates that it is nullable
43+
* however continues with validation against the nullable field
44+
*
45+
* If handleNullableField is set to true && incoming field is nullable && value is field: null --> succeed
46+
* If handleNullableField is set to false && incoming field is nullable && value is field: null --> it is up to the type
47+
* validator using the SchemaValidator to handle it.
48+
*/
49+
private boolean handleNullableField = true;
50+
4151
public boolean isTypeLoose() {
4252
return typeLoose;
4353
}
@@ -63,7 +73,15 @@ public void setMissingNodeAsError(boolean missingNodeAsError) {
6373
this.missingNodeAsError = missingNodeAsError;
6474
}
6575

66-
public SchemaValidatorsConfig() {
76+
public boolean isHandleNullableField() {
77+
return handleNullableField;
78+
}
79+
80+
public void setHandleNullableField(boolean handleNullableField) {
81+
this.handleNullableField = handleNullableField;
82+
}
83+
84+
public SchemaValidatorsConfig() {
6785
loadDefaultConfig();
6886
}
6987

0 commit comments

Comments
 (0)