Structure

You can build you own tests, validators and sanitizers using these classes.

class Test

A test can be called and will yield a validated and/or sanitized product. The Test itself doesn’t do anything.

class One

Given a callback, when the One test is ran, it runs the callback with the given value.

test = One(lambda v: v < 10, 'Should not exceed 10.')
value = test(6)  # 6
value = test(23)  # <Error 'Should not exceed 10.'>
__init__(callback[, error])
Parameters:
  • callback (Union[Callable, Test]) – The test that runs when called.
  • error (str) – Value of the error-message (‘Test failed.’).
class All

Given multiple callbacks in a list, when the All test is ran, it runs the all the callbacks with the given value.

Be aware that any sanitization must be wrapped in a To, when providing a simple function the outcome is not saved. A lambda (or any other callback like str or int) is only tested on truthness, when false is throws the error.

test = All([To(int), lambda v: v < 10], 'Must be an int, less than 10.')
value = test('6')  # 6
value = test(23)  # <Error 'Must be an int, less than 10.'>
value = test('aa')  # <Error 'Must be an int, less than 10.'>
__init__(callbacks[, error])
Parameters:
  • callbacks (List[Union[Callable, Test]]) – The test that runs when called.
  • error (str) – Value of the error-message (‘Test failed.’).
class Or

Given multiple callbacks in a list, when the Or test is ran, it runs the callbacks one by one until a success (errors are discarded). The successful result is returned. When no callback is successful an error is thrown.

test = Or([date(), datetime(), numeric()], 'Requires date, datetime or timestamp (int).')
value = test('2004-2-18')  # datetime.date(2004, 2, 18)
value = test(1077062400)  # Decimal(1077062400)
value = test('aa')  # <Error 'Requires date, datetime or timestamp (int).'>
__init__(callbacks[, error])
Parameters:
  • callbacks (List[Union[Callable, Test]]) – A list of callbacks.
  • error (str = 'All tests failed.') – Error when none of the callbacks is successful.
class To

This is a sanization helper, it simply tries to convert a value To another value. Usefull when running All, mostly in conjunction with other tests.

test = To(int)
value = test('6')  # 6
value = test(2.4)  # 2
value = test('a')  # <Error 'Conversion failed.'>
__init__(callbacks[, error])
Parameters:
  • into (Union[Callable, Test]) – Turn the value into this.
  • error (str = 'Conversion failed.') – Error when the conversion failed.
class Yield

Yield a fixed result when the callback over the value succeeds. There are not many usecases for this, but in conjunction with Or it can be handy (providing default values on test-success).

test = Or([
  Yield(lambda v: v in [True, False], 'boolean'),
  Yield(lambda v: v in [None], 'none'),
  Yield(One(lambda v: isinstance(v, (str, bytes))), 'string'),
  Yield(One(lambda v: isinstance(v, int)), 'number'),
])

value = test(True) # 'boolean'
test(None) # 'none'
test('blurp') # 'string'
test(44) # 'number'
__init__(callbacks[, error])
Parameters:
  • callback (Union[Callable, Test]) – The test to run.
  • fixed_result (Any) – Returned when the callback is truthful.
  • error (str = 'Test failed.') – Error when the callback was not truthful.