Skip to main content

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

Cerberus

va.cerberus

Django Forms

va.django

Marshmallow

va.marshmallow

PySchemes

va.pyschemes

Django REST Framework

va.restframework

WTForms

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

vaa-0.1.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

vaa-0.1.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file vaa-0.1.3.tar.gz.

File metadata

  • Download URL: vaa-0.1.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for vaa-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2c19dcf7b75c5b815c9cb02340db2b38e0e9b3a80e8a29a1524161d4173be7fc
MD5 52c2c3f41c3a433eba280cfdf9e662db
BLAKE2b-256 fc1c2698aaa2daf8fbdba5b0817f003d816a0b583c4925f8c5085c6aa9ae0ce1

See more details on using hashes here.

File details

Details for the file vaa-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: vaa-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for vaa-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 260c853653b0b2eeb0bde6e1f7402f7500f36d3fdea41d2699bdf31fa57142f9
MD5 e20e8bc8717cdd18864e5d29a2146de2
BLAKE2b-256 c24f2f250019ad95464eb83825519013a160ec946991077fcdd28fe4c96509cd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page