pydantic / pydantic-extra-types

Extra Pydantic types.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

More convenient pathlib.Path types

Ravencentric opened this issue · comments

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Working with Paths is fairly common and I usually end up writing my own field validator over and over again because the type I want is missing. I would like to see the Path types in pydantic extended with some more potentially useful types:

  1. ExistingPath - Union[FilePath, DirectoryPath]
  2. ResolvedFilePath - A file that get's resolved (pathlib.Path.expanduser().resolve()) by pydantic before returning
  3. ResolvedDirectoryPath - A directory that get's resolved (pathlib.Path.expanduser().resolve()) by pydantic before returning
  4. ResolvedPath - Union[ResolvedFilePath, ResolvedDirectoryPath]

My current solution is to just write a field validator:

    @field_validator("path_a", "path_b")
    @classmethod
    def resolve_path(cls, path: Path) -> Path:
            """Resolve all given Path fields"""
            return path.expanduser().resolve()

Affected Components

@Ravencentric,

Thanks for the feature request. I'm sure many people would appreciate extended support for these types. The right place for these, though, is in pydantic-extra-types. I'll transfer this issue over there 👍.

Makes sense, thank you!