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)
- 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
- class promptpy.validators.DateValidator(format: str, accept_empty=True)[source]
Validates that an input string is a valid date.
Pass a
strptimeformat string in the constructor, the validator will check that the input string can be converted to a date usingdatetime.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
- 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
- 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
- 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
- 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 thataAwill fail validation. IfTrueaandAare considered as different letters andaAwill pass validation.