Datatypes

Incoming provides basic datatypes for validation. These datatypes are responsible for performing validation tests on values. Incoming provides some datatypes and it also allows writing your own datatypes.

Note

also see using Custom validation methods for implementing custom validations.

Available Datatypes

Following datatypes are provided:

class incoming.datatypes.Integer(required=None, error=None, *args, **kwargs)

Sub-class of Types class for Integer type. Validates if a value is a int value.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
class incoming.datatypes.Float(required=None, error=None, *args, **kwargs)

Sub-class of Types class for Float type. Validates if a value is a float value.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
class incoming.datatypes.Number(required=None, error=None, *args, **kwargs)

Sub-class of Types class for Number type. Validates if a value is a int value or float value.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
class incoming.datatypes.String(required=None, error=None, *args, **kwargs)

Sub-class of Types class for String type. Validates if a value is a str value or unicode value.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
class incoming.datatypes.Boolean(required=None, error=None, *args, **kwargs)

Sub-class of Types class for Boolean type. Validates if a value is a bool.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
class incoming.datatypes.Array(required=None, error=None, *args, **kwargs)

Sub-class of Types class for Array type. Validates if a value is a list object.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
class incoming.datatypes.Function(func, *args, **kwargs)

Sub-class of Types class for Function type. This type allows using functions for validation. Using incoming.datatypes.Function, validation tests can be written in a function or a regular method, a staticmethod or a classmethod on the sub-class of incoming.PayloadValidator.

Parameters:func – any callable that accepts val, *args and

**kwawrgs and returns a bool value, True if val validates, False otherwise.

class incoming.datatypes.JSON(cls, *args, **kwargs)

Sub-class of Types class for JSON type. This type allows writing validation for nested JSON.

Parameters:cls – sub-class of incoming.PayloadValidator for

validating nested JSON. If you are using a class nested within the parent validator, you must define the inner class before using it, so that the name of the inner class is defined in the scope of the parent class.

Creating your own datatypes

You can create your own set of datatypes if you like.

Note

also see using Custom validation methods for implementing custom validations.

A few things to keep in mind

  • Sub-class incoming.datatypes.Types to implement a custom datatype.
  • The sub-class must add _DEFAULT_ERROR attribute to provide the default error message that will be used when validation fails.
  • The sub-class must implement validate() method as a regular method or as a staticmethod or as a classmethod.
  • validate() must return a bool value. True if validation passes, False otherwise.
  • validate() method gets the following arguments: * val - the value which must be validated * key - the field/key in the payload that held val. * payload - the entire payload that is being validated. * errors - incoming.incoming.PayloadErrors object.
  • For the above point mentioned above, you may want to use your validate() method elsewhere outside the scope of incoming where the above arguments are not necessary. In that case, use *args and **kwargs.

Example

The example below shows how you can use a staticmethod to implement validation test.

class Gender(datatypes.Types):
    _DEFAULT_ERROR = 'Gender must be either male or female.'

    @staticmethod
    def validate(val, *args, **kwargs):
        if not isinstance(val, str):
            return False
        if val.lower() not in ('male', 'female'):
            return False
        return True

class PersonValidator(PayloadValidator):
    name = datatypes.String()
    age = datatypes.Integer()
    gender = Gender()

datatypes.Types Base Class

class incoming.datatypes.Types(required=None, error=None, *args, **kwargs)

Base class for creating new datatypes for validation. When this class is sub-classed, validate() must be implemented in the sub-class and this method will be resposible for the actual validation.

Parameters:
  • required (bool) – if a particular (this) field is required or not. This param allows specifying field level setting if a particular field is required or not.
  • error (str) – a generic error message that will be used by incoming.PayloadValidator when the validation test fails.
validate(val, *args, **kwargs)

This method must be overridden by the sub-class for implementing the validation test. The overridden method can be a regular method or classmethod or staticmethod. This method must only return a bool value.

Parameters:val – the value that has to be validated.
Returns bool:if the implemented validation test passed or not.