12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import os
- from os import listdir
- from os.path import isfile
- import json
- from jsonschema import validate
- from jsonschema.exceptions import ValidationError
- def validate_configs(schemata_and_configs):
- for schema_loc, configs in schemata_and_configs.items():
- with open(schema_loc, mode="r") as schema_file:
- schema = json.load(schema_file)
- for config_loc in configs:
- with open(config_loc, mode="r") as config_file:
- config = json.load(config_file)
- try:
- validate(instance=config, schema=schema)
- print("{} is valid with respect to {}.".format(config_loc, schema_loc))
- except ValidationError as verr:
- print("{} is not valid with respect to {}.".format(config_loc, schema_loc))
- print(str(verr))
- def get_files_from_directory(directory, filter_proc):
- return [os.path.join(directory, f)
- for f in listdir(directory)
- if all([
- isfile(os.path.join(directory, f)),
- filter_proc(os.path.join(directory, f))])]
- def main():
- config_dir = os.path.join("config")
- schema_dir = os.path.join(config_dir, "schema")
- # define a schema to use for validating a list of json files
- schemata_and_configs = {
- os.path.join(schema_dir, "player-schema.json"):
- get_files_from_directory(
- os.path.join(config_dir, "player"),
- filter_proc=lambda fname: fname.endswith("json")),
- os.path.join(schema_dir, "transportation-schema.json"):
- get_files_from_directory(
- os.path.join(config_dir, "transportation"),
- filter_proc=lambda fname: fname.endswith("json")),
- os.path.join(schema_dir, "general-schema.json"): [
- os.path.join(config_dir, "general.json")]
- }
- validate_configs(schemata_and_configs)
- if __name__ == "__main__":
- main()
|