Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 967
Rob Avatar asked Dec 10 '15 00:12

Rob


1 Answers

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.']}
like image 153
Steve L Avatar answered Sep 20 '22 13:09

Steve L