这个 JSON 类型定义有什么问题?

What is wrong with this JSON Type Definition?

提问人:Henry 提问时间:11/10/2023 更新时间:11/10/2023 访问量:22

问:

我有一些代码来创建和验证应该表示标记联合的 JSON 架构。我希望 prop 值是 、 或 ,并且根据该值,对象将包含某些其他 props,例如 或:directionltgtetamountrange

import Ajv from "ajv/dist/jtd"
const ajv = new Ajv()

const Filter = {
    "discriminator": "direction",
    "mapping": {
        "gt": {
            "properties": {
                "amount": { "type": "number" }
            }
        },
        "lt": {
            "properties": {
                "amount": { "type": "number" }
            }
        },
        "et": {
            "properties": {
                "range": {
                    "properties": {
                        "top": { "type": "number" },
                        "bottom": { "type": "number" }
                    }
                }
            }
        }
    }
};

const validateFilter = ajv.compile(Filter);

结果:

Error: schema is invalid: 
data/discriminator must NOT have additional properties, 
data must have property 'ref', 
data must have property 'type', 
data must have property 'enum', 
data must have property 'elements', 
data must have property 'properties', 
data must have property 'optionalProperties', 
data/mapping/gt/properties/amount/type must NOT have additional properties, 
data/mapping/gt/properties/amount must have property 'ref', 
data/mapping/gt/properties/amount/type must be equal to one of the allowed values, 
data/mapping/gt/properties/amount must have property 'enum', 
data/mapping/gt/properties/amount must have property 'elements', 
data/mapping/gt/properties/amount must have property 'properties', 
data/mapping/gt/properties/amount must have property 'optionalProperties', 
data/mapping/gt/properties/amount must have property 'discriminator', 
data/mapping/gt/properties/amount must have property 'values', 
data/mapping/gt/properties/amount must match a schema in union, 
data/mapping/gt must have property 'optionalProperties', 
data/mapping/gt must match a schema in union, 
data must have property 'values', 
data must match a schema in union

我不明白为什么会这样。这里有一个沙盒可以搞砸: https://codesandbox.io/s/ajv-errors-54rdmx

JSON 类型 架构 AJV

评论

0赞 Jeremy Fiel 11/15/2023
您能否共享您正在尝试验证的示例数据实例?

答: 暂无答案