Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email Field validation not working for Schema.dump() #53

Closed
h0st1le opened this issue Oct 21, 2014 · 3 comments
Closed

Email Field validation not working for Schema.dump() #53

h0st1le opened this issue Oct 21, 2014 · 3 comments

Comments

@h0st1le
Copy link

h0st1le commented Oct 21, 2014

Email field type validation does not appear to work when using Schema.dump() but works fine for Schema.load(). Working example included below:

from datetime import datetime
from marshmallow import Schema, fields, pprint

# model
class Person(object):
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.date_born = datetime.now()

# serializer schema
class PersonSchema(Schema):
    name = fields.String()
    email = fields.Email()
    date_born = fields.DateTime()

person = Person(name='Guido van Rossum', email='invalid-email')
schema = PersonSchema()
dumps = schema.dump(person)

print '--DUMPS--'
pprint(dumps.data)
pprint(dumps.errors)

loads = schema.load({'name': 'Guido van Rossum', 'email': 'invalid-email'})

print '--LOADS--'
pprint(loads.data)
pprint(loads.errors)
@sloria
Copy link
Member

sloria commented Oct 21, 2014

Email and URL fields have built-in "user validators" (stored in each field's validators attribute. User validators are only applied during deserialization, i.e. when calling Schema#load. Schema#dump only validates against field types (e.g. a valid integer is passed to fields.Int).

See #45 (comment) and #47 (comment) for related discussion.

That said, one could argue that the email format validation defines fields.Email's "type" and that it should be validated on serialization. Perhaps this would make fields.Email and fields.URL more consistent with the behavior of the other fields.

@h0st1le
Copy link
Author

h0st1le commented Oct 24, 2014

Thanks for the clarification, the related discussions also helped to better understand the reasoning behind this. That being said I do still like the idea of the fields.Email and fields.URL "type" being used for validation on serialization. As pointed out it would make their behavior more consistent with that of other fields. At the very least maybe an addition to the documentation that points out the current behavior?

@sloria sloria closed this as completed in e9e49cd Oct 25, 2014
@sloria
Copy link
Member

sloria commented Oct 25, 2014

I have decided to restore validation upon serialization for Email and URL fields. Thank you for raising the issue!

@sloria sloria added this to the 1.0.0 stable release milestone Oct 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants