【发布时间】:2020-07-28 03:18:03
【问题描述】:
我有两种类型的数据集,即 csv 或固定长度数据。在 csv 数据中,字段列表只是一个名称列表, 而在固定长度数据中,每个字段由 fieldName 和 fieldLength 指定。 我需要一个 json 模式来验证这两种情况,但在尝试了几种解决方案之后,包括 these,我不确定可以做到。或者我对 JSON 模式的理解还不够完善。
json:
{
"dataset": "csv data",
"dataFormat": "csv",
"fieldList": [{
"fieldName": "id"
},
{
"fieldName": "num"
},
{
"fieldName": "struct"
}
]
}
{
"dataset": "fixed length",
"dataFormat": "fixed",
"fieldList": [{
"fieldName": "id",
"fieldLength": 13
},
{
"fieldName": "num"
},
{
"fieldName": "struct"
}
]
}
JSON 架构:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"dataset",
"dataFormat",
"fieldList"
],
"properties": {
"dataset": {
"$id": "#/properties/dataset",
"type": "string"
},
"dataFormat": {
"$id": "#/properties/dataFormat",
"type": "string",
"enum": [
"csv",
"fixed"
]
},
"fieldList": {
"$id": "#/properties/fieldList",
"type": "array",
"additionalItems": true,
"items": {
"$id": "#/properties/fieldList/items",
"type": "object",
"additionalProperties": true,
"required": [
"fieldName"
],
"if": {
"properties": {
"dataFormat": {
"const": "fixed"
}
}
},
"then": {"items":{
"required": [
"fieldLength"
]}
},
"properties": {
"fieldName": {
"$id": "#/properties/fieldList/items/properties/fieldName",
"type": "string"
},
"fieldLength": {
"$id": "#/properties/fieldList/items/properties/fieldLength",
"type": "integer"
}
}
}
}
}
}
两个文档都经过了肯定验证,即使在“固定”类型中只有第一项包含所需的字段长度。 有什么建议吗?
【问题讨论】:
标签: json schema jsonschema