我有一个 JSON 模式文件:
{
"id":"http://schema.acme.com/user",
"$schema":"http://json-schema.org/draft-06/schema#",
"definitions":{
"user":{
"description":"The name the user has selected",
"type":"object",
"required":[
"username",
"premium"
],
"properties":{
"username":{
"type":"string",
"maxLength":10,
"minLength":1
},
"premium":{
"type":"boolean"
}
}
}
}
}
我想针对 json 对象验证这一点。所以我用这个模式创建了一个该类型的临时对象:
{
"id":"http://schema.acme.com/uName",
"$schema":"http://json-schema.org/draft-06/schema#",
"properties":{
"uName":{
"$ref":"smUserSchema.json#/definitions/user"
}
},
"required":[
"uName"
]
}
我有这个 JSON 数据文件:
{
"uName":{
"username":"Bob",
"premium":true
}
}
这里的目标是不要将我的临时对象嵌入到类类型的 JSON 模式中。(是的,我的一个问题是我正在尝试将 OO 技术应用于 JSON。这是真的,我这样做只是出于重用和继承的原因,可能有更好的方法。)
当我去验证这个我得到这个错误:
$ ajv -s uNameSchema.json -d validUser.json
schema uNameSchema.json is invalid
error: can't resolve reference smUserSchema.json#/definitions/user from id http://schema.acme.com/uName#
如何在另一个模式中包含 JSON 模式?
也可以看看: