Validation

datatype.validation

datatype.validation.failures(datatype, value)

Return list of failures (if any) validating value against datatype.

Params:
datatype: Datatype to validate value against. See README.markdown
for examples.

value: Value to validate path: Used internally for location of failures.

Example:
>>> failures('int', 'foo')
['expected int, got str']
datatype.validation.is_valid(datatype, value)

Return boolean representing validity of value against datatype.

datatype.decorators

The decorators module provides means for enforcing proper function return value datatypes.

datatype.decorators.returns(dfn, strict=True)

Make decorators to watch return values of functions to ensure they match the given datatype definition.

Optional Arguments:
strict: if false, unexpected values on dictionaries will not raise an exception
Example:
>>> @returns('int')
... def myfunction():
...     return "bad return value"
>>> myfunction()
Traceback (most recent call last):
BadReturnValueError
datatype.decorators.returns_iter(dfn, strict=True)

Validate output of iterator/generator function.

Note: exceptions for bad return datatypes will not be raised until the bad value of the iterator is consumed.

Optional Arguments:
strict: if false, unexpected values on dictionaries will not raise an exception
Example:
>>> @returns_iter('str')
... def myfunction():
...     for x in range(3):
...         yield "number %s" % x
>>> list(myfunction())  # no error
['number 0', 'number 1', 'number 2']

Project Versions

Table Of Contents

Previous topic

Defining datatypes

Next topic

Coercion

This Page