1#!/usr/bin/python3 -i
2#
3# Copyright 2020-2023 The Khronos Group Inc.
4#
5# SPDX-License-Identifier: Apache-2.0
6
7# Description:
8# -----------
9# This script validates a json pipeline file against the schema files.
10
11import os,sys
12import re
13import argparse
14import json
15import jsonschema
16
17base_schema_filename = os.path.join("..", "json", "vk.json")
18vkpcc_schema_filename = os.path.join("..", "json", "vkpcc.json")
19
20# Parses input arguments
21def ParseArgs():
22    parser = argparse.ArgumentParser()
23    parser.add_argument('json_file', help='The json file to validate')
24    return parser.parse_args()
25
26def main():
27    args           = ParseArgs()
28    jsonText       = ""
29    baseSchemaText = ""
30    vkSchemaText   = ""
31
32    # Exit with error if json or schema files do not exist
33    if not os.path.exists(args.json_file):
34        print('Error: json file \"%s\" does not exist.' % args.json_file)
35        sys.exit(1)
36    elif not os.path.exists(base_schema_filename):
37        print('Error: json file \"%s\" does not exist.' % base_schema_filename)
38        sys.exit(1)
39    elif not os.path.exists(vkpcc_schema_filename):
40        print('Error: json file \"%s\" does not exist.' % vkpcc_schema_filename)
41        sys.exit(1)
42
43    # Read the json schemas files in as text
44    with open(base_schema_filename) as baseSchemaFile:
45        baseSchemaText = baseSchemaFile.read()
46    with open(vkpcc_schema_filename) as vkSchemaFile:
47        vkSchemaText = vkSchemaFile.read()
48    with open(args.json_file) as jsonFile:
49        jsonText = jsonFile.read()
50    baseSchema = json.loads(baseSchemaText)
51    vkSchema   = json.loads(vkSchemaText)
52    jsonData   = json.loads(jsonText)
53
54    # Ensure that the generated vk.json schema is a valid schema
55    try:
56        jsonschema.Draft4Validator.check_schema(baseSchema)
57        print(base_schema_filename, "is valid")
58    except jsonschema.SchemaError as e:
59        print(base_schema_filename, "error: " + str(e))
60
61    # Ensure that vkpcc.json is also a valid schema
62    try:
63        jsonschema.Draft4Validator.check_schema(vkSchema)
64        print(vkpcc_schema_filename, "schema is valid")
65    except jsonschema.exceptions.SchemaError as e:
66        print(vkpcc_schema_filename, "schema error: " + str(e))
67
68    # Construct a schema validator object from the two schema files
69    schemaRefStore = {
70        baseSchema["id"] : baseSchema,
71        vkSchema["id"]   : vkSchema
72    }
73    resolver  = jsonschema.RefResolver.from_schema(baseSchema, store=schemaRefStore)
74    validator = jsonschema.Draft4Validator(vkSchema, resolver=resolver)
75
76    # Validate the input .json file using the schemas
77    for error in sorted(validator.iter_errors(jsonData), key=str):
78        print(error.message)
79        print(list(error.path))
80        for suberror in sorted(error.context, key=lambda e: e.schema_path):
81            print(list(suberror.path), suberror.message, sep="\n")
82        print("\n")
83
84if __name__ == '__main__':
85    main()
86