def get_validator(overwrite=False, normalizers=None, **kwargs): r"""Return the validator that includes ygg expansion types. Args: overwrite (bool, optional): If True, the existing validator will be overwritten. Defaults to False. normalizers (dict, optional): Additional normalizers to add. **kwargs: Additional keyword arguments are passed to normalizer.create. Returns: jsonschema.IValidator: JSON schema validator. """ from yggdrasil.metaschema import normalizer global _validator if (_validator is None) or overwrite: metaschema = get_metaschema() # Get set of validators all_validators = copy.deepcopy(_base_validator.VALIDATORS) for k, v in get_registered_properties().items(): if (not v._replaces_existing): assert (k not in all_validators) all_validators[k] = v.wrapped_validate # Get set of datatypes # TODO: This will need to be changed if back-ported in jsonschema if _jsonschema_ver_maj < 3: all_datatypes = copy.deepcopy(_base_validator.DEFAULT_TYPES) for k, v in get_registered_types().items(): if (not v._replaces_existing): # Error raised on registration assert (k not in all_datatypes) all_datatypes[k] = v.python_types kwargs['default_types'] = all_datatypes else: type_checker = copy.deepcopy(_base_validator.TYPE_CHECKER) new_type_checkers = {} for k, v in get_registered_types().items(): if (not v._replaces_existing): # Error raised on registration assert (k not in type_checker._type_checkers) new_type_checkers[k] = v.jsonschema_type_checker kwargs['type_checker'] = type_checker.redefine_many( new_type_checkers) # Get set of normalizers if normalizers is None: normalizers = {} # Use default base and update validators _validator = normalizer.create(meta_schema=metaschema, validators=all_validators, normalizers=normalizers, **kwargs) _validator._base_validator = _base_validator return _validator
def test_create_normalizer(): r"""Test create normalizer with default types.""" cls = normalizer.create(get_metaschema()) assert_equal(cls({'type': 'int'}).normalize('1'), '1')
def test_normalize_schema(): r"""Test normalize_schema method on Normalizer.""" kwargs = dict(normalizer_validators={'invalid': None}) cls = normalizer.create(get_metaschema()) cls.normalize_schema({'type': 'int'}, **kwargs)