pydantic / pydantic-extra-types

Extra Pydantic types.
MIT License
165 stars 45 forks source link

More convenient pathlib.Path types #149

Open Ravencentric opened 5 months ago

Ravencentric commented 5 months ago

Initial Checks

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

sydney-runkle commented 5 months ago

@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 👍.

Ravencentric commented 5 months ago

Makes sense, thank you!