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

Simple Choices Validation error message could be clearer #5

Open
crdoconnor opened this issue Nov 7, 2017 · 3 comments
Open

Simple Choices Validation error message could be clearer #5

crdoconnor opened this issue Nov 7, 2017 · 3 comments

Comments

@crdoconnor
Copy link

crdoconnor commented Nov 7, 2017

>>> choices = validators.Any(["choiceA", "choiceB", "choiceC"])

>>> Scheme(choices).validate("choiceA")
'choiceA'

Instead of:


>>> Scheme(choices).validate("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: value did not pass any validation

This would be clearer


>>> Scheme(choices).validate("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: value was not one of "choiceA", "choiceB", "choiceC".
@spy16
Copy link
Owner

spy16 commented Nov 8, 2017

Any validator is a generic validator (Simple list of string choices is only one of the possibilites here). Following example demonstrates what i mean:

lambdas = validators.Any([lambda x: x > 100, lambda x: x < 0, "hello", {str: int}])

lambdas.validate( <value> )

In above example, the <value> will be validated without error in any of the following case:

  1. If value is an integer greater than 100
  2. If value is an integer less than 0
  3. If value is string "hello"
  4. If value is a dict with string keys and integer values

Printing out "value was not one of " followed by the above list would be cryptic since it would print out lambda functions and dicts in the error message

Simply put, the syntax of Any is Any([validator1, validator2,....]) and is not Any(["str1", "str2",...]).

Also, if the list of validators is very long, error message would end up being long too.

@crdoconnor
Copy link
Author

In which case something like::

Value was not one of "x > 100", "x < 0", "hello", "{str: int}"

Would be clearer.

(code inside lambdas can be extracted using the inspect module.)

Also, if the list of validators is very long, error message would end up being long too.

So truncate?

@spy16
Copy link
Owner

spy16 commented Nov 8, 2017

Yes both are possible and I too believe that the error message for Any validator would be better that way.. I did not get enough time to format it. So it is left as it is. That said, if someone adds this and creates a PR, I would happily merge it.

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

2 participants