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

Raising validation errors for multiple field names in schema-scoped validators #118

Closed
ksesong opened this Issue Dec 27, 2014 · 3 comments

Comments

Projects
None yet
2 participants
@ksesong

ksesong commented Dec 27, 2014

Supposing a schema where only one field is required (i.e. all the others must be missing), I am using a schema validator to check this condition, as I need the state of the other fields. If there is no such non-null field, I want to raise an error for each field (as they are equally responsible). If I however raise the error, it stalls the whole process. I could raise a number of ValidationError via a list, but I can't assign these errors to different field names.

class only_one_field_present(object):
    def __init__(self, choices):
        self.choices = choices
    def run(self, schema, data):
        if not any([data[name] for name in self.choices]):
            # -- Raise ValidationError for all the fields names in choices...

class DummySchema(Schema):
    __validators__ = [only_one_field_present(['field_a', 'field_b']).run]
    field_a = fields.Field()
    field_b = fields.Field()

This is what I would like to get in the end:

errors = {
    'field_a': ['one_required'],
    'field_b': ['one_required']
}

@ksesong ksesong changed the title from Raising multiple validation error for multiple field names in schema-scoped validators to Raising validation errors for multiple field names in schema-scoped validators Dec 27, 2014

@sloria

This comment has been minimized.

Member

sloria commented Dec 31, 2014

I suppose we could allow multiple fields to be passed to a validation error.

error_fields = #... compute which fields failed validation
raise ValidationError('one_required', error_fields)

Would this meet your use case?

@ksesong

This comment has been minimized.

ksesong commented Dec 31, 2014

Yes.

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

WIP: Allow schema-level errors to be stored on multiple fields
see #118

Right now, this is a backwards-incompatible change to the
UnmarshallingError interface, because it is storing
`field` and `field_names` (plural) and not the singular
attributes.
@sloria

This comment has been minimized.

Member

sloria commented Jan 2, 2015

OK, this feature has been merged into dev.

The feature introduced a backwards-incompatible change to the UnmarshallingError interface, so this will probably be released in version 2.0.

You can still install the dev branch with:

pip install -U git+https://github.com/marshmallow-code/marshmallow.git@dev

@sloria sloria closed this Jan 2, 2015

@sloria sloria added the enhancement label Jan 2, 2015

@sloria sloria added this to the 2.0-a milestone Jan 2, 2015

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