Skip to content

Commit

Permalink
Improve error message for NameEmail (#6939)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Aug 16, 2023
1 parent f4e96d8 commit 7908718
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pydantic/networks.py
Expand Up @@ -247,7 +247,11 @@ def __get_pydantic_core_schema__(
import_email_validator()
return core_schema.general_after_validator_function(
cls._validate,
core_schema.union_schema([core_schema.is_instance_schema(cls), core_schema.str_schema()]),
core_schema.union_schema(
[core_schema.is_instance_schema(cls), core_schema.str_schema()],
custom_error_type='name_email_type',
custom_error_message='Input is not a valid NameEmail',
),
serialization=core_schema.to_string_ser_schema(),
)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_networks.py
Expand Up @@ -834,3 +834,9 @@ class Model(BaseModel):
assert str(Model(v='foo bar <foobaR@example.com>').v) == 'foo bar <foobaR@example.com>'
assert NameEmail('foo bar', 'foobaR@example.com') == NameEmail('foo bar', 'foobaR@example.com')
assert NameEmail('foo bar', 'foobaR@example.com') != NameEmail('foo bar', 'different@example.com')

with pytest.raises(ValidationError) as exc_info:
Model(v=1)
assert exc_info.value.errors() == [
{'input': 1, 'loc': ('v',), 'msg': 'Input is not a valid NameEmail', 'type': 'name_email_type'}
]

0 comments on commit 7908718

Please sign in to comment.