如何将 ajv 与依赖 json-schemas 一起使用?
How to use ajv with dependent json-schemas?
我正在尝试让 ajv 与两个 json-schemas 一起工作,一个依赖于另一个。
这是我的模式的一个例子(简化):
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)
我得到的错误是:
Error: no schema with key or ref "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
有谁知道我做错了什么吗?
从版本 7 开始,已删除对 draft-04 的支持。
如果要使用draft-04,则需要使用ajv的版本6。
根据下面的答案建议,问题出在草稿上。
在这里,我 post 我是如何使用 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)
现在出现了不同的错误,但是删除“电子邮件”属性它起作用了,所以这是另一回事:
Error: unknown format "email" ignored in schema at path "#/properties/email"
我正在尝试让 ajv 与两个 json-schemas 一起工作,一个依赖于另一个。 这是我的模式的一个例子(简化):
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)
我得到的错误是:
Error: no schema with key or ref "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
有谁知道我做错了什么吗?
从版本 7 开始,已删除对 draft-04 的支持。 如果要使用draft-04,则需要使用ajv的版本6。
根据下面的答案建议,问题出在草稿上。 在这里,我 post 我是如何使用 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)
现在出现了不同的错误,但是删除“电子邮件”属性它起作用了,所以这是另一回事:
Error: unknown format "email" ignored in schema at path "#/properties/email"