spy16 / pyschemes

PySchemes is a library for validating data structures in python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple Choices Validation error message could be clearer

crdoconnor opened this issue · comments

>>> 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".

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.

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?

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.