summaryrefslogtreecommitdiffstats
path: root/scripts/schema/validator.py
blob: d537f92869ee12da320bcc3d141e20962c75f7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/python3

# A simple script to validate that a schema is valid for a file.

import argparse
import toml
import jsonschema_rs


def main():
    parser = argparse.ArgumentParser(
        description="Validates a file against a JSON schema"
    )
    parser.add_argument(
        "-f", "--file", type=str, required=True, help="The file to check."
    )
    parser.add_argument(
        "-s", "--schema", type=str, required=True, help="The schema to use."
    )
    parser.add_argument(
        "--should_fail",
        required=False,
        action="store_true",
        help="Whether the checked file should fail.",
    )
    args = parser.parse_args()

    file = args.file
    schema = args.schema
    should_fail = args.should_fail

    with open(file) as f, open(schema) as s:
        try:
            validator = jsonschema_rs.JSONSchema.from_str(s.read())
        except:
            print("Coudln't create validator.")
            exit()

        is_valid = validator.is_valid(toml.load(f))
        if is_valid:
            if should_fail:
                print("Fail!")
                exit(1)
            else:
                print("All good!")
        else:
            if should_fail:
                print("Caught error, good!")
            else:
                print("Fail!")
                exit(1)


if __name__ == "__main__":
    main()