我有三个 json-schema 定义。 客户、地址和联系方式。
客户端.json
{
"$id": "client.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"$id": "/properties/name",
"type": "string"
},
"id": {
"$id": "/properties/id",
"type": "integer"
},
"contact": {
"$ref": "contact.json"
},
"address": {
"$ref": "address.json"
}
}
}
地址.json
{
"$id": "address.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"$id": "/items",
"type": "object",
"properties": {
"addressId": {
"$id": "/items/properties/addressId",
"type": "integer"
},
"addressName": {
"$id": "/items/properties/addressName",
"type": "string"
}
}
}
}
联系人.json
{
"$id": "contact.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"$id": "/items",
"type": "object",
"properties": {
"contactId": {
"$id": "/items/properties/contactId",
"type": "integer"
},
"contactName": {
"$id": "/items/properties/contactName",
"type": "string"
},
"address": {
"$ref": "address.json"
}
}
}
}
待验证对象
var client = {
"name": "test",
"id": 12,
"contact": [
{
"contactId": 12212,
"contactName": "jon",
"address": [
{
"addressId": 64,
"addressName": "pi"
}
]
}
],
"address": [
{"addressId": 4242,
"addressName": "doe"}
]
};
“client.json”中的 $ref 工作正常,但在从“contact.json”中引用“address.json”时出现错误。 我在“additionalItems”中使用 $refs 时没有遇到任何错误,但无法根据 $ref 指向的模式进行验证。
我想知道如何使用数组类型模式定义中的 $ref。 另外,我使用 AJV 进行模式验证。
编辑 1: AJV 设置
var Ajv = require('ajv');
var ajv = new Ajv({
$data: true,
allErrors: true,
useDefaults: true,
coerceTypes: true,
});
ajv.addSchema(client);
ajv.addSchema(contact);
ajv.addSchema(address);
let valid = ajv.validate('client.json', payload);
if(!valid){
console.log(ajv.errors);
}
最佳答案
我确定问题是 $id
更改了 $ref
的解析范围。我猜测 $ref
解析是通过在文件系统上查找文件来实现的。假设您的三个架构在 file:///path/to/schema
中可用。
- 您开始处理
file:///path/to/schema/client.json
模式。 - 您遇到了引用
contact.json
。这是相对 URI,因此您需要确定它相对的 URI 才能解析它。 - 您回溯架构并找到最近的
$id
,其值为client.json
。 - 这是一个相对 URI,没有更多的
$id
,所以文件的路径,file:///path/to/schema/client.json
被使用。 - 您现在可以根据
file:///path/to/schema/client.json
解析client.json
并获取file:///path/to/schema/client.json
. - 您现在可以根据
file:///path/to/schema/client.json
解析contact.json
并获取file://path/到/schema/contact.json
.
这就是它开始变得奇怪的地方。
- 您检索
file:///path/to/schema/contact.json
架构。 - 您遇到了引用
address.json
。这是一个相对 URI,因此您需要确定它相对的 URI 才能解析它。 - 您回溯架构并找到最近的
$id
,其值为/items
。 - 这是一个相对 URI,因此您不断回溯并找到
contact.json
。 - 这是一个相对 URI,没有更多的
$id
,所以文件的路径file:///path/to/schema/contact.json
被使用。 - 现在您可以根据
file:///path/to/schema/contact.json
解析/items
并获得file:///items
。 - 现在您可以针对
file:///items
解析address.json
并获得file:///address.json
。 - 您尝试检索
file:///address.json
架构,但它不存在。
因为 $id
改变了 $ref
的解析范围,所以强烈建议不要像在模式中那样给所有东西一个 $id
.此功能适用于将多个小模式组合成一个这样的用例。除了在文档的根目录之外,你真的不应该使用它,除非你有充分的理由并理解其中的含义。
关于json - $ref 不适用于数组类型 json 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473116/