Giter Site home page Giter Site logo

vaa's Introduction

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        # [Error(message='Not a valid integer.', field='id')]

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        # [Error(message='Not a valid integer.', field='id')]

vaa's People

Contributors

bobronium avatar orsinium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

seanpm2001

vaa's Issues

New API

Let's discuss new vaa api here.

Previously discussed:

  • Make errors list of ErrorObj (Should we make it raisable?);
  • Drop special types for validators in favor of functions and typing.NamedTuple. (done in #3)

I also suggest to:

  • drop is_valid property and threat data as valid if no errors returned;
  • rename cleaned_data to data (cause it shorter) and just assume that is valid (still not sure about this).

New api example:

validator = vaa.get_from(some_scheme)
some_data = {'a': 1}
result = validator(some_data)
result.data
result.errors
# or
data, errors = validator(some_data)

if errors:  # not valid
    ...

To think about:

  • raising exception on errors?
  • if we return errors and data is valid, should we return errors as a empty list or as None (same goes for data)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.