问题描述
例如,文件系统的模式,目录包含文件列表。该模式包括文件的规范,接下来是子类型图像和另一个文本。
底部是主目录模式。目录具有属性内容,该属性内容是应该是文件的子类型的项目数组。
基本上我正在寻找的方法是告诉验证器从正在验证的json对象中的属性中查找$ ref的值。
示例json:
{
name:A directory,
content:[
{
fileType:http://xyz/fs-schema.json#definitions/ image,
name:an-image.png,
宽度:1024,
身高:800
}
{
fileType:http://xyz/fs-schema.json#definitions/text,
name:readme.txt,
lineCount:101
}
{
fileType:http://xyz/extended-fs-schema-video.json,
name:demo.mp4,
hd:true
}
]
}
伪架构请注意,图像和文本定义包含在同一架构中,但它们可能在其他地方定义
{
id:http://xyz/fs-schema.json,
definitions:{
file:{
type:object,
properties:{
name:{type:string},
fileType :{
type:string,
format:uri
}
}
},
image: {
allOf:[
{$ ref:#definitions / file},
{
properties:{
width: {type:integer},
height:{type:integer}
}
}
]
},
text:{
allOf:[
{$ ref:#definitions / file},
{properties:{lineCount:{输入:整数}}}
]
}
},
类型:对象,
属性:{
name:{type:string},
content:{
type:array,
items:{
allOf:[
{$ ref:#definitions / file},
{* $ refFromProperty*:fileType} //神奇的东西
]
}
}
}
}
仅JSON Schema的验证部分无法做到这一点 - 它代表一个固定的结构。您想要的是在验证 -time时解析/引用模式。
但是,您可以使用JSON Hyper-Schema和 rel =describeby
link:
{
title :目录条目,
类型:对象,
属性:{
fileType:{type:string,format: uri}
},
链接:[{
:describeby,
href:{+ fileType}
} ]
}
所以这里,它取值 fileType
并使用它来计算具有关系的链接describeby - 这意味着此位置的架构也描述了当前数据。
UPDATE :库已添加此功能
For example a schema for a file system, directory contains a list of files. The schema consists of the specification of file, next a sub type "image" and another one "text".
At the bottom there is the main directory schema. Directory has a property content which is an array of items that should be sub types of file.
Basically what I am looking for is a way to tell the validator to look up the value of a "$ref" from a property in the json object being validated.
Example json:
{
"name":"A directory",
"content":[
{
"fileType":"http://x.y.z/fs-schema.json#definitions/image",
"name":"an-image.png",
"width":1024,
"height":800
}
{
"fileType":"http://x.y.z/fs-schema.json#definitions/text",
"name":"readme.txt",
"lineCount":101
}
{
"fileType":"http://x.y.z/extended-fs-schema-video.json",
"name":"demo.mp4",
"hd":true
}
]
}
The "pseudo" Schema note that "image" and "text" definitions are included in the same schema but they might be defined elsewhere
{
"id": "http://x.y.z/fs-schema.json",
"definitions": {
"file": {
"type": "object",
"properties": {
"name": { "type": "string" },
"fileType": {
"type": "string",
"format": "uri"
}
}
},
"image": {
"allOf": [
{ "$ref": "#definitions/file" },
{
"properties": {
"width": { "type": "integer" },
"height": { "type": "integer"}
}
}
]
},
"text": {
"allOf": [
{ "$ref": "#definitions/file" },
{ "properties": { "lineCount": { "type": "integer"}}}
]
}
},
"type": "object",
"properties": {
"name": { "type": "string"},
"content": {
"type": "array",
"items": {
"allOf": [
{ "$ref": "#definitions/file" },
{ *"$refFromProperty"*: "fileType" } // the magic thing
]
}
}
}
}
The validation parts of JSON Schema alone cannot do this - it represents a fixed structure. What you want requires resolving/referencing schemas at validation-time.
However, you can express this using JSON Hyper-Schema, and a rel="describedby"
link:
{
"title": "Directory entry",
"type": "object",
"properties": {
"fileType": {"type": "string", "format": "uri"}
},
"links": [{
"rel": "describedby",
"href": "{+fileType}"
}]
}
So here, it takes the value from "fileType"
and uses it to calculate a link with relation "describedby" - which means "the schema at this location also describes the current data".
The problem is that most validators do not take any notice of any links (including "describedby" ones). You need to find a "hyper-validator" that does.
UPDATE: the tv4 library has added this as a feature
这篇关于如何告诉JSON模式验证器从属性值中选择模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!