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

Support for read-only fields #61

Closed
pmdarrow opened this issue Nov 18, 2014 · 7 comments
Closed

Support for read-only fields #61

pmdarrow opened this issue Nov 18, 2014 · 7 comments
Milestone

Comments

@pmdarrow
Copy link

It would be nice to have the ability to mark fields as read-only. When deserializing, validation should fail if field(s) marked as read-only are present in the target dictionary.

@sloria
Copy link
Member

sloria commented Nov 19, 2014

You could do something like:

from marshmallow import fields, Schema, ValidationError

def read_only(val):
    if val is not fields.missing:
        raise ValidationError('Cannot pass a read-only field.')


class AlbumSchema(Schema):
    published = fields.Date(validate=read_only)
    title = fields.Str()


s = AlbumSchema()
result = s.load({'published': '2014-11-18'})
print(result.errors)  # {'published': ['Cannot pass a read-only field.']}

@pmdarrow
Copy link
Author

This works, but what do you think about making readonly an optional keyword argument, just like required is? Django REST Framework, Cerberus and a number of other serilzation / validation libraries implement it this way and I personally find it very convinient.

@rozenm
Copy link

rozenm commented Nov 19, 2014

I looked for a read only attribute as well.
Used to working with Django Rest FrameWork
and the read only flag is quite handy.

@sloria
Copy link
Member

sloria commented Nov 21, 2014

I will consider adding a read-only flag as a kwarg. Thank you for your input.

@philtay
Copy link
Contributor

philtay commented Dec 1, 2014

+1

@sloria
Copy link
Member

sloria commented Jan 17, 2015

I am leaning towards adding this feature, but I will probably call the parameter dump_only or something similar, for consistency with load_only as described in #87. This would also avoid confusion over what "read" and "write" mean (keep in mind that marshmallow isn't meant to do any sort of "writing").

Thoughts?

@sloria sloria added this to the 2.0-a milestone Jan 17, 2015
@sloria
Copy link
Member

sloria commented Jan 25, 2015

Added in #131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants