多态数组对象JSON Schema验证异常:未检测必填字段缺失
问题:JSON Schema多态车辆数组验证失效
使用com.networknt json-schema-validator(版本1.0.79)验证包含多态类型车辆数组的JSON数据时,发现当bicycle类型的对象缺失必填的petName字段时,未触发预期的验证错误。
使用的依赖
<dependency> <groupId>com.networknt</groupId> <artifactId>json-schema-validator</artifactId> <version>1.0.79</version> <exclusions> <exclusion> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </exclusion> </exclusions> </dependency>
测试JSON数据
{ "firstname" : "Jack", "lastname" : "Sparrow", "age" : "42", "vehicles" : [ { "type" : "bicycle", "company" : "Hercules", "isElectric" : false }, { "type" : "motorcycle", "company" : "Hero", "hp" : 340, "isRich" : true } ] }
原JSON Schema
{ "$schema": "http://json-schema.org/draft-06/schema#", "type": "object", "required": [ "firstname", "lastname", "age", "vehicles" ], "properties": { "firstname": { "type": "string", "minLength": 2, "maxLength": 10 }, "lastname": { "type": "string" }, "age": { "type": [ "integer", "string" ] }, "vehicles": { "type": "array", "items": { "type": "object", "required": [ "type" ], "properties": { "company": { "type": "string", "minLength": 2, "maxLength": 10 }, "isElectric": { "type": "boolean" }, "hp": { "type": [ "string", "integer" ] }, "isRich": { "type": "boolean" }, "type": { "type": "string", "enum": [ "bicycle", "motorcycle" ] } }, "additionalProperties": false }, "anyOf": [ { "properties": { "type": { "const": "bicycle" }, "vehicles": { "items": { "required": [ "company", "isElectric", "petName" ] } } } }, { "properties": { "type": { "const": "motorcycle" }, "vehicles": { "items": { "required": [ "company", "hp", "isRich" ] } } } } ] }, "additionalProperties": false } }
问题原因分析
原Schema存在两个核心错误:
- 层级错误:将
anyOf放在了vehicles数组的层级下,但type是数组中每个车辆对象的属性,不是数组本身的属性,导致校验规则无法匹配到数组内的元素。 - 规则错位:
anyOf分支内错误地再次定义vehicles属性,完全偏离了针对单个车辆对象校验的逻辑,相当于试图给整个数组重复定义规则,而非针对数组中的每个元素。
修正后的JSON Schema
正确的做法是在数组的items中使用oneOf(或anyOf),针对每个车辆对象根据type字段区分校验规则:
{ "$schema": "http://json-schema.org/draft-06/schema#", "type": "object", "required": [ "firstname", "lastname", "age", "vehicles" ], "properties": { "firstname": { "type": "string", "minLength": 2, "maxLength": 10 }, "lastname": { "type": "string" }, "age": { "type": [ "integer", "string" ] }, "vehicles": { "type": "array", "items": { "type": "object", "required": ["type"], "properties": { "type": { "type": "string", "enum": ["bicycle", "motorcycle"] }, "company": { "type": "string", "minLength": 2, "maxLength": 10 } }, "additionalProperties": false, "oneOf": [ { "properties": { "type": {"const": "bicycle"}, "isElectric": {"type": "boolean"}, "petName": {"type": "string"} }, "required": ["company", "isElectric", "petName"] }, { "properties": { "type": {"const": "motorcycle"}, "hp": {"type": ["string", "integer"]}, "isRich": {"type": "boolean"} }, "required": ["company", "hp", "isRich"] } ] } }, "additionalProperties": false } }
修正说明
- 将
oneOf移动到items内部,确保每个数组元素(车辆对象)都会被校验。 - 每个
oneOf分支针对特定type的车辆定义专属的必填字段和属性规则,确保bicycle必须包含petName,motorcycle必须包含hp和isRich。 - 公共属性(如
company)提取到外层properties中,避免重复定义。
内容的提问来源于stack exchange,提问作者pjsagar
相关产品推荐
相关产品推荐

