PathType
This class provides specialized validation logic for filesystem paths within Pydantic models. It supports verifying specific path types such as files, directories, sockets, or non-existent new paths, and automatically updates the associated JSON schema with appropriate format metadata.
Attributes
| Attribute | Type | Description |
|---|---|---|
| path_type | Literal['file', 'dir', 'new', 'socket'] | Specifies the category of filesystem path to validate, determining whether the path must represent an existing file, directory, socket, or a non-existent new path. |
Methods
validate_file()
@classmethod
def validate_file(
path: Path,
_: core_schema.ValidationInfo
) - > Path
Validates that the provided path points to an existing file on the filesystem.
Parameters
| Name | Type | Description |
|---|---|---|
| path | Path | The filesystem path to be checked for file existence. |
| _ | core_schema.ValidationInfo | Pydantic validation context metadata (unused). |
Returns
| Type | Description |
|---|---|
Path | The validated Path object if it points to a file. |
validate_socket()
@classmethod
def validate_socket(
path: Path,
_: core_schema.ValidationInfo
) - > Path
Validates that the provided path points to a Unix domain socket.
Parameters
| Name | Type | Description |
|---|---|---|
| path | Path | The filesystem path to be checked for socket status. |
| _ | core_schema.ValidationInfo | Pydantic validation context metadata (unused). |
Returns
| Type | Description |
|---|---|
Path | The validated Path object if it points to a socket. |
validate_directory()
@classmethod
def validate_directory(
path: Path,
_: core_schema.ValidationInfo
) - > Path
Validates that the provided path points to an existing directory.
Parameters
| Name | Type | Description |
|---|---|---|
| path | Path | The filesystem path to be checked for directory existence. |
| _ | core_schema.ValidationInfo | Pydantic validation context metadata (unused). |
Returns
| Type | Description |
|---|---|
Path | The validated Path object if it points to a directory. |
validate_new()
@classmethod
def validate_new(
path: Path,
_: core_schema.ValidationInfo
) - > Path
Ensures the path does not yet exist but its parent directory does, making it valid for new file creation.
Parameters
| Name | Type | Description |
|---|---|---|
| path | Path | The filesystem path intended for a new file or directory. |
| _ | core_schema.ValidationInfo | Pydantic validation context metadata (unused). |
Returns
| Type | Description |
|---|---|
Path | The validated Path object representing a non-existent file in an existing directory. |