Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
 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)
Пример #5
0
 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,
             )
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
Файл: tags.py Проект: grgi/serde
 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
Пример #9
0
 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)
Пример #10
0
Файл: tags.py Проект: grgi/serde
    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
Пример #11
0
    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'
                }
            }
        }
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 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
Пример #15
0
 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,
         )
Пример #16
0
Файл: tags.py Проект: grgi/serde
    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]
Пример #17
0
 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,
         )
Пример #18
0
 def validate(self):
     if self.a == 0:
         raise ValidationError('invalid value')
     assert self.a != 0
Пример #19
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)
Пример #20
0
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]
Пример #21
0
 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 == []
Пример #22
0
 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 == []
Пример #23
0
 def __call__(self, value):
     if len(value) != self.length:
         raise ValidationError('expected length {!r}'.format(self.length),
                               value=value)
Пример #24
0
 def validate(self, value):
     super(field_cls, self).validate(value)
     if not self._validator(value):
         raise ValidationError('invalid {}'.format(human), value=value)
Пример #25
0
 def __call__(self, value):
     if len(value) != self.length:
         raise ValidationError(f'expected length {self.length!r}',
                               value=value)