【发布时间】:2021-04-18 08:01:13
【问题描述】:
我正在尝试使用另一个 JSON 架构来验证 JSON 架构。
要验证的 JSON Schema 示例:https://jsonschema.net/home
用于验证上述架构的验证架构参考:https://github.com/ajv-validator/ajv/blob/master/lib/refs/json-schema-draft-07.json
我有一个要求,property 只能是原始类型,即string, number, integer, boolean。
由于 JSON 模式的根应该有 type 作为 object 并且它里面的所有 properties 都将有 type 作为原始类型,我不知道我应该如何定义 type 定义来验证 type在根级别作为object 而type 在properties 内部作为原始类型。
示例 JSON:
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"description": "The root schema comprises the entire JSON document.",
"default": {},
"examples": [
{
"name": "A green door"
}
],
"required": [
"name"
],
"properties": {
"name": {
"$id": "#/properties/name",
"type": "string",
"title": "The name schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"A green door"
]
}
},
"additionalProperties": true
}
验证type的验证JSON:
definitions: {
simpleTypes: {
enum: ['array', 'boolean', 'integer', 'null', 'number', 'object', 'string'],
}
},
properties: {
type: {
anyOf: [
{ $ref: '#/definitions/simpleTypes' },
],
},
}
从上面的simpleTypes -> enum,如果我删除object,我的JSON 就会失效。
我可以以任何方式为根 type 定义 enum 与 properties 中的 type 不同吗?
【问题讨论】:
标签: json jsonschema json-schema-validator