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

Implement 'humanize_error' for voluptuous' validation #3198

Closed
jeff1evesque opened this issue Feb 3, 2018 · 5 comments
Closed

Implement 'humanize_error' for voluptuous' validation #3198

jeff1evesque opened this issue Feb 3, 2018 · 5 comments
Labels
Milestone

Comments

@jeff1evesque
Copy link
Owner

jeff1evesque commented Feb 3, 2018

We need to implement MultipleInvalid for our voluptuous validation, since we are currently implementing the except Exception, error case, which only generates a single error, when many could be possible:

root@trusty64:/home/vagrant# cat test.py
from voluptuous import Schema, Required, All, Length, Range, MultipleInvalid

schema = Schema({
    Required('q'): All(str, Length(min=1)),
    Required('per_page'): All(int, Range(min=1, max=20)),
    Required('page'): All(int, Range(min=0)),
})


print('1: ==============================================')

try:
    schema({})
except MultipleInvalid as e:
    print e.errors

print('2: ==============================================')

try:
    schema({})
except Exception, error:
    print error

print('3: ==============================================')

try:
    schema({'q': 123})
except MultipleInvalid as e:
    print e.errors

print('4: ==============================================')

try:
    schema({'q': 123})
except Exception, error:
    print error
root@trusty64:/home/vagrant#
root@trusty64:/home/vagrant#
root@trusty64:/home/vagrant#
root@trusty64:/home/vagrant# python test.py
1: ==============================================
[RequiredFieldInvalid('required key not provided',), RequiredFieldInvalid('required key not provided',), RequiredFieldInvalid('required key not provided',)]
2: ==============================================
required key not provided @ data['q']
3: ==============================================
[TypeInvalid('expected str',), RequiredFieldInvalid('required key not provided',), RequiredFieldInvalid('required key not provided',)]
4: ==============================================
expected str for dictionary value @ data['q']
root@trusty64:/home/vagrant#
@jeff1evesque jeff1evesque added this to the 0.7 milestone Feb 3, 2018
@ghost
Copy link

ghost commented Feb 3, 2018

Your issue doesn't follow our guidelines. Please fix the following:

  • Issue title must be shorter than 55 characters (?)

Click here for details.

Thank you! 🙏

This comment was made by GitMagic – Magically enforcing your contribution guidelines.

@jeff1evesque jeff1evesque changed the title Implement 'MultipleInvalid' with 'voluptuous' validation Implement 'MultipleInvalid' for voluptuous' validation Feb 3, 2018
@ghost
Copy link

ghost commented Feb 3, 2018

Thank you, everything looks good now! :bowtie:

@jeff1evesque
Copy link
Owner Author

jeff1evesque commented Feb 3, 2018

The humanize_error is more useful, since it provides the corresponding error(s):

root@trusty64:/home/vagrant# cat test.py
from voluptuous import Schema, Required, All, Length, Range
from voluptuous.humanize import humanize_error

schema = Schema({
    Required('q'): All(str, Length(min=1)),
    Required('per_page'): All(int, Range(min=1, max=20)),
    Required('page'): All(int, Range(min=0)),
})

try:
    data = {'q': 123}
    schema(data)
except Exception as e:
    print humanize_error(data, e)
root@trusty64:/home/vagrant#
root@trusty64:/home/vagrant#
root@trusty64:/home/vagrant#
root@trusty64:/home/vagrant# python test.py
expected str for dictionary value @ data['q']. Got 123
required key not provided @ data['page']. Got None
required key not provided @ data['per_page']. Got None

@jeff1evesque jeff1evesque changed the title Implement 'MultipleInvalid' for voluptuous' validation Implement 'humanize_error' for voluptuous' validation Feb 3, 2018
@jeff1evesque
Copy link
Owner Author

For the dataset validation, we should include the corresponding dependent-variable value, with the corresponding error, since a given dataset could have multiple dependent-variables, each with multiple nested independent variables. Without being explicit, we may not know if an independent variable error is associated with a given dependent-variable.

@jeff1evesque
Copy link
Owner Author

We actually decided to implement validate_with_humanized_errors, so that errors are formatted into verbose syntax, rather than converting the corresponding error messages into verbose syntax.

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

No branches or pull requests

1 participant