Skip to content
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

Cleaner error for invalid input to Path fields #6903

Merged
merged 1 commit into from Jul 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pydantic/_internal/_std_types_schema.py
Expand Up @@ -415,9 +415,9 @@ def path_validator(input_value: str) -> os.PathLike[Any]:
instance_schema,
core_schema.no_info_after_validator_function(path_validator, constrained_str_schema),
],
# custom_error_type='path_type',
# custom_error_message='Input is not a valid path',
# strict=True,
custom_error_type='path_type',
custom_error_message='Input is not a valid path',
strict=True,
),
strict_schema=instance_schema,
serialization=core_schema.to_string_ser_schema(),
Expand Down
21 changes: 8 additions & 13 deletions tests/test_types.py
Expand Up @@ -3210,19 +3210,14 @@ class Model(BaseModel):
Model(foo=123)
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'is_instance_of',
'loc': ('foo', 'json-or-python[json=function-after[path_validator(), str],python=is-instance[Path]]'),
'msg': 'Input should be an instance of Path',
'input': 123,
'ctx': {'class': 'Path'},
},
{
'type': 'string_type',
'loc': ('foo', 'function-after[path_validator(), str]'),
'msg': 'Input should be a valid string',
'input': 123,
},
{'type': 'path_type', 'loc': ('foo',), 'msg': 'Input is not a valid path', 'input': 123}
]

with pytest.raises(ValidationError) as exc_info:
Model(foo=None)
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{'type': 'path_type', 'loc': ('foo',), 'msg': 'Input is not a valid path', 'input': None}
]


Expand Down