Marshmallow makes no mistakes

If I used Marshmallow to create such a circuit:

class TempSchema(Schema):
    id = fields.Int()
    email = fields.Str(required=True,
                   validate=validate.Email(error='Not a valid email address'))
    password = fields.Str(required=True,
                      validate=[validate.Length(min=6, max=36)],
                      load_only=True)

and then I do something like:

temp = TempSchema()
temp.dumps({'email':123})

I expect an error, but I get:

MarshalResult(data='{"email": "123"}', errors={})

Why is this or something else not showing up as an error?

+4
source share
1 answer

Validation is only performed during deserialization (using Schema.load), not serialization ( Schema.dump).

data, errors = schema.load({'email': '123'})
print(errors)
# {'email': ['Not a valid email address'], 'password': ['Missing data for required field.']}

If you do not need deserialized data, you can use Schema.validate.

errors = schema.validate({'email': '123'})
print(errors)
# {'email': ['Not a valid email address'], 'password': ['Missing data for required field.']}
+7
source

Source: https://habr.com/ru/post/1619285/


All Articles