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

skip_missing is not working for fields of type String #71

Closed
malexer opened this issue Nov 28, 2014 · 1 comment
Closed

skip_missing is not working for fields of type String #71

malexer opened this issue Nov 28, 2014 · 1 comment

Comments

@malexer
Copy link
Contributor

malexer commented Nov 28, 2014

It seems that skip_missing option is working only if field will have None value.
In case if input dict will not have some key which is declared as String type we will have and empty string in result.

Sample code:

class UserSchema(Schema):
    first = String()
    last = String()

    class Meta:
        skip_missing = True


test_data = dict(
    first='Name',
)

sch = UserSchema()
print sch.dump(test_data)
@sloria
Copy link
Member

sloria commented Nov 29, 2014

Only key:value pairs for which value=None are skipped. This is intentional. marshmallow does not make assumptions about whether a falsy value--such as an empty string--is a "null" value or if it is a valid, user-provided value. We would not, for example, want to skip 0 for a Number field.

For now, you will have to explicitly set a String field's default to None for it to be skipped.

A more long-term solution would be to allow Field classes to define a set of null values that are skippable:

class String(Field):
    # values that are skipped when skip_missing=True
    NULL_VALUES = set([None, ''])

class List(Field):
    # can't use a set, because lists are unhashable
    NULL_VALUES = (None, [], tuple())

Thoughts?

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