def __call__(self, value): if self.inclusive: if value > self.endpoint: raise ValidationError(f'expected at most {self.endpoint!r}', value=value) else: if value >= self.endpoint: raise ValidationError(f'expected less than {self.endpoint!r}', value=value)
def __call__(self, value): if self.inclusive: if value < self.endpoint: raise ValidationError(f'expected at least {self.endpoint!r}', value=value) else: if value <= self.endpoint: raise ValidationError(f'expected more than {self.endpoint!r}', value=value)
def validate(self, value): """ Validate that the given value is one of the choices. """ super(Choice, self).validate(value) if value not in self.choices: raise ValidationError('invalid choice', value=value)
def deserialize(self, d): """ Deserialize the given dictionary to a `~serde.Model` instance. """ if not isinstance(d, MappingType): raise ValidationError("invalid type, expected 'mapping'", value=d) return self.ty.from_dict(d)
def deserialize(self, value): """ Deserialize the given string as a `~datetime.date`. """ if self.format == 'iso8601': try: return isodate.parse_date(value) except isodate.ISO8601Error: raise ValidationError('invalid ISO 8601 date', value=value) else: try: return datetime.datetime.strptime(value, self.format).date() except (TypeError, ValueError): raise ValidationError( 'invalid date, expected format {!r}'.format(self.format), value=value, )
def validate(self, value): """ Validate that the given value is equal to the value. """ if value != self.value: raise ValidationError('invalid literal, expected {!r}'.format( self.value), value=value)
def validate(self, value): """ Validate the given value is an instance of the specified type. """ if not isinstance(value, self.ty): raise ValidationError('invalid type, expected {!r}'.format( self.ty.__name__), value=value)
def deserialize(self, value): """ Deserialize a tag value into a Model variant. """ variant = self.lookup_variant(value) if not variant: raise ValidationError('no variant found', value=value) return variant
def validate(self, value): """ Validate the given deque. """ super(Deque, self).validate(value) if value.maxlen != self.maxlen: raise ValidationError('invalid max length, expected {}'.format( self.maxlen), value=value)
def _deserialize_with(self, model, d): """ Deserialize the model variant from an adjacently tagged dictionary. """ try: tag = d[self.tag] except KeyError: raise ValidationError('missing data, expected tag {!r}'.format( self.tag)) try: content = d[self.content] except KeyError: raise ValidationError('missing data, expected content {!r}'.format( self.content)) model.__class__ = self._deserialize(tag) return model, content
def test_messages(self): error = ValidationError('something failed') f1 = fields.Field() f2 = fields.Field(rename='f-two') f3 = fields.Field() f1._bind(object(), 'f1') f2._bind(object(), 'f2') f3._bind(object(), 'f3') error._fields.append(f3) error._fields.append(f2) error._fields.append(f1) assert error.messages() == { 'f1': { 'f-two': { 'f3': 'something failed' } } }
def _iter(self, value): """ Iterate over the sequence. """ try: for element in enumerate(value): yield element except TypeError: raise ValidationError('invalid type, expected {!r}'.format( self.ty.__name__), value=value)
def _iter(self, value): """ Iterate over the mapping's items. """ try: for element in value.items(): yield element except (AttributeError, TypeError): raise ValidationError('invalid type, expected {!r}'.format( self.ty.__name__), value=value)
def _deserialize_with(self, model, d): """ Deserialize the corresponding model attribute from a dictionary. """ try: value = d[self._serde_name] except KeyError: raise ValidationError('missing data, expected field {!r}'.format( self._serde_name)) setattr(model, self._attr_name, self._deserialize(value)) return model, d
def validate(self, value): """ Validate the given string matches the specified regex. """ super(Regex, self).validate(value) if not self._compiled.match(value): raise ValidationError( 'invalid string, expected to match regex pattern {!r}'.format( self.pattern), value=value, )
def _deserialize_with(self, model, d): """ Deserialize the model variant from an externally tagged dictionary. """ try: tag = next(iter(d)) except StopIteration: raise ValidationError( 'missing data, expected externally tagged data') model.__class__ = self._deserialize(tag) return model, d[tag]
def _iter(self, value): """ Iterate over the fields and each element in the tuple. """ try: for element in zip_equal(self.elements, super(Tuple, self)._iter(value)): yield element except ValueError: raise ValidationError( 'invalid length, expected {} elements'.format( len(self.elements), ), value=value, )
def validate(self): if self.a == 0: raise ValidationError('invalid value') assert self.a != 0
def validate(self, value): super(IpAddress, self).validate(value) if not self._validator_ipv4(value) and not self._validator_ipv6(value): raise ValidationError('invalid IP address', value=value)
def test_add_context(): field = object() with pytest.raises(ValidationError) as e: with add_context(field): raise ValidationError('something failed') assert e.value._fields == [field]
def test___init___value(self): # You should be able to construct a ValidationError with a value. error = ValidationError('something failed', value=5) assert error.args == ('something failed', ) assert error.value == 5 assert error._fields == []
def test___init___basic(self): # You should be able to construct ValidationError without any value. error = ValidationError('something failed') assert error.args == ('something failed', ) assert error.value is None assert error._fields == []
def __call__(self, value): if len(value) != self.length: raise ValidationError('expected length {!r}'.format(self.length), value=value)
def validate(self, value): super(field_cls, self).validate(value) if not self._validator(value): raise ValidationError('invalid {}'.format(human), value=value)
def __call__(self, value): if len(value) != self.length: raise ValidationError(f'expected length {self.length!r}', value=value)