Skip to content

Commit 1a6dd5f

Browse files
hramezaniadriangb
authored andcommitted
Add enum error type (#751)
1 parent 29f46b0 commit 1a6dd5f

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

python/pydantic_core/core_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,6 +3811,7 @@ def definition_reference_schema(
38113811
'string_too_short',
38123812
'string_too_long',
38133813
'string_pattern_mismatch',
3814+
'enum',
38143815
'dict_type',
38153816
'mapping_type',
38163817
'list_type',

src/errors/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ pub enum ErrorType {
159159
pattern: String,
160160
},
161161
// ---------------------
162+
// enum errors
163+
Enum {
164+
expected: String,
165+
},
166+
// ---------------------
162167
// dict errors
163168
DictType,
164169
MappingType {
@@ -415,6 +420,7 @@ impl ErrorType {
415420
Self::IterationError { .. } => extract_context!(IterationError, ctx, error: String),
416421
Self::StringTooShort { .. } => extract_context!(StringTooShort, ctx, min_length: usize),
417422
Self::StringTooLong { .. } => extract_context!(StringTooLong, ctx, max_length: usize),
423+
Self::Enum { .. } => extract_context!(Enum, ctx, expected: String),
418424
Self::StringPatternMismatch { .. } => extract_context!(StringPatternMismatch, ctx, pattern: String),
419425
Self::MappingType { .. } => extract_context!(Cow::Owned, MappingType, ctx, error: String),
420426
Self::BytesTooShort { .. } => extract_context!(BytesTooShort, ctx, min_length: usize),
@@ -492,6 +498,7 @@ impl ErrorType {
492498
Self::StringTooShort {..} => "String should have at least {min_length} characters",
493499
Self::StringTooLong {..} => "String should have at most {max_length} characters",
494500
Self::StringPatternMismatch {..} => "String should match pattern '{pattern}'",
501+
Self::Enum {..} => "Input should be {expected}",
495502
Self::DictType => "Input should be a valid dictionary",
496503
Self::MappingType {..} => "Input should be a valid mapping, error: {error}",
497504
Self::ListType => "Input should be a valid list",
@@ -628,6 +635,7 @@ impl ErrorType {
628635
Self::StringTooShort { min_length } => to_string_render!(tmpl, min_length),
629636
Self::StringTooLong { max_length } => to_string_render!(tmpl, max_length),
630637
Self::StringPatternMismatch { pattern } => render!(tmpl, pattern),
638+
Self::Enum { expected } => to_string_render!(tmpl, expected),
631639
Self::MappingType { error } => render!(tmpl, error),
632640
Self::BytesTooShort { min_length } => to_string_render!(tmpl, min_length),
633641
Self::BytesTooLong { max_length } => to_string_render!(tmpl, max_length),
@@ -697,6 +705,7 @@ impl ErrorType {
697705
Self::StringTooShort { min_length } => py_dict!(py, min_length),
698706
Self::StringTooLong { max_length } => py_dict!(py, max_length),
699707
Self::StringPatternMismatch { pattern } => py_dict!(py, pattern),
708+
Self::Enum { expected } => py_dict!(py, expected),
700709
Self::MappingType { error } => py_dict!(py, error),
701710
Self::BytesTooShort { min_length } => py_dict!(py, min_length),
702711
Self::BytesTooLong { max_length } => py_dict!(py, max_length),

tests/test_errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def f(input_value, info):
192192
('invalid_key', 'Keys should be strings', None),
193193
('get_attribute_error', 'Error extracting attribute: foo', {'error': 'foo'}),
194194
('none_required', 'Input should be None', None),
195+
('enum', 'Input should be foo', {'expected': 'foo'}),
195196
('greater_than', 'Input should be greater than 42.1', {'gt': 42.1}),
196197
('greater_than', 'Input should be greater than 42.1', {'gt': '42.1'}),
197198
('greater_than', 'Input should be greater than 2020-01-01', {'gt': '2020-01-01'}),

0 commit comments

Comments
 (0)