|
24 | 24 | import java.util.Set;
|
25 | 25 |
|
26 | 26 | public class TypeValidator extends BaseJsonValidator implements JsonValidator {
|
| 27 | + private static final String TYPE = "type"; |
| 28 | + private static final String ENUM = "enum"; |
| 29 | + |
27 | 30 | private static final Logger logger = LoggerFactory.getLogger(TypeValidator.class);
|
28 | 31 |
|
29 | 32 | private JsonType schemaType;
|
| 33 | + private JsonNode parentSchemaNode; |
30 | 34 | private UnionTypeValidator unionTypeValidator;
|
31 | 35 |
|
32 | 36 | public TypeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
|
33 | 37 | super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.TYPE, validationContext);
|
34 | 38 | schemaType = TypeFactory.getSchemaNodeType(schemaNode);
|
| 39 | + parentSchemaNode = parentSchema.getSchemaNode(); |
35 | 40 |
|
36 | 41 | if (schemaType == JsonType.UNION) {
|
37 | 42 | unionTypeValidator = new UnionTypeValidator(schemaPath, schemaNode, parentSchema, validationContext);
|
@@ -62,6 +67,11 @@ public boolean equalsToSchemaType(JsonNode node) {
|
62 | 67 | return true;
|
63 | 68 | }
|
64 | 69 | }
|
| 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 | + } |
65 | 75 | if(config.isTypeLoose()) {
|
66 | 76 | if (nodeType == JsonType.STRING) {
|
67 | 77 | if(schemaType == JsonType.INTEGER) {
|
@@ -215,4 +225,13 @@ public static boolean isNumber(JsonNode node, boolean isTypeLoose) {
|
215 | 225 | }
|
216 | 226 | return false;
|
217 | 227 | }
|
| 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 | + } |
218 | 237 | }
|
0 commit comments