I am being asked to validate a json file created by another team's program. I'm having trouble conditionally validating some of the attributes of one of the objects. Here's a snippet of the json file with the object and it's attributes:
"productConfig": {
"aggregateFlag": "N",
"aggregateCode": "",
"companionFlag": "N",
"insertMatchCodeFlag": "N",
"pomFlag": "N",
"ncoaFlag": "N",
"cleanseAddressFlag": "N",
"frequency": "D",
"resolutionDpi": "0300",
"priority": "35",
"stidInd": "I",
"stidAuto": "270",
"remitImbFlag": "Y",
"remitStidInd": "I",
"remitStid": "030",
"keylineAppendFlag": "N",
"imbPlacementInd": "U",
"keylinePlacementInd": "U",
"hriPlacement": "LM",
"inputScanPlacement": "R3",
"outputScanPlacement": "L"
},
If remitImbFlag
is "Y" I need to validate remitStidInd
and remitStid
.
When remitImbFlag
is "N" I don't care what values are in remitStidInd
and remitStid
. However, remitStidInd
and remitStid
are still in the json file and set to empty strings. This causes my validation to fail when I don't want it to.
How do I have the validation schema ignore remitStidInd
and remitStid
when remitImbFlag
is "N"?
I tried setting up dependencies:
"remitImbFlag": {
"type": "string",
"enum": ["Y", "N"],
"options": {
"dependencies":[
{"id":"remitStidInd","value":"Y"},
{"id":"remitStid","value":"Y"}
]
}
},
"remitStidInd": {
"type": "string",
"enum": ["I", "N"]
},
"remitStid": {
"type": "string",
"enum": ["030", "032"]
},
But because remitStidInd
and remitStid
are still in the file they are being evaluated and failing.
...
"remitImbFlag": "N",
"remitStidInd": "",
"remitStid": "",
...
I am using Draft4Validator from the python library jsonschema and doing "lazy" validation to find all the errors in a file.
from jsonschema import Draft4Validator
...
v = Draft4Validator(schema)
error_cnt = 0
errors = list(v.iter_errors(data))
if not errors:
print("File passed validation.")
else:
print("File failed validation, found {} error(s):".format(len(errors)))
for error in sorted(errors, key=str):
error_cnt += 1
print("{}. Error at {}: {}".format(error_cnt, error.absolute_path.pop(), error.message))
print("\n")