I have two type of datasets, with csv or fixed length data. In csv data the fieldlist is just a list of names, while in fixed length data each field is specified by fieldName and fieldLength. I need a json schema to validate both cases, but after trying several solutions, including these, I am not sure it can be done. Or maybe my understanding of JSON schema is still far from perfect.
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:
{
"$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"
}
}
}
}
}
}
Both documents are positively validated, even if in the "fixed" type only the first item includes the required fieldLength. Any suggestion ?