Skip to main content

Validators Adapter. The common interface for all validators.

Project description

VAA

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 vaa 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 an error isn't for a specific field, it 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.

Unknown scheme

If you're making a library that should accept any validator without explicit vaa usage, use vaa.wrap:

class Scheme(marshmallow.Schema):
  id = marshmallow.fields.Int(required=True)
  name = marshmallow.fields.Str(required=True)

validator = vaa.wrap(Scheme)({'id': 'no', 'name': 'Oleg'})
validator = Scheme({'id': 'no', 'name': 'Oleg'})
validator.is_valid()    # False
validator.errors        # {'id': ['Not a valid integer.']}

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.2.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

vaa-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vaa-0.2.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: DepHell/0.8.2

File hashes

Hashes for vaa-0.2.0.tar.gz
Algorithm Hash digest
SHA256 04090e6433f164a9c3b9ea09a15ae1663115397f4489b85ca632a0b9e88075ea
MD5 83ccf3c3f984800f9e2c74d676a67913
BLAKE2b-256 1f2df16860e00c2a3598b5be66bb85726aa9829cb2b94f03f1086f9113c8f3c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vaa-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: DepHell/0.8.2

File hashes

Hashes for vaa-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02328ae7afff675a089ecd6cfca24787bf56a5fe70ffe154ba3ae62d8e4a4e06
MD5 a7f009acedf7ffa34359070feb0afe87
BLAKE2b-256 8aedbb251dfc0a0a8e4caaded7d535fe20f6976ac4f1261c47f54ecc5e62e810

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