Skip to main content

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

AttributeTypeDescription
path_typeLiteral['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

NameTypeDescription
pathPathThe filesystem path to be checked for file existence.
_core_schema.ValidationInfoPydantic validation context metadata (unused).

Returns

TypeDescription
PathThe 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

NameTypeDescription
pathPathThe filesystem path to be checked for socket status.
_core_schema.ValidationInfoPydantic validation context metadata (unused).

Returns

TypeDescription
PathThe 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

NameTypeDescription
pathPathThe filesystem path to be checked for directory existence.
_core_schema.ValidationInfoPydantic validation context metadata (unused).

Returns

TypeDescription
PathThe 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

NameTypeDescription
pathPathThe filesystem path intended for a new file or directory.
_core_schema.ValidationInfoPydantic validation context metadata (unused).

Returns

TypeDescription
PathThe validated Path object representing a non-existent file in an existing directory.