Closed sajith closed 1 year ago
This appears to be the way to validate a given JSON blob against a schema with Python:
import json
import pathlib
import jsonschema
import jsonref # to expand `$ref`
blob = json.loads(pathlib.Path("port-data.json").read_text())
schema_file = pathlib.Path("port-schema.json")
schema = jsonref.loads(schema_file.read_text(), base_uri = schema_file.absolute().as_uri())
jsonschema.validate(blob, schema)
With some changes (correct $ref
etc), most of the test data files validate against the schemas we have, except for this little annoying vendor
thing:
Perhaps that could be removed? The draft Data Model 2.0.0 spec has no mention of a vendor
field.
Tthis can be commented out for now. Noticing the 'not' key, it was asked by FIU to be able to exclude certain vendor's equipments for security reasons.
Sadly JSON do not have "real" comment syntax. And it is the not
that makes validator unhappy:
jsonschema.exceptions.SchemaError: [{'type': 'array', 'items': [{'type': 'string'}]}] is not of type 'object', 'boolean'
I'm guessing it should be "not": { "anyOf" : [...] }"
-- that anyOf
should give not
the boolean it needs.
Totals | |
---|---|
Change from base Build 5882765968: | 0.0% |
Covered Lines: | 1029 |
Relevant Lines: | 1247 |
Yes, this should work: "not": { "anyOf" : [...] }"
This tool is cool. I guess it's relatively new. I googled around and didn't find such a tool two years ago!
Fixes #105. Changes:
$ref
values refer to the correct external files, and other things that surfaced when testing.python3 -m json.tool <infile> <outfile
. I just like mechanically formatted files. ;-)