Skip to main content

A easy dependency validator

# required: Easy multi-field validation

Required is a simple library which allows you to validate dependencies across multiple fields. The goal is to make writing things like forms, seralizers and functions much easier by providing a declarative way to encode validation logic. It aims to:

• Have a declarative way to encode validation logic
• Allow you to maintain validation logic easily
• Allow you to reuse your validation logic easily
• Be flexible with what you want to validate

If this all sounds good. Read On!

## Installation

Install using `pip`

``````pip install required
``````

## Quickstart

You can use required in a number of ways. The easiest way is to use the `validate` decorator to validate inputs to function calls.

```from required import validate

@validate
def calculate_sum(positive_number, negative_number):
"""
positive_number -> positive_number > 0
negative_number -> negative_number < 0
"""
return positive_number + negative_number

# the following will raise a validation exception
calculate_sum(1, 1)

# this will pass validation
calculate_sum(1, -1) # 0
```

If you want to have other information in the docstring, the validation rules can be wrapped inside of `Requires { }` as shown below:

```@validate
def calculate_sum(positive_number, negative_number):
"""
Other documentation relating to calculate_sum

Requires {
positive_number -> positive_number > 0
negative_number -> negative_number < 0
}

You can also put information after the requires rules
"""
return positive_number + negative_number
```

Validation rules are written in the doc string of the function. They look like:

`[param] -> [expression_1] [comparator] [expression_2]`

When `param` is present, it requires `expression_1 [comparator] expression_2` to evaluate to true.

The most simple expressions are just variables passed into the function to validate, however they can be more complex. See cookbook for more examples.

The comparator can be one of the standard python comparator operations; `==`, `!=`, `in`, `>=` `<=`, `>`, `<`.

## Cookbook

The following shows some examples for writing validation rules

``````
# Arithmetic on the objects follow normal maths rules.
# you need to put brackets to define expressions
x -> (x + 1) < 1
x -> (x - y) == 1

# A value `x` needs to be in an array
x -> x in arr

# The length of x must be 10
# see section on registering functions
x -> len(x) == 10

# The length of x and y must be the same
x -> len(x) == len(y)

# when x is present y must not be present
# TODO: not implemented in DSL yet
x -> x == <empty>

# x must be equal to the return value of a function
x -> x == func(x)

# Partial dependencies can be also specified

# when x == 1 then y must be 2
x == 1 -> y == 2

# when x == 1 then y must be set
x == 1 -> y
``````

## Registering callables

You can register callables into the validation scope. This is useful when you want to call normal python builtins or custom callables.

```from required import validate

scoped_validate = validate.register_callables({
"len": len,
"abs": abs
})

@scoped_validate
def return_first_element(arr):
"""
arr -> len(arr) >= 1
"""
return arr

# validation scoped callables can also be nested
# len, abs and my_func are available in the second_scoped_validate decorator

second_scoped_validate = scoped_validate.register_callables({
"my_func": my_func
})

# or inserted at function level
@validate(callable_dict={"new_func": new_func})
def other_function(var):
"""
var -> new_func(var) >= 1
"""
return var
```

## Contributing

If you want to contribute you are most welcome! This project is distributed under the MIT licence. It is tested using tox against Python 2.7 and 3.4+

## Release history Release notifications | RSS feed

This version 0.4.0 0.3.4.post1 0.3.4 0.3.3 0.3.2 0.3.1 0.3.0 0.2.2 0.2.1 0.2.0 0.1.4 0.1.3 0.1.2 0.1.1

## Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for required, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size required-0.4.0-py2.py3-none-any.whl (11.1 kB) File type Wheel Python version py2.py3 Upload date Hashes
Filename, size required-0.4.0.tar.gz (15.6 kB) File type Source Python version None Upload date Hashes
{"ft_lang_label":"__label__en","ft_lang_prob":0.6953827,"math_prob":0.92966545,"size":4368,"snap":"2021-21-2021-25","text_gpt3_token_len":1090,"char_repetition_ratio":0.13863428,"word_repetition_ratio":0.050221566,"special_character_ratio":0.27541208,"punctuation_ratio":0.10056657,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.976274,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-22T13:43:13Z\",\"WARC-Record-ID\":\"<urn:uuid:962577d6-49b9-4873-a77b-495c089e35d5>\",\"Content-Length\":\"73652\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cb0adcd7-ba7f-4f37-913e-10959b863186>\",\"WARC-Concurrent-To\":\"<urn:uuid:b9e15427-3c86-4c69-b15c-6d6358bd29ae>\",\"WARC-IP-Address\":\"151.101.64.223\",\"WARC-Target-URI\":\"https://pypi.org/project/required/\",\"WARC-Payload-Digest\":\"sha1:2FNIZINFCME37Z7SV4WNC2PTUGBNXST5\",\"WARC-Block-Digest\":\"sha1:GSCFL6RDRFMX36QKPUNUUYD6AODLCCVO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488517820.68_warc_CC-MAIN-20210622124548-20210622154548-00435.warc.gz\"}"}