Skip to main content

Simple validation tool for API

Project description

data-spec-validator

Why

  • To get rid of code snippet like these (... cumbersome and tedious validation)
def do_something(params):
    val_a_must_int = params.get('a', 0)
    val_b_must_be_non_empty_list = params.get('b', [])
    # if key c presents, value c must be a date string between '2000-01-01' to '2020-01-01'
    val_c_might_be_none = params.get('c', None)

    # check type
    if type(val_a_must_int) != int:
      raise XXX

    # check type & value
    if type(val_b_must_list) != list or len(val_b_must_be_non_empty_list) == 0:
      raise XXX

    # if value exists, check its value
    if val_c_might_be_none is not None:
        date_c = datetime.strptime(val_c_might_be_present, '%Y-%m-%d')
        date_20000101 = datetime.date(2000, 1, 1)
        date_20200101 = datetime.date(2020, 1, 1)
        if not (date_20000101 <= date_c <= date_20200101):
          raise XXX
    ...
    # do something actually

Quick Example

  • Do validate_data_spec directly wherever you like (see test_spect.py for more)
from data_spec_validator.spec import INT, DIGIT_STR, ONE_OF, OPTIONAL, Checker, CheckerOP, validate_data_spec

class SomeSpec:
    field_a = Checker([INT])
    field_b = Checker([DIGIT_STR, OPTIONAL], op=CheckerOP.ANY)

some_data = dict(field_a=3, field_b='4', field_c=[1,2])
validate_data_spec(some_data, SomeSpec) # return True

some_data = dict(field_a=4)
validate_data_spec(some_data, SomeSpec) # return True

some_data = dict(field_a='3')
validate_data_spec(some_data, SomeSpec) # raise Exception

some_data = dict(field_a='3')
validate_data_spec(some_data, SomeSpec, nothrow=True) # return False

class AnotherSpec:
    field = Checker([ONE_OF], extra={ONE_OF: [1, '2', [3, 4], {'5': 6}]})

another_data = dict(field=[3, 4])
validate_data_spec(another_data, AnotherSpec) # return True

another_data = dict(field='4')
validate_data_spec(another_data, AnotherSpec) # raise Exception
  • Decorate a method with data_spec_validation, the method must meet one of the following requirements.
    1. Has a WSGIRequest(django.core.handlers.wsgi.WSGIRequest) attribute.
    2. The 2nd argument of the method is a rest_framework.request.Request instance.
from rest_framework.views import APIView

from data_spec_validator.decorator import data_spec_validation
from data_spec_validator.spec import UUID, EMAIL, Checker

class SomeViewSpec:
  param_a = Checker([UUID])
  param_b = Checker([EMAIL])

class SomeView(APIView):
    @data_spec_validation(SomeViewSpec)
    def get(self, request):
        pass

Register Custom Spec Check & Validator

  • Define custom CHECK constant (gt_check in this case) and write custom Validator(GreaterThanValidator in this case)
gt_check = 'gt_check'
from data_spec_validator.spec.defines import BaseValidator
class GreaterThanValidator(BaseValidator):
    name = gt_check

    @staticmethod
    def validate(value, extra, data):
        criteria = extra.get(GreaterThanValidator.name)
        return value > criteria, ValueError(f'{value} is not greater than {criteria}')
  • Register custom check & validator into data_spec_validator
from data_spec_validator.spec import custom_spec, Checker, validate_data_spec
custom_spec.register(dict(gt_check=GreaterThanValidator()))

class GreaterThanSpec:
    key = Checker([gt_check], extra={gt_check: 10})

ok_data = dict(key=11)
validate_data_spec(ok_data, GreaterThanSpec) # return True

nok_data = dict(key=9)
validate_data_spec(ok_data, GreaterThanSpec) # raise Exception

Test

python -m unittest test.test_spec

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

data-spec-validator-0.9.1.tar.gz (13.6 kB view details)

Uploaded Source

File details

Details for the file data-spec-validator-0.9.1.tar.gz.

File metadata

  • Download URL: data-spec-validator-0.9.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.8

File hashes

Hashes for data-spec-validator-0.9.1.tar.gz
Algorithm Hash digest
SHA256 86eaea8ce739f7ebdfc56df219178d43e6d4a08cd01c42ece1e23780ebd6f812
MD5 bcf7b83ad7d8a47d7fdb14824ffc6f51
BLAKE2b-256 a939081fac56c33426d952762361c91c3f4f2e17dbfe7a45a381fa57ea9598ea

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