Skip to content

a functional library for creating validators in Python

License

Notifications You must be signed in to change notification settings

json2d/validium

Repository files navigation

validium

a Python utility library for performing validations flexibly

Installation

pip install validium

Quick Start

Here's an example of how to create and use some very simple validators for some numbers:

import validium as V

PI = 3.14
ONE = 1

is_number = V.Validator(lambda x: isinstance(x, Number), 'must be a number')

is_number.validate(PI) # pass
is_number.validate(ONE) # pass

is_positive = V.Validator(lambda x: x > 0, 'must be positive')

is_positive.validate(PI) # pass
is_positive.validate(ONE) # pass

is_not_one = V.Validator(lambda x: not x == 1, 'must not equal 1')

is_not_one.validate(PI) # pass
is_not_one.validate(ONE) # AssertionError: must not equal 1

Building Up

Here's an example of how to parameterize and reuse a common validator pattern:

is_not = lambda y: V.Validator(lambda x: not x == y, 'must not equal {}'.format(x)) # u

is_not(-1).validate(ONE) # pass
is_not(0).validate(ONE) # pass
is_not(1).validate(ONE) # AssertionError: must not equal 1

This approach will help keep your code nice and DRY in the event you need handful of validators that behave mostly the same but slightly different.