mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Make .json() work for EnumError
This commit is contained in:
+1
-1
@@ -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}'
|
||||
|
||||
|
||||
|
||||
@@ -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
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user