validators

Validators for command-line input.

A validator is a callable that takes a string as its single parameter and raises a ValidationError if the string is invalid according to its validation rules.

class promptpy.validators.CharacterValidator(valid: str | None = None, invalid: str | None = None, case_sensitive=False)[source]

Validates that all characters are or are not within a predefined list.

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import CharacterValidator, ValidationError
>>> v = CharacterValidator(valid='abcde')
>>> v('abc')
>>> v('xyz')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Invalid character(s)

>>> v = CharacterValidator(invalid='abcde')
>>> v('xyz')
>>> v('abc')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Invalid character(s)

>>> v = CharacterValidator(valid='abcde', case_sensitive=True)
>>> v('abc')
>>> v('ABC')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Invalid character(s)
Parameters:
  • valid (str) – All characters must be contained in this list (default=None)

  • invalid (str) – No character may be contained in this list (default=None)

  • case_sensitive (bool) – True for case_sensitive comparison (default=False)

class promptpy.validators.ChoiceValidator(choices: list[str], case_sensitive=False, accept_empty=True)[source]

Validates that an input string is one of a pre-defined list of choices.

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import ChoiceValidator, ValidationError
>>> v = ChoiceValidator(['hello', 'goodbye'])
>>> v('hello')
>>> v('HELLO')
>>> v('')
>>> v('ciao')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: That is not a valid option

>>> v = ChoiceValidator(['hello', 'goodbye'], case_sensitive=True)
>>> v('hello')
>>> v('HELLO')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: That is not a valid option

>>> v = ChoiceValidator(['hello', 'goodbye'], accept_empty=False)
>>> v('')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: That is not a valid option
Parameters:
  • choices (list[str]) – The string to validate must be an item in this list

  • case_sensitive (bool) – If True, the validation will use a case-sensitive comparison (default=False)

  • accept_empty (bool) – Whether the validator should accept the empty string (default=True)

class promptpy.validators.DateValidator(format: str, accept_empty=True)[source]

Validates that an input string is a valid date.

Pass a strptime format string in the constructor, the validator will check that the input string can be converted to a date using datetime.datetime.strptime().

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import DateValidator, ValidationError
>>> v = DateValidator('%d/%m/%Y')
>>> v('10/03/2022')
>>> v('xyz')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a valid date

>>> v('0/0/0')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a valid date

>>> v('')
>>> v = DateValidator('%d/%m/%Y', accept_empty=False)
>>> v('')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a valid date
Parameters:
  • format (str) – The format string used to parse the date

  • accept_empty (bool) – Whether the validator should accept the empty string (default=True)

class promptpy.validators.FloatValidator(min: float | None = None, max: float | None = None, accept_empty=True)[source]

Validates that an input string is a valid float.

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import FloatValidator, ValidationError
>>> v = FloatValidator()
>>> v('3')
>>> v('3.2')
>>> v('xyz')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a number

>>> v = FloatValidator(min=3.0)
>>> v('3.0')
>>> v('2.8')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Number should be 3.0 or greater

>>> v = FloatValidator(max=6.0)
>>> v('3.0')
>>> v('6.1')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Number should be 6.0 or less

>>> v = FloatValidator()
>>> v('')

>>> v = FloatValidator(accept_empty=False)
>>> v('')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a number
Parameters:
  • min (float) – The value cannot be lower than this (default=None)

  • max (float) – The value cannot be higher than this (default=None)

  • accept_empty (bool) – Whether the validator should accept the empty string (default=True)

class promptpy.validators.IntegerValidator(min: int | None = None, max: int | None = None, accept_empty=True)[source]

Validates that an input string is a valid integer.

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import IntegerValidator, ValidationError
>>> v = IntegerValidator()
>>> v('3')
>>> v('xyz')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a number

>>> v('3.2')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a whole number

>>> v = IntegerValidator(min=3)
>>> v('3')
>>> v('2')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Number should be 3 or greater

>>> v = IntegerValidator(max=6)
>>> v('3')
>>> v('7')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Number should be 6 or less

>>> v = IntegerValidator()
>>> v('')

>>> v = IntegerValidator(accept_empty=False)
>>> v('')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Not a number
Parameters:
  • min (int) – The value cannot be lower than this (default=None)

  • max (int) – The value cannot be higher than this (default=None)

  • accept_empty (bool) – Whether the validator should accept the empty string (default=True)

class promptpy.validators.Length(min=0, max=0, exact=0)[source]

Validates that an input string is of the correct length.

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import Length, ValidationError
>>> v = Length(min=2)
>>> v('abc')
>>> v('a')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Text too short

>>> v = Length(max=3)
>>> v('xyz')
>>> v('abcde')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Text too long

>>> v = Length(exact=3)
>>> v('abc')
>>> v('abcd')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Text must be 3 characters long
Parameters:
  • min (int) – The string to validate must contain at least this number of characters

  • max (int) – The string to validate cannot be longer than this number of characters

  • exact (int) – The string to validate must contain exactly this number of characters

class promptpy.validators.Unique(case_sensitive=False)[source]

Validates that the text contains no repeated characters.

To validate a string pass it as a parameter when calling the class instance. If validation fails it will raise a ValidationError.

>>> from promptpy.validators import Unique, ValidationError
>>> v = Unique()
>>> v('abc')
>>> v('abb')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Repeated letter 'b'

>>> v('aBb')
Traceback (most recent call last):
    ...
promptpy.validators.ValidationError: Repeated letter 'b'

>>> v = Unique(case_sensitive=True)
>>> v('aBb')
Parameters:

case_sensitive (bool) – If False (the default) the validator will ignore letter case when comparing letters, so that aA will fail validation. If True a and A are considered as different letters and aA will pass validation.

exception promptpy.validators.ValidationError[source]

Raised if input validation fails.