Skip to content

arganic.validators

Dir

Bases: Validator

Directory Validator.

validate

validate(value) -> bool

Test the existence of a directory according to the path provided.

Parameters:

  • value

    Path to the directory that must exist on the file system.

Returns:

  • bool

    True if the directory exists.

Raises:

  • FileNotFoundError

    If the directory does not exist on the file system.

Examples:

from arganic.arguments import function_arguments, Argument
from arganic.validators import Dir


@function_arguments(
    dir=Argument(
        type=str,
        validator=Dir()
    )
)
def dir_handler(*args, **kwargs) -> str:
    print(dir_handler.arguments.get('dir'))
    return dir_handler.arguments.get('dir')


# Validation
dir_handler(dir='tests/examples/validate_dir')

Email

Bases: Validator

Email address Validator.

validate

validate(value) -> bool

Validates the syntax of an email address.

Parameters:

  • value

    Email address whose syntax must be checked.

Returns:

  • bool

    If the value provided is a correct email address format.

Raises:

  • ValueError

    If the value provided is not a valid email address.

Examples:

from arganic.validators import Email
from arganic.arguments import function_arguments, Argument


@function_arguments(
    email=Argument(
        type=str,
        validator=Email()
    )
)
def email_handler(*args, **kwargs) -> str:
    print(email_handler.arguments.get('email'))
    return email_handler.arguments.get('email')


# Validation
email_handler(email='example@example.com')

File

Bases: Validator

File Validator

validate

validate(value) -> bool

Test the existence of a file according to the path provided.

Parameters:

  • value

    Path to the file that must exist on the file system.

Returns:

  • bool

    True if the file exists.

Raises:

  • FileNotFoundError

    If the file does not exist on the file system.

Examples:

from arganic.arguments import function_arguments, Argument
from arganic.validators import File


@function_arguments(
    file=Argument(
        type=str,
        validator=File()
    )
)
def file_handler(*args, **kwargs) -> str:
    print(file_handler.arguments.get('file'))
    return file_handler.arguments.get('file')


# Validation
file_handler(file='tests/examples/validate_dir/validate_file.txt')

MaxLength

Bases: Validator

Maximum length validator.

__init__

__init__(max_length: int) -> None

Max length Validator constructor.

Parameters:

  • max_length (int) –

    The maximum length that the values to validate must not exceed.

validate

validate(value) -> bool

Validates a value whose maximum length must not be greater than the value specified in the validator constructor.

The value must be of a type supporting the builtin len() Python function.

Parameters:

  • value

    The value to validate.

Returns:

  • bool

    True if the validation succeeded.

Raises:

  • TypeError

    If the length of the value is longer than the specified maximum length.

Examples:

from arganic.validators import MaxLength
from arganic.arguments import function_arguments, Argument


@function_arguments(
    max_len=Argument(
        type=str,
        validator=MaxLength(10)
    )
)
def max_len_10_handler(*args, **kwargs) -> str:
    print(max_len_10_handler.arguments.get('max_len'))
    return max_len_10_handler.arguments.get('max_len')


@function_arguments(
    max_len=Argument(
        type=str,
        validator=MaxLength(1)
    )
)
def max_len_1_handler(*args, **kwargs) -> str:
    print(max_len_1_handler.arguments.get('max_len'))
    return max_len_1_handler.arguments.get('max_len')


# Validation
max_len_10_handler(max_len='example')
# Validation
max_len_1_handler(max_len='e')

MinLength

Bases: Validator

Minimum length validator.

__init__

__init__(min_length: int) -> None

Max length Validator constructor.

Parameters:

  • min_length (int) –

    The minimum length that the value must be.

validate

validate(value) -> bool

Verifies that the provided value have a length must be at least the minimum value given in the validator constructor.

The value must be of a type supporting the builtin len() Python function.

Parameters:

  • value

    The value to validate.

Returns:

  • bool

    True if the validation succeeded.

Raises:

  • TypeError

    If the length of the value is shorter than the specified minimum length.

Examples:

from arganic.validators import MinLength
from arganic.arguments import function_arguments, Argument


@function_arguments(
    min_len=Argument(
        type=str,
        validator=MinLength(1)
    )
)
def min_len_1_handler(*args, **kwargs) -> str:
    return min_len_1_handler.arguments.get('min_len')


@function_arguments(
    min_len=Argument(
        type=str,
        validator=MinLength(10)
    )
)
def min_len_10_handler(*args, **kwargs) -> str:
    print(min_len_10_handler.arguments.get('min_len'))
    return min_len_10_handler.arguments.get('min_len')


# Validation
min_len_1_handler(min_len='example')
# Validation
min_len_1_handler(min_len='example1234')

Url

Bases: Validator

URL Validator.

validate

validate(value) -> bool

Validate if an URL is well formatted. supported protocols: http, https, ftp, ftps.

Parameters:

  • value

    The value of the URL to validate.

Returns:

  • bool

    True if the URL is well formatted.

Raises:

  • ValueError

    If the provided value is an invalid Url.

Examples:

from arganic.validators import Url
from arganic.arguments import function_arguments, Argument


@function_arguments(
    url=Argument(
        type=str,
        validator=Url()
    )
)
def url_handler(*args, **kwargs) -> str:
    print(url_handler.arguments.get('url'))
    return url_handler.arguments.get('url')


# Validation
url_handler(url='https://www.example.com')

Validator

Bases: ABC

Base class for validators.

It's possible to define your own validators by extending this class.

Examples:

Example of a custom validator.

from arganic.arguments import function_arguments, Argument
from arganic.validators import Validator


class CityValidator(Validator):
    """
    Custom validator class.
    """
    def validate(self, value) -> bool:
        if value in ('Geneva', 'Paris', 'Lyon', 'Madrid'):
            return True
        raise ValueError('Invalid value')


@function_arguments(
    start=Argument(
        type=str,
        validator=CityValidator()
    ),
    destination=Argument(
        type=str,
        validator=CityValidator()
    )
)
def drive(*args, **kwargs) -> None:
    print('Drive')
    print('start', drive.arguments.get('start'))
    print('destination', drive.arguments.get('destination'))
    return drive.arguments.values


drive(start='Geneva', destination='Paris')
drive(start='Lyon', destination='Geneva')
drive(start='Madrid', destination='Paris')

validate abstractmethod

validate(value) -> bool

Override this method on inherited classes.

Parameters:

  • value

    The value to validate.

Returns:

  • bool

    The validate() methods needs to return True if the validation pass or raises an exception if the validation fails.