Skip to content

Commit

Permalink
Cleaner error for invalid input to Path fields (#6903)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jul 27, 2023
1 parent 1c02b19 commit eab48b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
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

0 comments on commit eab48b1

Please sign in to comment.