0

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")
Pengo
  • 470
  • 1
  • 5
  • 17
  • 1
    You can do this with the implication pattern https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required/38781027#38781027 – Jason Desrosiers Aug 17 '18 at 04:33
  • Possible duplicate of [jsonSchema attribute conditionally required](https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required) – Relequestual Aug 17 '18 at 08:31
  • JasonDesrosiers and Relequestual thank you for linking to the other question. I don't think it solves my problem. (I'm sorry I'm having trouble explaining it). If remitImbFlag=Y then remitStidInd needs to have certain values. If remitImbFlag=N then remitStidInd is still be included but has a value that would be considered invalid. But I don't want to fail validation if remitStidInd is invalid when remitImbFlag=N – Pengo Aug 17 '18 at 20:05

0 Answers0