【问题标题】:How to use ajv with dependent json-schemas?如何将ajv与依赖的json模式一起使用?
【发布时间】:2021-04-19 21:30:44
【问题描述】:

我正在尝试使 ajv 与两个 json 模式一起工作,一个依赖于另一个。 这是我的模式的示例(简化):

types.json

{
    "$schema":"http://json-schema.org/draft-04/schema#",
    "$id": "http://a.site.org/schemas/types.json",
    "type": "object",
    "definitions": {
        "gender":{"enum": ["male", "female"]}
    },
    "properties":{
        "gender":{"$ref": "#/definitions/gender"}
    }
}

definitions.json

{
"$schema":"http://json-schema.org/draft-04/schema#",
"$id": "http://a.site.org/schemas/definitions.json",
"definitions": {
    "person":{
        "type":"object",
        "properties": {
            "username":{"type":"string"},
            "password": {"type":"string"},
            "name":{"type":"string"},
            "surname":{"type":"string"},
            "sex":{"$ref": "types.json#/properties/gender"},
            "email":{"type":"string", "format":"email"}
        },
        "required":["name", "surname", "sex", "email"]
    }
},
"type":"object",
"properties": {
    "person": {"$ref": "#/definitions/person"}
    }
}

从节点,我像这样使用ajv:

import Ajv, {JSONSchemaType, DefinedError} from "ajv"
import {Gender} from "./types"
import {Person} from "./definitions";
const ajv = new Ajv()

const types : JSONSchemaType<Gender>= require("../types.json");
const PersonSchema : JSONSchemaType<Person> = require('../definitions.json').definitions.person;

const isPerson = ajv.addSchema(types).compile(PersonSchema);
const riccardo:Person={name: "Riccardo", surname: "surname", sex: "male", email: "email@gmail.com"};
const personValid = isPerson(riccardo);
if (!personValid) console.log(isPerson.errors)

我得到的错误是:

错误:没有带有键或引用的架构“http://json-schema.org/draft-04/schema#”

更新: 如果我从 types.json 中删除“$schema ...”,我得到的错误是:

MissingRefError: can't resolve reference types.json#/definitions/gender from id #

我使用了这个参考:https://github.com/ajv-validator/ajv/blob/master/docs/validation.md#modular-schemas

有谁知道我做错了什么?

【问题讨论】:

    标签: json jsonschema ajv


    【解决方案1】:

    根据以下答案的建议,问题出在草稿版本上。 在这里,我发布了我是如何使用更新的 json-schema 草案解决的。

    文件现在以这种方式更改:

    types.json

    {
        "$schema":"http://json-schema.org/draft-06/schema#",
        "$id": "http://asite.org/schemas/types.json",
        "type": "object",
        "definitions": {
            "gender":{"enum": ["male", "female"]}
        },
        "properties":{
            "gender":{"$ref": "#/definitions/gender"}
        }
    }
    

    definition.json:

    {
        "$id": "http://asite.org/schemas/definitions.json",
        "$schema":"http://json-schema.org/draft-06/schema#",
        "definitions": {
            "person":{
                "$schema":"http://json-schema.org/draft-06/schema#",
                "$id": "http://asite.org/schemas/person.json",
                "type":"object",
                "properties": {
                    "username":{"type":"string"},
                    "password": {"type":"string"},
                    "name":{"type":"string"},
                    "surname":{"type":"string"},
                    "sex":{"$ref": "types.json#/definitions/gender"},
                    "email":{"type":"string", "format":"email"}
                },
                "required":["name", "surname", "sex", "email"]
            }
        },
        "type":"object",
        "properties": {
            "person": {"$ref": "#/definitions/person"}
        }
    }
    
    

    这样使用:

    import Ajv, {JSONSchemaType, DefinedError} from "ajv"
    import {Gender} from "./types"
    import {Person} from "./definitions";
    const ajv = new Ajv();
    ajv.addMetaSchema(require("../node_modules/ajv/lib/refs/json-schema-draft-06.json"))
    
    const types = require("../types.json");
    const PersonSchema : JSONSchemaType<Person> = require('../definitions.json').definitions.person;
    const isPerson = ajv.addSchema(types).compile(PersonSchema);
    const riccardo:Person={name: "Riccardo", surname: "surname", sex: "male", email: "email@gmail.com"};
    
    const personValid = isPerson(riccardo);
    if (!personValid) console.log(isPerson.errors)
    

    现在出现了不同的错误,但是删除它可以工作的“电子邮件”属性,所以这是另一回事:

    错误:路径“#/properties/email”的架构中忽略了未知格式“email”

    【讨论】:

      【解决方案2】:

      从版本 7 开始,已删除对 draft-04 的支持。 如果要使用draft-04,需要使用版本6的ajv。

      【讨论】:

      • 您能否提供更多关于如何以我的示例进行操作的详细信息?
      • 该错误是因为您正在尝试使用 JSON Schema 的 Draft-04,ajv 不再支持。如果要继续使用draft-04,需要降级到ajv版本6。
      • 好的,我尝试在我的shema中使用draft-06,现在我有一个不同的错误,你知道如何解决吗?
      • 您现在问的是另一个问题。老问题实际上对人们来说是有价值和有用的。我建议您将您的新问题作为新问题发布,然后我可以进一步帮助您=]
      • 好的,我再次编辑了问题并将之前的内容发布为答案,以便更清楚我为使其工作所做的更改
      猜你喜欢
      • 2018-09-25
      • 1970-01-01
      • 2019-06-05
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多