ama.validator

Provides access to a registry of validation functions.

Functions are returned via the get_validator() function and can be refined by passing a specification which alters what passes the validation.

All validators throw TypeError if the value’s type cannot be validated and ValueError if the value fails validation.

Validator Name Tests that the value…
nonempty is not None or an empty string
constant always returns the same value
str can be converted to a string
int can be converted to an integer value
float can be converted to a floating point value
bool can be converted to a boolean value
yesno matches one of yes, y, no, n with any case plus 1, 0, True and False
re matches the regular expression.
path is a valid path
date is a valid date
time is a valid time
color is a valid RGB or RGB hex color
email is a valid email address
ama.validator.NonEmpty(*args, **kwargs)

Create a validator that checks that any value is provided

ama.validator.Constant(*args, **kwargs)

Create a validator that always return the same value.

ama.validator.Str(*args, **kwargs)

Create a validator that checks that the value is a valid string according to the spec

Parameters:spec (str) –

The specification to check the string against. Can be either

None
Anything that can be converted to a string passes
The string nonempty
a string of length greater than 1 passes
A string of argument=value pairs separated by commas.
Checks the string matches based on the arguments specified

The following arguments can be specified.

min - The minimum number of characters
max - The maximum number of characters

e.g. “min=3,max=6” means the string must be between 3 and 6 characters long.

ama.validator.Int(*args, **kwargs)

Create a validator that checks that the value is a valid integer according to the spec

Parameters:spec (str) –

The specification to check the integer against. Can be either

None
Anything that is an integer passes. e.g. 1 and “1” are valid integers but 1.2, “1.2” or “chas” are not.
A string of argument=value pairs separated by commas.
Alters how the integer is validated. The following arguments can be specified.
min - The minimum value
max - The maximum value

e.g. “min=3,max=6” means the value must be between 3 and 6.

ama.validator.Float(*args, **kwargs)

Create a validator that checks that the value is a valid float according to the spec

Parameters:spec (str) –

The specification to check the float against. Can be either

None
Anything that is a float passes. e.g. 1.2 and “1.2” are valid floats but 1, “1” or “dave” are not.
A string of argument=value pairs separated by commas.
Alters how the float is validated. The following arguments can be specified.
min - The minimum value
max - The maximum value
decimal - The character to consider as the decimal separator
nocoerce - Disable coercing int to float

e.g. “min=3.1,max=6.0” means the value must be between 3.1 and 6.0; “decimal=\,” means that “33,234” is a valid float.

ama.validator.Number(*args, **kwargs)

Create a validator that checks that the value is a valid number according to the spec

Parameters:spec (str) –

The specification to check the integer against. Can be either

None
Anything that is a number passes.
A string of argument=value pairs separated by commas.
Check s the integer matches based on the arguments specified

The following arguments can be specified.

min - The minimum value
max - The maximum value
decimal - The character to consider as the decimal separator

e.g. “min=3,max=6” means the value must be between 3 and 6.

ama.validator.Bool(*args, **kwargs)

Create a validator that checks that the value is a valid bool.

ama.validator.Regex(*args, **kwargs)

Create a validator that checks that the value matches a regular expression.

ama.validator.Path(*args, **kwargs)

Create a validator that checks that the value is a valid path.

The meaning of valid is determined by the spec argument

Parameters:spec (str) –

Determines what is a valid path.

existing
is a path that exists (the default)
empty
is a path that is empty
nonempty
is a path that is not empty
new
is a path that does not exist and is a valid name for a path
pathspec
is a valid path name that contains files that conform to pathspec

pathspec is of the form [+-]glob where the leading + indicates that the path must include a file that matches the glob and - indicates that it must not include files that match the glob. Multiple pathspecs can be specified separated by commas.

ama.validator.Date(*args, **kwargs)

Create a validator that checks that the value is a valid date.

Parameters:spec (str) –

The date format to accept if a string value is used.

spec follows the standard Python strftime format string.

ama.validator.Time(*args, **kwargs)

Create a validator that checks that the value is a valid time.

Parameters:spec (str) –

The time format to accept if a string value is used.

spec follows the standard Python strftime format string.

ama.validator.Color(*args, **kwargs)

Create a validator that checks that the value is a valid color

The color format, which is determined by the spec argument, can be one of the following

  • An RGB hex representation i.e. # followed by either 3 or 6 hex digits.
  • A string of the form ‘rgb(R, G, B)’ where R, G and B are floating point values between 0.0 and 1.0
Parameters:spec (str) – The color type to accept either ‘rgbhex’ or ‘rgb’
ama.validator.Email(*args, **kwargs)

Create a validator that checks that the value is a valid email address.

If the pyisemail module is available then that is used to validate the email address otherwise a regular expression is used (which my produce false positives.)