def getFields(schema): """Return a dictionary containing all the Fields in a schema. """ fields = {} for name in schema: attr = schema[name] if IValidatable.providedBy(attr): fields[name] = attr return fields
def get_schema_validation_errors(schema, value, _validating_objects=_ObjectsBeingValidated()): """ Validate that *value* conforms to the schema interface *schema*. All :class:`zope.schema.interfaces.IField` members of the *schema* are validated after being bound to *value*. (Note that we do not check for arbitrary :class:`zope.interface.Attribute` members being present.) :return: A `dict` mapping field names to `ValidationError` subclasses. A non-empty return value means that validation failed. """ errors = {} # Interface can be used as schema property for Object fields that plan to # hold values of any type. # Because Interface does not include any Attribute, it is obviously not # worth looping on its methods and filter them all out. if schema is Interface: return errors # if `value` is part of a cyclic graph, we need to break the cycle to avoid # infinite recursion. Collect validated objects in a thread local dict by # it's python represenation. A previous version was setting a volatile # attribute which didn't work with security proxy id_value = id(value) ids_being_validated = _validating_objects.ids_being_validated if id_value in ids_being_validated: return errors ids_being_validated.add(id_value) # (If we have gotten here, we know that `value` provides an interface # other than zope.interface.Interface; # iow, we can rely on the fact that it is an instance # that supports attribute assignment.) try: for name in schema.names(all=True): attribute = schema[name] if IMethod.providedBy(attribute): continue # pragma: no cover try: if IValidatable.providedBy(attribute): # validate attributes that are fields field_value = getattr(value, name) attribute = attribute.bind(value) attribute.validate(field_value) except ValidationError as error: errors[name] = error except AttributeError as error: # property for the given name is not implemented errors[name] = SchemaNotFullyImplemented( error).with_field_and_value(attribute, None) finally: ids_being_validated.remove(id_value) return errors
def get_schema_validation_errors(schema, value, _validating_objects=_ObjectsBeingValidated()): """ Validate that *value* conforms to the schema interface *schema*. All :class:`zope.schema.interfaces.IField` members of the *schema* are validated after being bound to *value*. (Note that we do not check for arbitrary :class:`zope.interface.Attribute` members being present.) :return: A `dict` mapping field names to `ValidationError` subclasses. A non-empty return value means that validation failed. """ errors = {} # Interface can be used as schema property for Object fields that plan to # hold values of any type. # Because Interface does not include any Attribute, it is obviously not # worth looping on its methods and filter them all out. if schema is Interface: return errors # if `value` is part of a cyclic graph, we need to break the cycle to avoid # infinite recursion. Collect validated objects in a thread local dict by # it's python represenation. A previous version was setting a volatile # attribute which didn't work with security proxy id_value = id(value) ids_being_validated = _validating_objects.ids_being_validated if id_value in ids_being_validated: return errors ids_being_validated.add(id_value) # (If we have gotten here, we know that `value` provides an interface # other than zope.interface.Interface; # iow, we can rely on the fact that it is an instance # that supports attribute assignment.) try: for name in schema.names(all=True): attribute = schema[name] if IMethod.providedBy(attribute): continue # pragma: no cover try: if IValidatable.providedBy(attribute): # validate attributes that are fields field_value = getattr(value, name) attribute = attribute.bind(value) attribute.validate(field_value) except ValidationError as error: errors[name] = error except AttributeError as error: # property for the given name is not implemented errors[name] = SchemaNotFullyImplemented(error).with_field_and_value(attribute, None) finally: ids_being_validated.remove(id_value) return errors