Skip to content

Commit 80340a5

Browse files
committed
Updated because the Validator no longer expects a parameter named types
1 parent 4dc990b commit 80340a5

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

aodntools/ncwriter/schema.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
"""This module holds schema definitions for validating the various :py:class:`dicts` that make up parts of a
22
template, and also the helper functions necessary to validate an object against their respective schema.
33
"""
4-
54
import json
6-
75
import numpy as np
86
from jsonschema import validators, Draft4Validator, FormatChecker, ValidationError
97
from pkg_resources import resource_filename
108

9+
def is_array(checker, instance):
10+
return isinstance(instance, (list, np.ndarray))
1111

12-
# Create a new validator class (based on Draft4Validator) to allow templates to use
13-
# * Python types or numpy dtypes to specify variable data types; and
14-
# * numpy arrays to specify variable data.
15-
TemplateValidator = validators.create(meta_schema=Draft4Validator.META_SCHEMA,
16-
validators=Draft4Validator.VALIDATORS)
17-
format_checker = FormatChecker()
12+
# Extend the default type checker by redefining "array"
13+
custom_type_checker = Draft4Validator.TYPE_CHECKER.redefine("array", is_array)
1814

15+
# Create a custom validator that uses the new type checker.
16+
CustomValidator = validators.extend(Draft4Validator, type_checker=custom_type_checker)
17+
format_checker = FormatChecker()
1918

2019
@format_checker.checks('datatype')
2120
def is_python_datatype(value):
@@ -24,32 +23,25 @@ def is_python_datatype(value):
2423
return True
2524
if isinstance(value, type):
2625
return issubclass(value, np.number)
27-
2826
return False
2927

30-
31-
TYPES = {'array': (list, np.ndarray)}
32-
3328
TEMPLATE_SCHEMA_JSON = resource_filename(__name__, 'template_schema.json')
3429
with open(TEMPLATE_SCHEMA_JSON) as f:
3530
TEMPLATE_SCHEMA = json.load(f)
36-
TemplateValidator.check_schema(TEMPLATE_SCHEMA)
37-
38-
template_validator = TemplateValidator(TEMPLATE_SCHEMA, types=TYPES, format_checker=format_checker)
31+
CustomValidator.check_schema(TEMPLATE_SCHEMA)
3932

33+
# Use the custom validator
34+
template_validator = CustomValidator(TEMPLATE_SCHEMA, format_checker=format_checker)
4035

4136
def validate_template(t):
4237
template_validator.validate(t)
4338

44-
4539
def validate_dimensions(d):
4640
validate_template({'_dimensions': d})
4741

48-
4942
def validate_variables(v):
5043
validate_template({'_variables': v})
51-
52-
44+
5345
def validate_global_attributes(a):
5446
if hasattr(a, 'keys'):
5547
special = [k for k in a.keys() if k.startswith('_')]

0 commit comments

Comments
 (0)