Validators Adapter. The common interface for all validators.
Project description
VAlidators Adapter makes validation by any existing validator with the same interface.
Supported validators:
validator |
adapter |
---|---|
va.cerberus |
|
va.django |
|
va.marshmallow |
|
va.pyschemes |
|
va.restframework |
|
va.wtforms |
python3 -m pip install --user vaa
Example
import marshmallow
import vaa
@vaa.marshmallow
class Scheme(marshmallow.Schema):
id = marshmallow.fields.Int(required=True)
name = marshmallow.fields.Str(required=True)
Validating data
All schemes adopted by va has the same interface:
validator = Scheme({'id': '1', 'name': 'Oleg'})
validator.is_valid() # True
validator.cleaned_data
# {'name': 'Oleg', 'id': 1}
validator = Scheme({'id': 'no', 'name': 'Oleg'})
validator.is_valid() # False
validator.errors
# {'id': ['Not a valid integer.']}
If error isn’t for specific field, iw will be in __all__ key.
Simple scheme
If you want to do validation with simple function, you can use va.simple adapter. For example, you want to check that in dict {'a': ..., 'b': ...} both values are positive. There are many ways to do so.
It can return bool:
@vaa.simple
def validate(a, b) -> bool:
return a > 0 and b > 0
Or return message for error:
@vaa.simple
def validate(a, b) -> bool:
if a > 0 and b > 0:
return True
return 'should be positive'
Or return errors dict:
@vaa.simple
def validate(a, b) -> bool:
if a <= 0:
return {'a': 'should be positive'}
if b <= 0:
return {'b': 'should be positive'}
return True
Or raise va.ValidationError with error message or dict:
@vaa.simple
def validate(a, b) -> bool:
if a > 0 and b > 0:
return True
raise vaa.ValidationError('should be positive')
Also, if you want to get the original dict without unpacking it into keyword arguments, do a function that accepts only one _ argument:
@vaa.simple
def validate(_):
return _['a'] > 0 and _['b'] > 0
In that dict keys can be accessed as attributes:
@vaa.simple
def validate(_):
return _.a > 0 and _.b > 0
Choose the best way and follow it. Avoid mixing them in one project.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file vaa-0.1.4.tar.gz
.
File metadata
- Download URL: vaa-0.1.4.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 578f2ece43ddf9f5e1adb906737736ddd2db16bdfde7c1b65e1a750fa502c8c0 |
|
MD5 | 09700f00915e9fe8fb70f59947465491 |
|
BLAKE2b-256 | 4d31e4a7fea6cb674e3461b9a7fd68b72d752fe9203b20d67ae2a96bc529602c |
File details
Details for the file vaa-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: vaa-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fc051e15968012498bb79ef10c667c293a6c71edb0ada2793f5d8ea55a6fae3 |
|
MD5 | 2efc0439d069ef1421afe9fe96ae5a94 |
|
BLAKE2b-256 | eb645e4a8443e4e4610d1b3fc539e7ca5df149f97e3358e40c8651a98725fd86 |