Skip to content

Commit ab9fa4a

Browse files
authored
delete concept of default metadata and clarify validation err (#69)
1 parent f1a3f47 commit ab9fa4a

File tree

1 file changed

+9
-50
lines changed

1 file changed

+9
-50
lines changed

sigmf/validate.py

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,7 @@
2525
from . import error, schema, sigmffile
2626

2727

28-
def extend_with_default(validator_class):
29-
"""
30-
Boilerplate code from [1] to retrieve jsonschema default dict.
31-
32-
References
33-
----------
34-
[1] https://python-jsonschema.readthedocs.io/en/stable/faq/
35-
"""
36-
validate_properties = validator_class.VALIDATORS["properties"]
37-
38-
def set_defaults(validator, properties, instance, topschema):
39-
for property, subschema in properties.items():
40-
if "default" in subschema:
41-
instance.setdefault(property, subschema["default"])
42-
43-
for err in validate_properties(
44-
validator,
45-
properties,
46-
instance,
47-
topschema,
48-
):
49-
yield err
50-
51-
return jsonschema.validators.extend(
52-
validator_class,
53-
{"properties": set_defaults},
54-
)
55-
56-
57-
def get_default_metadata(ref_schema=schema.get_schema()):
58-
"""
59-
retrieve defaults from schema
60-
FIXME: not working yet
61-
"""
62-
default = {}
63-
validator = extend_with_default(jsonschema.Draft7Validator)
64-
validator(ref_schema).validate(default)
65-
return default
66-
67-
68-
def validate(metadata, ref_schema=schema.get_schema()):
28+
def validate(metadata, ref_schema=schema.get_schema()) -> None:
6929
"""
7030
Check that the provided `metadata` dict is valid according to the `ref_schema` dict.
7131
Walk entire schema and check all keys.
@@ -79,22 +39,21 @@ def validate(metadata, ref_schema=schema.get_schema()):
7939
Since the schema evolves over time, we may want to be able to check
8040
against different versions in the *future*.
8141
82-
Returns
83-
-------
84-
None, will raise error if invalid.
42+
Raises
43+
------
44+
ValidationError
45+
If metadata is invalid.
8546
"""
8647
jsonschema.validators.validate(instance=metadata, schema=ref_schema)
8748

88-
# assure capture and annotation order
89-
# TODO: There is a way to do this with just the schema apparently.
49+
# ensure captures and annotations have monotonically increasing sample_start
9050
for key in ["captures", "annotations"]:
9151
count = -1
9252
for item in metadata[key]:
9353
new_count = item["core:sample_start"]
9454
if new_count < count:
95-
raise jsonschema.exceptions.ValidationError(f"{key} has bad order")
96-
else:
97-
count = new_count
55+
raise jsonschema.exceptions.ValidationError(f"{key} has incorrect sample start ordering.")
56+
count = new_count
9857

9958

10059
def _validate_single_file(filename, skip_checksum: bool, logger: logging.Logger) -> int:
@@ -125,7 +84,7 @@ def _validate_single_file(filename, skip_checksum: bool, logger: logging.Logger)
12584
# handle any of 4 exceptions at once...
12685
except (jsonschema.exceptions.ValidationError, error.SigMFFileError, json.decoder.JSONDecodeError, IOError) as err:
12786
# catch the error, log, and continue
128-
logger.error("file `{}`: {}".format(filename, err))
87+
logger.error(f"file `{filename}`: {err}")
12988
return 1
13089
else:
13190
return 0

0 commit comments

Comments
 (0)