scripts/validate_configs.py: Ensure schemas are validated as well
The quick'n'dirty method of validating a schema is via
`jsonschema.validate()`. From `help(jsonschema.validate)` we have:
> :func:`validate` will first verify that the provided schema is
> itself valid, since not doing so can lead to less obvious error
> messages and fail in less obvious or consistent ways.
>
> If you know you have a valid schema already, especially if you
> intend to validate multiple instances with the same schema, you
> likely would prefer using the `Validator.validate` method directly
> on a specific validator (e.g. ``Draft7Validator.validate``).
The implication is that explicitly instantiating a validator does not
validate the provided schema.
The implementation of validate_configs.py directly instantiates the
Draft7Validator() in order to provide a RefResolver. However, it does
not do the required work of validating the provided schema.
Rework the code instantiating the validator to validate the schema prior
to use.
Tested:
```
$ git revert -n $(git log --grep 'I2b8102e7d6047c8a7e624e182465065268fb8ad9' --format='%H') && ./scripts/validate_configs.py -v -k; git reset --hard
Traceback (most recent call last):
File "/home/andrew/src/openbmc.org/openbmc/entity-manager/origin/./scripts/validate_configs.py", line 187, in <module>
main()
File "/home/andrew/src/openbmc.org/openbmc/entity-manager/origin/./scripts/validate_configs.py", line 138, in main
spec.check_schema(schema)
File "/usr/lib/python3/dist-packages/jsonschema/validators.py", line 204, in check_schema
raise exceptions.SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: ['The schema for an entity manager configuration file. An entity ', 'mananger configuration file can consist of a single object, or an ', 'array of objects. Objects must be of type EMConfig.'] is not of type 'string'
Failed validating 'type' in metaschema['properties']['description']:
{'type': 'string'}
On schema['description']:
['The schema for an entity manager configuration file. An entity ',
'mananger configuration file can consist of a single object, or an ',
'array of objects. Objects must be of type EMConfig.']
HEAD is now at cecbd8129645 scripts/validate_configs.py: Ensure schemas are validated as well
```
Change-Id: Ib4d41c0f7a07c1ea4b9a483893969f21870a50e1
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/scripts/validate_configs.py b/scripts/validate_configs.py
index 0b47420..3afd335 100755
--- a/scripts/validate_configs.py
+++ b/scripts/validate_configs.py
@@ -134,11 +134,13 @@
)
sys.exit(2)
+ spec = jsonschema.Draft7Validator
+ spec.check_schema(schema)
base_uri = "file://{}/".format(
os.path.split(os.path.realpath(schema_file))[0]
)
resolver = jsonschema.RefResolver(base_uri, schema)
- validator = jsonschema.Draft7Validator(schema, resolver=resolver)
+ validator = spec(schema, resolver=resolver)
results = {
"invalid": [],