Json 用于验证的架构模板
Json Schema template for valiadtion
我不熟悉定义 JSON 架构并根据架构验证 json。
这是一个示例 json,我想为其定义一个 json 模式模板以进行验证:
{
"version": "1.0",
"config": {
"globalConfig": {
“ClientNames”: [
“client1”, “client2”, “client3”
]
},
“ClientConfigs”: [
{
“ClientName”: “client1”,
“property1”: “some value”,
“property2”: “some value”
},
{
“ClientName”: “client2”,
“property1”: “some value”,
“property2”: “some value”
},
{
“ClientName”: “client3”,
“property1”: “some value”,
“property2”: “some value”
}
]
}
据我所知,“ClientConfigs”将是一个对象数组(比方说 ClientConfig),其中包含 clientName、属性1 和 属性2。这是我认为模式想要的:
{
"$schema": "http://json-schema.org/draft-01/schema#",
"title": "ClientConfig",
"type": "object",
"description": "Some configuration",
"properties": {
"version": {
"type": "string"
},
"config": {
"$ref": "#/definitions/config"
}
},
"definitions": {
"config": {
"type": "object",
"properties": {
"globalConfig": {
"type": "object",
"description": "Global config for all clients",
"properties": {
"ClientNames": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
}
}
}
},
"ClientConfigs": {
"type": "array",
"description": "List of configs for different clients",
"minItems": 1,
"items": {
"$ref": "#/definitions/ClientConfig"
}
}
}
},
"ClientConfig": {
"type": "object",
"properties": {
"ClientName": {
"type": "string"
},
"property1": {
"type": "string"
},
"property2": {
"type": "string"
}
}
}
}
}
我想用 jsonschema 验证 2 件事:
- ClientConfigs 数组每个元素中的 ClientName 是“ClientNames”中的值之一,即“ClientConfigs”数组中的单个 ClientConfig 应仅包含 属性“ClientNames”中定义的客户端名称。
- “ClientNames”中出现的每个 clientName 都应定义为“ClientConfigs”数组中的一个元素。更准确地说,ClientConfig 是为“ClientNames”属性.
中存在的每个 clientName 定义的
根据我的要求,这是一个无效的示例:
{
"version": "1.0",
"config": {
"globalConfig": {
“ClientNames”: [
“client1”, “client2”, “client3”
]
},
“ClientConfigs”: [
{
“ClientName”: “client4”,
“property1”: “some value”,
“property2”: “some value”
}
]
}
无效,因为:
- 它没有为client1、client2和client3定义ClientConfig。
- 它为“ClientNames”中不存在的 client4 定义了 ClientConfig。
是否可以使用 json 模式模板进行此类验证?如果是,如何验证?
您不能在 JSON 架构中引用实例数据。这被认为是业务逻辑,超出了 JSON 模式的范围。
我不熟悉定义 JSON 架构并根据架构验证 json。
这是一个示例 json,我想为其定义一个 json 模式模板以进行验证:
{
"version": "1.0",
"config": {
"globalConfig": {
“ClientNames”: [
“client1”, “client2”, “client3”
]
},
“ClientConfigs”: [
{
“ClientName”: “client1”,
“property1”: “some value”,
“property2”: “some value”
},
{
“ClientName”: “client2”,
“property1”: “some value”,
“property2”: “some value”
},
{
“ClientName”: “client3”,
“property1”: “some value”,
“property2”: “some value”
}
]
}
据我所知,“ClientConfigs”将是一个对象数组(比方说 ClientConfig),其中包含 clientName、属性1 和 属性2。这是我认为模式想要的:
{
"$schema": "http://json-schema.org/draft-01/schema#",
"title": "ClientConfig",
"type": "object",
"description": "Some configuration",
"properties": {
"version": {
"type": "string"
},
"config": {
"$ref": "#/definitions/config"
}
},
"definitions": {
"config": {
"type": "object",
"properties": {
"globalConfig": {
"type": "object",
"description": "Global config for all clients",
"properties": {
"ClientNames": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
}
}
}
},
"ClientConfigs": {
"type": "array",
"description": "List of configs for different clients",
"minItems": 1,
"items": {
"$ref": "#/definitions/ClientConfig"
}
}
}
},
"ClientConfig": {
"type": "object",
"properties": {
"ClientName": {
"type": "string"
},
"property1": {
"type": "string"
},
"property2": {
"type": "string"
}
}
}
}
}
我想用 jsonschema 验证 2 件事:
- ClientConfigs 数组每个元素中的 ClientName 是“ClientNames”中的值之一,即“ClientConfigs”数组中的单个 ClientConfig 应仅包含 属性“ClientNames”中定义的客户端名称。
- “ClientNames”中出现的每个 clientName 都应定义为“ClientConfigs”数组中的一个元素。更准确地说,ClientConfig 是为“ClientNames”属性. 中存在的每个 clientName 定义的
根据我的要求,这是一个无效的示例:
{
"version": "1.0",
"config": {
"globalConfig": {
“ClientNames”: [
“client1”, “client2”, “client3”
]
},
“ClientConfigs”: [
{
“ClientName”: “client4”,
“property1”: “some value”,
“property2”: “some value”
}
]
}
无效,因为:
- 它没有为client1、client2和client3定义ClientConfig。
- 它为“ClientNames”中不存在的 client4 定义了 ClientConfig。
是否可以使用 json 模式模板进行此类验证?如果是,如何验证?
您不能在 JSON 架构中引用实例数据。这被认为是业务逻辑,超出了 JSON 模式的范围。