Make .json() work for EnumError

This commit is contained in:
David Montague
2019-07-30 11:38:25 -07:00
parent b58f31dfd9
commit a028687f98
3 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -87,7 +87,7 @@ class UrlRegexError(PydanticValueError):
class EnumError(PydanticTypeError):
def __str__(self) -> str:
permitted = ', '.join(repr(v.value) for v in self.ctx['enum_type']) # type: ignore
permitted = ', '.join(repr(v.value) for v in self.ctx['enum_values']) # type: ignore
return f'value is not a valid enumeration member; permitted: {permitted}'
+2 -1
View File
@@ -222,7 +222,8 @@ def enum_validator(v: Any, field: 'Field', config: 'BaseConfig') -> Enum:
try:
enum_v = field.type_(v)
except ValueError:
raise errors.EnumError(enum_type=field.type_)
# field.type_ should be an enum, so will be iterable
raise errors.EnumError(enum_values=list(field.type_)) # type: ignore
return enum_v.value if config.use_enum_values else enum_v
+2 -1
View File
@@ -477,9 +477,10 @@ def test_enum_fails():
'loc': ('tool',),
'msg': 'value is not a valid enumeration member; permitted: 1, 2',
'type': 'type_error.enum',
'ctx': {'enum_type': ToolEnum},
'ctx': {'enum_values': [ToolEnum.spanner, ToolEnum.wrench]},
}
]
assert len(exc_info.value.json()) == 217
def test_int_enum_successful_for_str_int():