请求有关JSON Schema验证的帮助,下面是JSON和Schema示例。我正在尝试弄清楚如何指定"ppd“模式规则,特别是"cfg”是字符串、字符串的映射,并且需要通过枚举定义进一步限制此映射中的键和值的条目,即"inputDateTimeFormat“的允许值是有效的日期时间格式,因此规则应该编码如果键是"inputDateTimeFormat”,则允许的值是与日期时间格式匹配的模式,类似地,如果键是“value映射”,则允许的值是模式匹配的k=v (示例如下)。
你能建议一种实现这一目标的方法吗?
JSON示例-
{
"sm": [
{
"mid": "id-1",
"ppd": [
{
"name": "cc-1",
"cfg": {
"columns": "v-1",
"valueMapping": "B=01;S=02"
}
},
{
"name": "cc-2",
"cfg": {
"columns": "v-2",
"inputDateTimeFormat": "ddMMMyyyy_HH:mm:ss.SSSSSS",
"outputDateTimeFormat": "yyyy-MM-dd'T'HH:mm:Ss.SSSZ"
}
},
{
"name": "cc-3",
"cfg": {
"columns": "v-3;v-4",
"markers": "d=01"
}
}
]
}
]
}JSON架构:
{
"type": "object",
"$schema": "http://json-schema.org/draft-06/schema",
"id": "source-mappings-schema",
"required": true,
"properties": {
"sm": {
"type": "array",
"id": "source-mappings-schema/sm",
"required": true,
"items": {
"type": "object",
"id": "source-mappings-schema/sm/0",
"required": true,
"properties": {
"mappingId": {
"type": "string",
"id": "source-mappings-schema/sm/0/mappingId",
"required": true
},
"ppd": {
"type": "array",
"id": "source-mappings-schema/sm/0/ppd",
"required": true,
"items": {
"type": "object",
"id": "source-mappings-schema/sm/0/ppd/0",
"required": true,
"properties": {
"name": {
"type": "string",
"id": "source-mappings-schema/sm/0/ppd/0/name",
"required": true
},
"cfg": {
"type": "array",
"id": "source-mappings-schema/sm/0/ppd/0/cfg",
"required": true,
"items": {
"type": "string"
}
}
}
}
}
}
}
}
}
}发布于 2017-11-29 22:39:06
首先,您的模式包含一些问题。
$schema标记错误,应该是
"$schema": "http://json-schema.org/draft-06/schema#",“required”属性应该是一个必需的属性名称数组(而不是bool),因此您需要在上面的级别应用此属性。
最后对cfg进行了验证。通过为'additionalProperties‘指定一个模式,您可以为所有未指定的键值提供验证规则(您说它是一个字符串映射,所以我将其设置为string,但您也可以在此处添加其他规则,如最大长度等)。对于您所知道的键,您可以使用相应的验证规则为每个键添加一个属性(我已经添加的规则演示了这个概念,并且需要进行调整以供您使用)。

"cfg": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"properties": {
"inputDateTimeFormat": {
"type": "string",
"format": "date-time"
},
"valuemapping": {
"type": "string",
"pattern": "[a-z]\\=[a-z]"
}
}
}https://stackoverflow.com/questions/47438855
复制相似问题