This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Description Would it be possible for the Choice class to include a list of acceptable choices in the validation error output?
Here is a snippet doing this using subclassing.
import typesystem
class MyChoice (typesystem .fields .Choice ):
def __init__ (self , * , choices = None , ** kwargs ):
super ().__init__ (choices = choices , ** kwargs )
self .single_choices = [x [0 ] for x in self .choices ]
self .errors ["choice" ] = "Not a valid choice. Please choose: {single_choices}"
field = MyChoice (choices = [('foo' , 'foo' )])
field .validate ('bar' )
This will raise the following error message (this is an ipython notebook error message, but hopefully demonstrates ok):
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-65-854f15494f03> in <module>
8 field = MyChoice(choices=[('foo', 'foo')])
9
---> 10 field.validate('bar')
~/Documents/entanglement/venv/lib/python3.7/site-packages/typesystem/fields.py in validate(self, value, strict)
384 return None
385 raise self.validation_error("required")
--> 386 raise self.validation_error("choice")
387 return value
388
ValidationError: Not a valid choice. Please choose: ['foo']
Reactions are currently unavailable
Would it be possible for the
Choiceclass to include a list of acceptable choices in the validation error output?Here is a snippet doing this using subclassing.
This will raise the following error message (this is an ipython notebook error message, but hopefully demonstrates ok):