Marshmallow not giving errors

If I used Marshmallow to create a schema like this:

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’d expect an error but I get:

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

Why isn’t this or anything else showing up as an error?

Best answer

Validation only happens on 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 don’t need the 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.']}