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

Provide positional information in errors dictionary when serializing a collection (many=True) #75

Closed
sloria opened this Issue Dec 4, 2014 · 2 comments

Comments

Projects
None yet
1 participant
@sloria
Member

sloria commented Dec 4, 2014

When validating a collection with invalid elements, the returned errors dictionary does not provide any information about the positions of invalid elements

Example:

def validate_age(age):
    if age < 21:
        raise ValidationError('Must be over 21')

class UserSchema(Schema):
    age = fields.Number(validate=validate_age)

in_data = [{'age': 24}, {'age': 19}]
data, errors = UserSchema(many=True).load(in_data)

Current

errors  # => {'age': ['Must be over 21']}

Possible solution

Provide positional information in a nested dictionary, keyed by index.

errors  # => {'age': {1: ['Must be over 21']}}

@sloria sloria added this to the 2.0 milestone Dec 4, 2014

sloria added a commit that referenced this issue Jan 13, 2015

Store index of invalid items when using many=True
See #75.
Still a WIP. Still needs more tests and docs. May also want to
add a config option to turn this behavior on/off.
@sloria

This comment has been minimized.

Member

sloria commented Jan 13, 2015

An alternative format to the one proposed above would be to key the errors dictionary by the indices of invalid items:

errors  # => {1: {'age': ['Must be over 21']}}

I've implemented this behavior in a09fa92, though it's not finalized. This feature is still up for discussion.

Another thought: We could add a class Meta option to turn this error indexing behavior on or off. Perhaps we default it to be turned off and release it in the next feature release (1.3) so that it can get some use before finalizing it in 2.0.

sloria added a commit that referenced this issue Feb 23, 2015

Store index of invalid items when using many=True
See #75.
Still a WIP. Still needs more tests and docs. May also want to
add a config option to turn this behavior on/off.
@sloria

This comment has been minimized.

Member

sloria commented Mar 1, 2015

Implemented in a3d3602

@sloria sloria closed this Mar 1, 2015

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