def __init__(self, default=None, attribute=None, error=None, validate=None, required=False, **metadata): self.default = default self.attribute = attribute if error: warnings.warn('The error parameter is deprecated. Raise a ' 'marshmallow.ValidationError in your validators ' 'instead.', category=DeprecationWarning) self.error = error self.validate = validate if utils.is_iterable_but_not_string(validate): if not utils.is_generator(validate): self.validators = validate else: self.validators = [i for i in validate()] elif callable(validate): self.validators = [validate] elif validate is None: self.validators = [] else: raise ValueError("The 'validate' parameter must be a callable " 'or a collection of callables.') self.required = required self.metadata = metadata self._creation_index = Field._creation_index Field._creation_index += 1 self.parent = FieldABC.parent
def __init__(self, default=missing_, attribute=None, load_from=None, error=None, validate=None, required=False, allow_none=False, load_only=False, dump_only=False, missing=missing_, **metadata): self.default = default self.attribute = attribute self.load_from = load_from # this flag is used by Unmarshaller if error: warnings.warn('The error parameter is deprecated. Raise a ' 'marshmallow.ValidationError in your validators ' 'instead.', category=DeprecationWarning) self.error = error self.validate = validate if utils.is_iterable_but_not_string(validate): if not utils.is_generator(validate): self.validators = validate else: self.validators = list(validate) elif callable(validate): self.validators = [validate] elif validate is None: self.validators = [] else: raise ValueError("The 'validate' parameter must be a callable " "or a collection of callables.") self.required = required self.allow_none = allow_none self.load_only = load_only self.dump_only = dump_only self.missing = missing self.metadata = metadata self._creation_index = Field._creation_index Field._creation_index += 1 self.parent = FieldABC.parent
def __init__( self, default=missing_, attribute=None, load_from=None, dump_to=None, error=None, validate=None, required=False, allow_none=None, load_only=False, dump_only=False, missing=missing_, error_messages=None, **metadata ): self.default = default self.attribute = attribute self.load_from = load_from # this flag is used by Unmarshaller self.dump_to = dump_to # this flag is used by Marshaller self.validate = validate if utils.is_iterable_but_not_string(validate): if not utils.is_generator(validate): self.validators = validate else: self.validators = list(validate) elif callable(validate): self.validators = [validate] elif validate is None: self.validators = [] else: raise ValueError( "The 'validate' parameter must be a callable " "or a collection of callables." ) self.required = required # If missing=None, None should be considered valid by default if allow_none is None: if missing is None: self.allow_none = True else: self.allow_none = False else: self.allow_none = allow_none self.load_only = load_only self.dump_only = dump_only self.missing = missing self.metadata = metadata self._creation_index = Field._creation_index Field._creation_index += 1 # Collect default error message from self and parent classes messages = {} for cls in reversed(self.__class__.__mro__): messages.update(getattr(cls, "default_error_messages", {})) messages.update(error_messages or {}) self.error_messages = messages
def __init__(self, default=missing_, attribute=None, load_from=None, error=None, validate=None, required=False, allow_none=False, load_only=False, dump_only=False, missing=missing_, error_messages=None, **metadata): self.default = default self.attribute = attribute self.load_from = load_from # this flag is used by Unmarshaller self.validate = validate if utils.is_iterable_but_not_string(validate): if not utils.is_generator(validate): self.validators = validate else: self.validators = list(validate) elif callable(validate): self.validators = [validate] elif validate is None: self.validators = [] else: raise ValueError("The 'validate' parameter must be a callable " "or a collection of callables.") if isinstance(required, basestring): warnings.warn( 'Passing a string for `required` is deprecated. Pass ' '{"required": "Your message"} to `error_messages` instead.', DeprecationWarning) self.required = required if isinstance(allow_none, basestring): warnings.warn( 'Passing a string for `allow_none` is deprecated. Pass ' '{"null": "Your message"} to `error_messages` instead.', DeprecationWarning) self.allow_none = allow_none self.load_only = load_only self.dump_only = dump_only self.missing = missing self.metadata = metadata self._creation_index = Field._creation_index Field._creation_index += 1 self.parent = FieldABC.parent # Collect default error message from self and parent classes messages = {} for cls in reversed(self.__class__.__mro__): messages.update(getattr(cls, 'default_error_messages', {})) messages.update(error_messages or {}) self.error_messages = messages
def build_validator_list(src): if src is None: return [] if utils.is_iterable_but_not_string(src): if utils.is_generator(src): return list(src) else: return src elif callable(src): return [src] else: raise ValueError("The 'validate' parameter must be a callable " "or a collection of callables.")
def __init__(self, default=missing_, attribute=None, load_from=None, dump_to=None, error=None, validate=None, required=False, allow_none=None, load_only=False, dump_only=False, missing=missing_, error_messages=None, **metadata): self.default = default self.attribute = attribute self.load_from = load_from # this flag is used by Unmarshaller self.dump_to = dump_to # this flag is used by Marshaller self.validate = validate if utils.is_iterable_but_not_string(validate): if not utils.is_generator(validate): self.validators = validate else: self.validators = list(validate) elif callable(validate): self.validators = [validate] elif validate is None: self.validators = [] else: raise ValueError("The 'validate' parameter must be a callable " "or a collection of callables.") self.required = required # If missing=None, None should be considered valid by default if allow_none is None: if missing is None: self.allow_none = True else: self.allow_none = False else: self.allow_none = allow_none self.load_only = load_only self.dump_only = dump_only self.missing = missing self.metadata = metadata self._creation_index = Field._creation_index Field._creation_index += 1 self.parent = FieldABC.parent # Collect default error message from self and parent classes messages = {} for cls in reversed(self.__class__.__mro__): messages.update(getattr(cls, 'default_error_messages', {})) messages.update(error_messages or {}) self.error_messages = messages