Skip to content

Add enum error type #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3811,6 +3811,7 @@ def definition_reference_schema(
'string_too_short',
'string_too_long',
'string_pattern_mismatch',
'enum',
'dict_type',
'mapping_type',
'list_type',
Expand Down
9 changes: 9 additions & 0 deletions src/errors/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ pub enum ErrorType {
pattern: String,
},
// ---------------------
// enum errors
Enum {
expected: String,
},
// ---------------------
// dict errors
DictType,
MappingType {
Expand Down Expand Up @@ -415,6 +420,7 @@ impl ErrorType {
Self::IterationError { .. } => extract_context!(IterationError, ctx, error: String),
Self::StringTooShort { .. } => extract_context!(StringTooShort, ctx, min_length: usize),
Self::StringTooLong { .. } => extract_context!(StringTooLong, ctx, max_length: usize),
Self::Enum { .. } => extract_context!(Enum, ctx, expected: String),
Self::StringPatternMismatch { .. } => extract_context!(StringPatternMismatch, ctx, pattern: String),
Self::MappingType { .. } => extract_context!(Cow::Owned, MappingType, ctx, error: String),
Self::BytesTooShort { .. } => extract_context!(BytesTooShort, ctx, min_length: usize),
Expand Down Expand Up @@ -492,6 +498,7 @@ impl ErrorType {
Self::StringTooShort {..} => "String should have at least {min_length} characters",
Self::StringTooLong {..} => "String should have at most {max_length} characters",
Self::StringPatternMismatch {..} => "String should match pattern '{pattern}'",
Self::Enum {..} => "Input should be {expected}",
Self::DictType => "Input should be a valid dictionary",
Self::MappingType {..} => "Input should be a valid mapping, error: {error}",
Self::ListType => "Input should be a valid list",
Expand Down Expand Up @@ -628,6 +635,7 @@ impl ErrorType {
Self::StringTooShort { min_length } => to_string_render!(tmpl, min_length),
Self::StringTooLong { max_length } => to_string_render!(tmpl, max_length),
Self::StringPatternMismatch { pattern } => render!(tmpl, pattern),
Self::Enum { expected } => to_string_render!(tmpl, expected),
Self::MappingType { error } => render!(tmpl, error),
Self::BytesTooShort { min_length } => to_string_render!(tmpl, min_length),
Self::BytesTooLong { max_length } => to_string_render!(tmpl, max_length),
Expand Down Expand Up @@ -687,6 +695,7 @@ impl ErrorType {
Self::StringTooShort { min_length } => py_dict!(py, min_length),
Self::StringTooLong { max_length } => py_dict!(py, max_length),
Self::StringPatternMismatch { pattern } => py_dict!(py, pattern),
Self::Enum { expected } => py_dict!(py, expected),
Self::MappingType { error } => py_dict!(py, error),
Self::BytesTooShort { min_length } => py_dict!(py, min_length),
Self::BytesTooLong { max_length } => py_dict!(py, max_length),
Expand Down
1 change: 1 addition & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def f(input_value, info):
('invalid_key', 'Keys should be strings', None),
('get_attribute_error', 'Error extracting attribute: foo', {'error': 'foo'}),
('none_required', 'Input should be None', None),
('enum', 'Input should be foo', {'expected': 'foo'}),
('greater_than', 'Input should be greater than 42.1', {'gt': 42.1}),
('greater_than', 'Input should be greater than 42.1', {'gt': '42.1'}),
('greater_than', 'Input should be greater than 2020-01-01', {'gt': '2020-01-01'}),
Expand Down