【问题标题】:no schema with key or ref with AJV when validating schema property验证架构属性时,没有带有键的架构或带有 AJV 的引用
【发布时间】:2021-06-06 13:57:44
【问题描述】:

我正在使用ajv,我只想验证一个特定的属性。我的架构是:

{
    properties:{
        dog:{type:"string"}
    }
}

我想知道foo 是否是一只有效的狗。

我愿意:

const ajv = new AjvModule.default({ removeAdditional: "all", coerceTypes: true});
ajv.addSchema(require("./schemas/the_above_schema.json"))
ajv.validate(`#/dog`, 'foo');

但我得到了错误:Error: no schema with key or ref "#/dog"

我也试过:`ajv.addSchema(require("./schemas/the_above_schema.json"), 'dog_schema')

ajv.validate(dog_schema#/dog`, 'foo')

我错过了什么?

【问题讨论】:

    标签: json jsonschema ajv


    【解决方案1】:

    好的,我找到了答案。

    要仅验证一个特定属性,您可以执行以下操作:

    ajv.addSchema(schema, schema_name)
    const valid = ajv.validate(`${schema_name}#/properties/${property_name}`, value);
    
    

    您也可以使用definitions 代替properties

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2020-07-13
      • 2021-01-31
      相关资源
      最近更新 更多