コード例 #1
0
class TestFlatSerializer(Serializer):
    _type = TypeField('test_object')
    id = IntegerField(required=True, identity=True)
    name = StringField(required=True)
    street = StringField(required=False, on_null=HIDE_FIELD)
    uuid_var = UUIDField(required=True)
    maxmin = IntegerField(max_value=10, min_value=6, required=True)
    datetime_var = DatetimeField(required=True)
    date_var = DateField(required=True)
    time_var = TimeField(required=True)
    haus = StringField(required=True, map_field='house')
    url = UrlField(required=True, base='http://www.base.com', default='api')
    action = StringField(required=False, action_field=True)
コード例 #2
0
class RecursionRefSerializerTwo(Serializer):
    name = StringField()
    ref_one = SerializerField('RecursionRefSerializerOne',
                              exclude=['sers'],
                              required=True)
    ref_two = SerializerField('RecursionRefSerializerOne',
                              exclude=['sers'],
                              required=True)
コード例 #3
0
    class TestSerializerOne(Serializer):
        _type = TypeField('test_object_one')
        street = StringField(required=True)

        def street_clean_value(self, value):
            if py2to3._unicode(value).startswith('sesame'):
                return 'Street for kids'
            else:
                return value
コード例 #4
0
class CustomValidationSerializer(Serializer):
    code = StringField(required=True, max_length=3)
    name = StringField(required=True, map_field='foo')
    pk = UUIDField(required=False)

    def code_validate(self, value):
        if value == 'ABC':
            raise SerializerFieldValueError(message='ABC is not allowed.')

    def foo_validate(self, value):
        if value == 'FOOBAR':
            raise SerializerFieldValueError(message='FOOBAR is not allowed.')

    def pk_validate(self, value):
        if not isinstance(value, uuid.UUID):
            raise SerializerFieldValueError('NEVER RAISE')
        elif str(value).upper() == 'BD0623E8-27A6-4A94-9B57-2A6D833D55F7':
            raise SerializerFieldValueError(
                message='BD0623E8-27A6-4A94-9B57-2A6D833D55F7 is not allowed.')
コード例 #5
0
    class TestNestSerializer(Serializer):
        class NestSerializer(Serializer):
            name = StringField(required=True)
            id = IntegerField(required=True, identity=True)
            planet = StringField(required=True)
            os = StringField(required=True)

        _type = TypeField('test_object')
        id = IntegerField(required=True, identity=True)
        name = StringField(required=True)
        nest = SerializerField(NestSerializer, fields=['name'], required=True)
コード例 #6
0
    class TestSerializerTwo(Serializer):
        _type = TypeField('test_object_two')
        street = StringField(required=True)

        def street_to_python(self, field):
            value = field.to_python()
            return 'Changed for python: {}'.format(value)

        def street_to_native(self, field):
            value = field.to_native()
            return 'Changed for native: {}'.format(value)
コード例 #7
0
class MySerializer(Serializer):
    class MyNestSerializer(Serializer):
        name = StringField(required=True)
        id = IntegerField(required=True, identity=True)

    _type = TypeField('test_object')
    id = IntegerField(required=True, identity=True)
    name = StringField(required=True)
    date_var = DateField(required=True, map_field='dt')
    time_var = TimeField(required=True, map_field='t')
    url = UrlField(required=True, base='http://www.base.com', default='api')
    nest = SerializerField(MyNestSerializer, required=True)
コード例 #8
0
    class TestSerializerThree(Serializer):
        _type = TypeField('test_object_three')
        street = StringField(required=True)

        def street_to_python(self, field):
            value = field.to_python()
            if value.startswith('ignore'):
                raise IgnoreField()
            return value

        def street_to_native(self, field):
            value = field.to_native()
            if value.startswith('ignore'):
                raise IgnoreField()
            return value
コード例 #9
0
ファイル: field_tests.py プロジェクト: MRigal/aserializer
    def test_max_min_length(self):
        field = StringField(required=True, max_length=7, min_length=4)
        field.set_value('foobar')
        field.validate()
        self.assertEqual(field.to_python(), 'foobar')
        self.assertEqual(field.to_native(), 'foobar')

        field = StringField(required=True, max_length=7, min_length=4)
        field.set_value('foo')
        self.assertRaises(SerializerFieldValueError, field.validate)

        field = StringField(required=True, max_length=7, min_length=4)
        field.set_value('foobarfoobar')
        self.assertRaises(SerializerFieldValueError, field.validate)
コード例 #10
0
 class TestSerializer(Serializer):
     _type = TypeField('test_type')
     name = StringField(required=True)
     email = EmailField(required=True)
コード例 #11
0
 def test_default(self):
     field = StringField(required=True, default='a string')
     field.validate()
     self.assertEqual(field.to_python(), 'a string')
     self.assertEqual(field.to_native(), 'a string')
コード例 #12
0
 class KnownTestSerialzer(Serializer):
     code = StringField(required=True, max_length=3)
     number = IntegerField(required=True)
     address = SerializerField(NestKnownTestSerialzer,
                               required=True,
                               fields=['street'])
コード例 #13
0
 class KnownTestSerialzer(Serializer):
     code = StringField(required=True, max_length=3)
     number = IntegerField(required=True)
     address = SerializerField(UnknownTestSerialzer, required=True)
コード例 #14
0
 class ListItemSerialzer(Serializer):
     code = StringField(required=True, max_length=3)
     uuid = UUIDField(required=True)
コード例 #15
0
ファイル: field_tests.py プロジェクト: MRigal/aserializer
 def test_default(self):
     field = StringField(required=True, default='a string')
     field.validate()
     self.assertEqual(field.to_python(), 'a string')
     self.assertEqual(field.to_native(), 'a string')
コード例 #16
0
 def test_set_value(self):
     field = StringField(required=True)
     field.set_value('string')
     field.validate()
     self.assertEqual(field.to_python(), 'string')
     self.assertEqual(field.to_native(), 'string')
コード例 #17
0
        class SE(Serializer):
            name = StringField()

            def the_method(self, field):
                return 'test {}'.format(field.to_python())
コード例 #18
0
class MetaTestSerializer(Serializer):
    name = StringField()
    last_name = StringField()
    street = StringField()
    city = StringField()
    country = StringField()
コード例 #19
0
class NotRequiredSerializerField(Serializer):
    name = StringField()
    nest = SerializerField(NotRequiredNest, required=False)
コード例 #20
0
class NotRequiredNest(Serializer):
    name = StringField(required=True)
    foo = StringField(required=False)
コード例 #21
0
 class TestSerializer(Serializer):
     uuid = UUIDField(required=True)
     name = StringField(required=True)
     foo_number = IntegerField(required=False)
コード例 #22
0
 def test_hide_on_null(self):
     field = StringField(required=False, on_null=HIDE_FIELD)
     self.assertRaises(IgnoreField, field.to_native)
     self.assertIsNone(field.to_python())
コード例 #23
0
 class NestSerializer(Serializer):
     name = StringField(required=True)
     id = IntegerField(required=True, identity=True)
     planet = StringField(required=True)
     os = StringField(required=True)
コード例 #24
0
ファイル: field_tests.py プロジェクト: MRigal/aserializer
 def test_hide_on_null(self):
     field = StringField(required=False, on_null=HIDE_FIELD)
     self.assertRaises(IgnoreField, field.to_native)
     self.assertIsNone(field.to_python())
コード例 #25
0
ファイル: field_tests.py プロジェクト: MRigal/aserializer
 def test_validate_raises(self):
     field = StringField(required=True)
     field.set_value(12)
     self.assertRaises(SerializerFieldValueError, field.validate)
コード例 #26
0
class RecursionRefSerializerOne(Serializer):
    name = StringField()
    sers = ListSerializerField('RecursionRefSerializerTwo',
                               exclude=['ser'],
                               required=False)
コード例 #27
0
 class LazyFieldNestSerializer(Serializer):
     name = StringField(required=True)
     id = IntegerField(required=True, identity=True)
コード例 #28
0
 class UnknownTestSerialzer(Serializer):
     street = StringField(required=True)
コード例 #29
0
 class TestSerializer(Serializer):
     _type = TypeField('test_object')
     street = StringField(required=True)
コード例 #30
0
 class NestKnownTestSerialzer(Serializer):
     street = StringField(required=True)
     city = StringField(required=True)
コード例 #31
0
class TestSerializer(Serializer):
    name = StringField(required=True)
    number = IntegerField(required=True, max_value=10, min_value=5)
コード例 #32
0
ファイル: field_tests.py プロジェクト: MRigal/aserializer
 def test_set_value(self):
     field = StringField(required=True)
     field.set_value('string')
     field.validate()
     self.assertEqual(field.to_python(), 'string')
     self.assertEqual(field.to_native(), 'string')
コード例 #33
0
 def test_validate_raises(self):
     field = StringField(required=True)
     field.set_value(12)
     self.assertRaises(SerializerFieldValueError, field.validate)