编写坐标系统校验JSON Schema出现重复键等报错如何解决?
存在的问题
- JSON语法不允许同一个对象内存在重复键,你定义了3个
if和3个then重复键,后定义的内容会直接覆盖之前的内容,多个条件分支需要放在allOf数组中,每个分支作为独立的if/then子对象。 - 结构定义错误:你将根节点定义为固定长度3的数组,但实际要校验的是单个坐标对象,包含
system字段和对应坐标参数,不需要外层数组规则。 - 枚举值不匹配:
system字段的枚举值写的是Cylindric,但后续条件判断用的是Cylindrical,二者不一致会导致柱坐标分支无法触发。 - 规则参数错误:
- 角度的
minimum、maximum值写成了字符串格式,需要改为数字类型,去掉两侧引号 - 球坐标、柱坐标的必填字段错误写为
x/y/z,应该对应各自的参数名
- 角度的
- 语法不完整:代码最后多了一个多余的闭合大括号,导致JSON解析时报文件结束异常。
修正后的完整JSON Schema
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "system": { "enum": ["Cartesian", "Cylindrical", "Spherical"], "description": "The type of the coordinate system" } }, "required": ["system"], "allOf": [ { "if": { "properties": { "system": { "const": "Cartesian" } } }, "then": { "properties": { "x": { "type": "number" }, "y": { "type": "number" }, "z": { "type": "number" } }, "required": ["x", "y", "z"] } }, { "if": { "properties": { "system": { "const": "Spherical" } } }, "then": { "properties": { "r": { "type": "number" }, "theta": { "type": "number", "minimum": 0, "maximum": 360 }, "phi": { "type": "number", "minimum": 0, "maximum": 180 } }, "required": ["r", "theta", "phi"] } }, { "if": { "properties": { "system": { "const": "Cylindrical" } } }, "then": { "properties": { "r": { "type": "number" }, "theta": { "type": "number", "minimum": 0, "maximum": 360 }, "z": { "type": "number" } }, "required": ["r", "theta", "z"] } } ] }
内容的提问来源于stack exchange,提问作者Spearitch502
相关产品推荐
相关产品推荐

