models.validators.Validators

Note

This module contains the implemented validator classes for all validators.

class NumericRangeValidator(json=None, minvalue=None, maxvalue=None, stepsize=None, enforce_stepsize=None, **kwargs)[source]

A numeric range validator, which validates a number between a range.

The range validates positively upto and including the minvalue and maxvalue.

An added ability is the check if the number conforms to a step within that range. The validation checks for both integer and floats. The stepsize is only enforced when the enforce_stepsize is set to True. This enforcement is accurate to an accuracy set in the accuracy (normally set to be 1E-6).

New in version 2.2.

Variables
  • minvalue – minimum value of the range

  • maxvalue – maximum value of the range

  • stepsize – stepsize

  • enforce_stepsize – flag to ensure that the stepsize is enforced

Examples

>>> validator = NumericRangeValidator(minvalue=0, maxvalue=50)
>>> validator.is_valid(42)
True
>>> validator.is_valid(50)
True
>>> validator.is_valid(50.0001)
False
>>> validator.is_valid(-1)
False
>>> validator.get_reason()
Value '-1' should be between 0 and 50
>>> stepper = NumericRangeValidator(stepsize=1000, enforce_stepsize=True)
>>> stepper.is_valid(2000)
True

Construct the numeric range validator.

class RequiredFieldValidator(json=None, *args, **kwargs)[source]

Required field validator ensures that a value is provided.

Does validate all values. Does not validate None or ‘’ (empty string).

New in version 2.2.

Examples

>>> validator = RequiredFieldValidator()
>>> validator.is_valid("A value")
True
>>> validator.is_valid("")
False
>>> validator.is_valid(None)
False
>>> validator.get_reason()
"Value is required"

Construct a Property Validator.

class BooleanFieldValidator(json=None, *args, **kwargs)[source]

A boolean field validator.

This is a stub implementation. Should validate if a value is either ‘truthy’ or ‘falsy’.

Construct a Property Validator.

class EvenNumberValidator(json=None, *args, **kwargs)[source]

An even number validator that validates True when the number is even.

Even numbers are scalar numbers which can be diveded by 2 and return a scalar. Floating point numbers are converted to integer first. So int(4.5) = 4.

New in version 2.2.

Example

>>> validator = EvenNumberValidator()
>>> validator.is_valid(4)
True
>>> validator.is_valid(4.5)  # float is converted to integer first
True

Construct a Property Validator.

class OddNumberValidator(json=None, *args, **kwargs)[source]

A odd number validator that validates True when the number is odd.

Example

>>> validator = OddNumberValidator()
>>> validator.is_valid(3)
True
>>> validator.is_valid(3.5)  # float is converted to integer first
True

Construct a Property Validator.

class SingleReferenceValidator(json=None, *args, **kwargs)[source]

A single reference validator, ensuring that only a single reference is selected.

New in version 2.2.

Construct a Property Validator.

class RegexStringValidator(json=None, pattern=None, **kwargs)[source]

A regular expression string validator.

With a configured regex pattern, a string value is compared and matched against this pattern. If there is a positive match, the validator validates correctly.

For more information on constructing regex strings, see the python documentation, regex101.com, or regexr.com.

New in version 2.2.

Variables

pattern – the regex pattern to which the provided value is matched against.

Example

>>> validator = RegexStringValidator(pattern=r"Yes|Y|1|Ok")
>>> validator.is_valid("Yes")
True
>>> validator.is_valid("No")
False

Construct an regex string validator.

If no pattern is provided than the regexstring ‘.+’ will be used, which matches all provided text with at least a single character. Does not match ‘’ (empty string).

Parameters
  • json (dict) – (optional) dict (json) object to construct the object from

  • pattern – (optional) valid regex string, defaults to r’.+’ which matches all text.

  • kwargs (dict) – (optional) additional kwargs to pass down

class EmailValidator(json=None, **kwargs)[source]

A email string validator.

Variables

pattern – the email regex pattern to which the provided value is matched against.

Construct an email string validator.

Parameters
  • json (dict) – (optional) dict (json) object to construct the object from

  • kwargs (dict) – (optional) additional kwargs to pass down

class FileSizeValidator(json: Optional[Dict] = None, max_size: Optional[Union[int, float]] = None, **kwargs)[source]

A file size Validator.

The actual size of the file cannot be checked in pykechain without downloading this from the server, hence when the validator is used inside an attachment property, the validator returns always being valid.

Variables

max_size – maximum size to check

Example

>>> validator = FileSizeValidator(max_size=100)
>>> validator.is_valid(100)
True
>>> validator.is_valid(-1)
False
>>> validator.is_valid("attachments/12345678-1234-5678-1234-567812345678/some_file.txt")
True
>>> validator.get_reason()
We determine the filesize of 'some_file.txt' to be valid. We cannot check it at this end.

Construct a file size validator.

Parameters
  • json (Optional[Dict]) – (optional) dict (json) object to construct the object from

  • max_size – (optional) number that counts as maximum size of the file

  • kwargs – (optional) additional kwargs to pass down

class FileExtensionValidator(json: Optional[Dict] = None, accept: Optional[Union[str, List[str]]] = None, **kwargs)[source]

A file extension Validator.

It checks the value of the property attachment against a list of acceptable mime types or file extensions.

Example

>>> validator = FileExtensionValidator(accept=[".png", ".jpg"])
>>> validator.is_valid("picture.jpg")
True
>>> validator.is_valid("document.pdf")
False
>>> validator.is_valid("attachments/12345678-1234-5678-1234-567812345678/some_file.txt")
False
>>> validator = FileExtensionValidator(accept=["application/pdf",
...                                            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"])
>>> validator.is_valid("document.pdf")
True
>>> validator.is_valid("comma-separated-values.csv")
False
>>> validator.is_valid("attachments/12345678-1234-5678-1234-567812345678/modern_excel.xlsx")
True

Construct a file extension validator.

Parameters
  • json (Optional[Dict]) – (optional) dict (json) object to construct the object from

  • accept (Optional[List[Text]]) – (optional) list of mimetypes or file extensions (including a ., eg .csv, .pdf)

  • kwargs – (optional) additional kwargs to pass down