Skip to content

Validator

Validator for files with configurable validation rules.

Authors

Hubert Michael Sanyoto (hubert.m.sanyoto@gdplabs.id)

CharacterCountValidator(max_character_length=500000, stop_on_failure=False, applicable_extensions=None)

Bases: BaseValidator

Validator for checking if the total character length of file content does not exceed a maximum limit.

Character length counting is currently supported for CSV files only.

Initialize the CharacterCountValidator.

Parameters:

Name Type Description Default
max_character_length int

The maximum allowed character length for the file. Default is 500,000 characters. It should be greater than 0.

500000
stop_on_failure bool

Whether to stop the validation process if this validator fails. Default is False.

False
applicable_extensions list[str] | None

The list of file extensions that this validator is applicable to. Default is None which means all extensions are applicable.

None

FileSizeValidator(max_file_size=10485760, stop_on_failure=True, applicable_extensions=None)

Bases: BaseValidator

Validator for checking if file size does not exceed a maximum limit.

Initialize the FileSizeValidator.

Parameters:

Name Type Description Default
max_file_size int

The maximum allowed size for the file in bytes. Default is 10,485,760 bytes (10MB). It should be greater than 0.

10485760
stop_on_failure bool

Whether to stop the validation process if this validator fails. Default is True.

True
applicable_extensions list[str] | None

The list of file extensions that this validator is applicable to. Default is None which means all extensions are applicable.

None

PageCountValidator(max_pages=100, stop_on_failure=False, applicable_extensions=None)

Bases: BaseValidator

Validator for checking if the number of pages in a file does not exceed a maximum limit.

Page counting is currently supported for PDF files only.

Initialize the PageCountValidator.

Parameters:

Name Type Description Default
max_pages int

The maximum allowed number of pages. A non-negative value enforces a limit. Default is 100 pages. It should be greater than 0.

100
stop_on_failure bool

Whether to stop the validation process if this validator fails. Default is False.

False
applicable_extensions list[str] | None

The list of file extensions that this validator is applicable to. Default is None which means all extensions are applicable.

None

PipelineValidator()

A pipeline for validating files against multiple validation rules.

This class provides a flexible way to validate files by chaining multiple BaseValidator instances. Each validator is applied sequentially, and validation behavior depends on the stop_on_failure setting of each validator.

Attributes:

Name Type Description
validators list[BaseValidator]

A list of BaseValidator instances to apply for file validation.

Initialize the PipelineValidator object.

add_validator(validator)

Add a validator to the validation pipeline.

Parameters:

Name Type Description Default
validator BaseValidator

The validator to add to the pipeline.

required

Returns:

Name Type Description
PipelineValidator PipelineValidator

The validation pipeline object for method chaining.

validate(file_validation_input)

Validate the file against all configured validation rules.

Parameters:

Name Type Description Default
file_validation_input ValidatorInput

The file validation input object to validate.

required

Returns:

Name Type Description
ValidatorResult ValidatorResult

A ValidatorResult object indicating success or failure with an appropriate message. If multiple validations fail, messages are combined with semicolons.