JSON Schema if-then条件验证失效问题求助
JSON Schema条件验证失效问题修复
问题根源
你的if-then条件逻辑判断路径完全错误:question是包含key和type的对象,但你直接用const: "rating"匹配整个question对象——这永远不会触发条件,因为对象不可能等于字符串"rating"。正确的判断应该是检查question.type字段的值。
修正后的Schema代码
{ "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "identifierKey": { "type": "string", "pattern": "^[a-z][a-z0-9]*(_[a-z0-9]+)*$", "minLength": 2 }, "uuid": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", "minLength": 36, "maxLength": 36 }, "mail_identifierKey": { "pattern": "^(?:(?!.*?[.]{2})[a-zA-Z0-9](?:[a-zA-Z0-9.+!%-]{1,64}|)|\"[a-zA-Z0-9.+!% -]{1,64}\")@[a-zA-Z0-9][a-zA-Z0-9.-]+(.[a-z]{2,}|.[0-9]{1,})$" }, "question_type": { "enum": ["barcode", "name", "nps", "single_choice", "text", "date", "number", "id", "mail_address", "multiple_choice", "phone_number", "zip_code", "rating"] }, "question": { "type": "object", "additionalProperties": false, "properties": { "key": {"$ref": "#/definitions/identifierKey"}, "type": {"$ref": "#/definitions/question_type"} }, "required": ["key", "type"] }, "answers": { "type": "array", "items": { "type": "object", "properties": { "question": {"$ref": "#/definitions/question"}, "status": {"type": "string"} }, "allOf": [ { "if": { "properties": { "question": { "properties": { "type": {"const": "rating"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "number", "minimum": 1, "maximum": 5} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "zip_code"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string", "$ref": "#/definitions/identifierKey", "minLength": 4, "maxLength": 10} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "phone_number"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string", "minLength": 8, "maxLength": 15} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "multiple_choice"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "array", "minItems": 1, "uniqueItems": true, "contains": {"type": "string"}} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "single_choice"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string", "$ref": "#/definitions/identifierKey"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "mail_address"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string", "$ref": "#/definitions/mail_identifierKey"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "id"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string", "$ref": "#/definitions/uuid"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "number"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "number"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "text"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "date"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string", "format": "date"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "nps"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "number", "minimum": 1, "maximum": 10} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "name"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "string"} }, "required": ["value"] } }, { "if": { "properties": { "question": { "properties": { "type": {"const": "barcode"} }, "required": ["type"] } } }, "then": { "properties": { "value": {"type": "number"} }, "required": ["value"] } } ] } } }, "properties": { "answers": {"$ref": "#/definitions/answers"} } }
额外优化说明
- 每个
then块新增required: ["value"],确保匹配问题类型时必须提供对应值,避免空值漏洞 - 格式化Schema代码为多行结构,大幅提升可读性(原单行压缩格式极难排查问题)
验证测试
使用你提供的待验证数据:
{"$schema": "./blank.json", "answers":[{"status": "hello1", "question": {"key": "hello", "type": "rating"}, "value": 1234}]}
现在会触发rating类型的条件验证,因value=1234超出1-5的范围,验证会失败,符合预期。
内容的提问来源于stack exchange,提问作者Vivek Raven
相关产品推荐
相关产品推荐

