Skip to main content

A powerfull schema validator

Project description

kohi

A powerfull schema validator

GitHub Repo stars GitHub Workflow Status GitHub

Quickstart

To validate a type you can import your schema validator from kohi or from kohi.<type> import <type>Schema

e.g.

Let's check if a person's date of birth is a positive integer less than the current date — 2023 — and greater than or equal to 2005

from kohi import NumberSchema
# from kohi.number import NumberSchema

n = NumberSchema().int().positive().lt(2023).gte(2005)

print(n.validate(2005)) # True
print(n.validate(2022)) # True
print(n.validate(2004)) # False
print(n.validate(2023)) # False

# You can see the errors generated in the last `validate` call just by accessing the `errors` property
# print(n.errors) # ['number must be less than 2022']

Validators

Only one base class for all schema validators

or kohi.NumberSchema

or kohi.StringSchema

Methods

BaseSchema

  • add_validator(name, func): Self

    Add a custom data validator

  • validate(data): bool

    The method to be called when we validate the schema

  • reset(): None

    Reset error list

  • throw(): Self

    By default no errors are thrown, but when this method is chained a ValidationError will be thrown

NumberSchema

inherits from BaseSchema

By default validates int and float

  • float(): Self

    Validate only float

  • int(): Self

    Validate only int

  • lt(num): Self

    Validates if the data is less than num

  • gt(num): Self

    Validates if the data is greater than num

  • lte(num): Self

    Validates if the data is less than or equal to num

  • gte(num): Self

    Validates if the data is greater than or equal to num

  • min(num): Self

    Just an alias for gte(num)

  • max(num): Self

    Just an alias for lte(nun)

  • positive(): Self

    Just an alias for gt(0)

  • negative(): Self

    Just an alias for lt(0)

StringSchema

inherits from BaseSchema

Dev env

  • install development dependencies
  • check types using mypy
  • run all tests using pytest

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

kohi-0.1.0.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

kohi-0.1.0-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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