Skip to content

Commit 82f017a

Browse files
committed
rec_info: Validate through JSON schema
BFW-8025
1 parent 6895a28 commit 82f017a

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ cbor2~=5.7.1
44
jinja2~=3.1.5
55
numpy~=2.2.3
66
simple_parsing~=0.1.7
7+
referencing~=0.37.0
8+
jsonschema~=4.25.1

utils/rec_info.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
from record import Record
66
from common import default_config_file
77
from opt_check import opt_check
8+
from pathlib import Path
9+
import referencing
10+
import urllib.parse
11+
import jsonschema
12+
import jsonschema.validators
13+
import json
814

915
parser = argparse.ArgumentParser(prog="rec_info", description="Reads a record from the STDIN and prints various information about it in the YAML format")
1016
parser.add_argument("-c", "--config-file", type=str, default=default_config_file, help="Record configuration YAML file")
@@ -129,6 +135,24 @@
129135
return_fail = True
130136

131137

138+
# Check that the output of this utility is up to the spec
139+
def validate_output_with_json_schema():
140+
def file_retrieve(uri):
141+
path = Path(__file__).parent / "schema" / urllib.parse.urlparse(uri).path
142+
result = json.loads(path.read_text(encoding="utf-8"))
143+
return referencing.Resource.from_contents(result)
144+
145+
registry = referencing.Registry(retrieve=file_retrieve)
146+
entry = "opt_json.schema.json"
147+
148+
schema = registry.get_or_retrieve(entry).value.contents
149+
validator = jsonschema.validators.validator_for(schema)(schema, registry=registry)
150+
validator.validate(output)
151+
152+
153+
validate_output_with_json_schema()
154+
155+
132156
def yaml_hex_bytes_representer(dumper: yaml.SafeDumper, data: bytes):
133157
return dumper.represent_str("0x" + data.hex())
134158

0 commit comments

Comments
 (0)