Skip to content

Commit 730862b

Browse files
committed
Fix enum object validation error
1 parent d2a3a8d commit 730862b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424
import java.util.Set;
2525

2626
public class TypeValidator extends BaseJsonValidator implements JsonValidator {
27+
private static final String TYPE = "type";
28+
private static final String ENUM = "enum";
29+
2730
private static final Logger logger = LoggerFactory.getLogger(TypeValidator.class);
2831

2932
private JsonType schemaType;
33+
private JsonNode parentSchemaNode;
3034
private UnionTypeValidator unionTypeValidator;
3135

3236
public TypeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3337
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.TYPE, validationContext);
3438
schemaType = TypeFactory.getSchemaNodeType(schemaNode);
39+
parentSchemaNode = parentSchema.getSchemaNode();
3540

3641
if (schemaType == JsonType.UNION) {
3742
unionTypeValidator = new UnionTypeValidator(schemaPath, schemaNode, parentSchema, validationContext);
@@ -62,6 +67,11 @@ public boolean equalsToSchemaType(JsonNode node) {
6267
return true;
6368
}
6469
}
70+
// Skip the type validation when the schema is an enum object schema. Since the current type
71+
// of node itself can be used for type validation.
72+
if (isEnumObjectSchema(parentSchemaNode)) {
73+
return true;
74+
}
6575
if(config.isTypeLoose()) {
6676
if (nodeType == JsonType.STRING) {
6777
if(schemaType == JsonType.INTEGER) {
@@ -215,4 +225,13 @@ public static boolean isNumber(JsonNode node, boolean isTypeLoose) {
215225
}
216226
return false;
217227
}
228+
229+
private static boolean isEnumObjectSchema(JsonNode schemaNode) {
230+
JsonNode typeNode = schemaNode.get(TYPE);
231+
JsonNode enumNode = schemaNode.get(ENUM);
232+
if (typeNode != null && enumNode != null) {
233+
return TypeFactory.getSchemaNodeType(typeNode) == JsonType.OBJECT && enumNode.isArray();
234+
}
235+
return false;
236+
}
218237
}

0 commit comments

Comments
 (0)