def _validate(cls, validator, value, instance, schema): r"""Validator for 'class' property.""" # TODO: Normalization can be removed if metadata is normalized from yggdrasil.metaschema import validate_instance value = validate_instance(value, cls.schema, normalize=True) if not isinstance(instance, value): yield "Instance %s is not of type(s) %s" % (instance, value)
def validate_component(self, comp_name, obj): r"""Validate an object against a specific component. Args: comp_name (str): Name of the component to validate against. obj (object): Object to validate. """ comp_schema = self.get_component_schema(comp_name) return metaschema.validate_instance(obj, comp_schema)
def validate_definition(cls, obj, **kwargs): r"""Validates a type definition. Args: obj (object): Type definition to validate. **kwargs: Additional keyword arguments are passed to the validator. """ # jsonschema.validate(obj, cls.metaschema(), cls=cls.validator()) # jsonschema.validate(obj, cls.definition_schema(), cls=cls.validator()) return validate_instance(obj, cls.definition_schema(), **kwargs)
def validate_instance(cls, obj, typedef, **kwargs): r"""Validates an object against a type definition. Args: obj (object): Object to validate against a type definition. typedef (dict): Type definition to validate against. **kwargs: Additional keyword arguments are passed to the validator. """ # cls.validate_definition(typedef) # jsonschema.validate(obj, typedef, cls=cls.validator()) return validate_instance(obj, typedef, **kwargs)
def validate(self, obj, **kwargs): r"""Validate an object against this schema. Args: obj (object): Object to valdiate. **kwargs: Additional keyword arguments are passed to validate_instance. """ if kwargs.get('normalize', False): kwargs.setdefault('normalizers', self._normalizers) # kwargs.setdefault('no_defaults', True) kwargs.setdefault('schema_registry', self) return metaschema.validate_instance(obj, self.schema, **kwargs)
def compare(cls, prop1, prop2, root1=None, root2=None): r"""Method to determine compatiblity of one property value with another. This method is not necessarily symmetric in that the second value may not be compatible with the first even if the first is compatible with the second. Args: prop1 (object): Property value to compare against prop2. prop2 (object): Property value to compare against. Yields: str: Comparision failure messages. """ # TODO: Normalization can be removed if metadata is normalized from yggdrasil.metaschema import validate_instance prop1 = validate_instance(prop1, cls.schema, normalize=True) prop2 = validate_instance(prop2, cls.schema, normalize=True) if not isinstance(prop1, (list, tuple)): prop1 = (prop1, ) if not isinstance(prop2, (list, tuple)): prop2 = (prop2, ) if not (set(prop1) & set(prop2)): yield 'No overlap between %s and %s' % (prop1, prop2)
def validate_metadata(cls, obj, **kwargs): r"""Validates an encoded object. Args: obj (string): Encoded object to validate. **kwargs: Additional keyword arguments are passed to the validator. """ if ((isinstance(obj, dict) and ('type' in obj) and (obj['type'] != cls.name))): type_cls = get_type_class(obj['type']) if type_cls.is_fixed and type_cls.issubtype(cls.name): obj = type_cls.typedef_fixed2base(obj) # jsonschema.validate(obj, cls.metaschema(), cls=cls.validator()) # jsonschema.validate(obj, cls.metadata_schema(), cls=cls.validator()) return validate_instance(obj, cls.metadata_schema(), **kwargs)
def test_validate_instance(): r"""Test validate_instance.""" for k, v in _valid_objects.items(): metaschema.validate_instance(v, {'type': k})
def __init__(self, skip_component_schema_normalization=None, **kwargs): if skip_component_schema_normalization is None: skip_component_schema_normalization = (not (os.environ.get( 'YGG_VALIDATE_COMPONENTS', 'None').lower() in ['true', '1'])) comptype = self._schema_type if (comptype is None) and (not self._schema_properties): self.extra_kwargs = kwargs return subtype = None if self._schema_subtype_key is not None: subtype = getattr( self, self._schema_subtype_key, getattr(self, '_%s' % self._schema_subtype_key, None)) # Fall back to some simple parsing/normalization to save time on # full jsonschema normalization for k, v in self._schema_properties.items(): if k in self._schema_excluded_from_class: continue default = v.get('default', None) if (k == self._schema_subtype_key) and (subtype is not None): default = subtype if default is not None: kwargs.setdefault(k, copy.deepcopy(default)) if v.get('type', None) == 'array': if isinstance(kwargs.get(k, None), (bytes, str)): kwargs[k] = kwargs[k].split() # Parse keyword arguments using schema if (((comptype is not None) and (subtype is not None) and (not skip_component_schema_normalization))): from yggdrasil.schema import get_schema s = get_schema().get_component_schema( comptype, subtype, relaxed=True, allow_instance_definitions=True) props = list(s['properties'].keys()) if not skip_component_schema_normalization: from yggdrasil import metaschema kwargs.setdefault(self._schema_subtype_key, subtype) # Remove properties that shouldn't ve validated in class for k in self._schema_excluded_from_class_validation: if k in s['properties']: del s['properties'][k] # Validate and normalize metaschema.validate_instance(kwargs, s, normalize=False) # TODO: Normalization performance needs improvement # import pprint # print('before') # pprint.pprint(kwargs_comp) # kwargs_comp = metaschema.validate_instance(kwargs_comp, s, # normalize=True) # kwargs.update(kwargs_comp) # print('normalized') # pprint.pprint(kwargs_comp) else: props = self._schema_properties.keys() # Set attributes based on properties for k in props: if k in self._schema_excluded_from_class: continue v = kwargs.pop(k, None) if getattr(self, k, None) is None: setattr(self, k, v) # elif (getattr(self, k) != v) and (v is not None): # warnings.warn(("The schema property '%s' is provided as a " # "keyword with a value of %s, but the class " # "already has an attribute of the same name " # "with the value %s.") # % (k, v, getattr(self, k))) self.extra_kwargs = kwargs