Skip to main content

A function argument validation for humans

Project description

Function argument validation for humans for Python 3!

Highlights

  • Straight port from the amazing library ow from Sindre Sorhus
  • Expressive chainable API
  • Pythonic approach via decorator
  • Lots of built-in validations
  • Supports custom validations
  • Written in Python3 with Type hinting

Notes

Since this is a straight up port from the JavaScript library not all features are available. Partly since this is a port and I haven't caught up and also since Python doesn't support all usecases as JavaScript does.

Install

$ pip3 install pyow

Usage

from pyow import pyow

def unicorn(input):
	pyow(input, pyow.string.min_length(5))

	# ...

unicorn(3)
>>> ArgumentError: 'Expected argument to be of type `str` but received type `int`

unicorn('yo')
>>> ArgumentError: Expected string to have a minimum length of `3`, got `yo`

or via decorator

from pyow.decorator import validate

@validate(
    pyow.string.min_length(5)
)
def unicorn(input):
    return 1

API

pyow(value, predicate)

Test if value matches the provided predicate. Throws an ArgumentError if the test fails.

pyow.is_valid(value, predicate)

Returns True if the value matches the predicate, otherwise returns False.

pyow.create(predicate)

Create a reusable validator.

check_password = pyow.create(pyow.string.min_length(6))

check_password('foo')
>>> ArgumentError: ('Expected string to have a minimum length of `6`, got `foo`')

pyow.any(predicate: List[Predicate])

Returns a predicate that verifies if the value matches at least one of the given predicates.

pyow('foo', pyow.any(pyow.string.max_length(3), pyow.number))

pyow.{type}

All the below types return a predicate. Every predicate has some extra operators that you can use to test the value even more fine-grained.

Primitives

Built-in types

Predicates

The following predicates are available on every type.

nix/isnot

Inverts the following predicates.

pyow(1, pyow.number.nix.infinite)
pyow(1, pyow.number.isnot.infinite)

pyow('', pyow.string.isnot.empty);
>>> ArgumentError: [NOT] Expected string to be empty, got ``

is_(fn)

Use a custom validation function. Return True if the value matches the validation, return False if it doesn't.

pyow(1, pyow.number.is_(lambda x: x < 10))

pyow(1, pyow.number.is_(lambda x: x > 10))
>>> ArgumentError: Expected `1` to pass custom validation function

Instead of returning False, you can also return a custom error message which results in a failure.

def greater_than(max_number: int, x: int):
	return x > max_number or f'Expected `{x}` to be greater than `{max_number}`'
};

pyow(5, pyow.number.is_(lambda x: greater_than(10, x)))
>>> ArgumentError: Expected `5` to be greater than `10`

Maintainers

Related

License

MIT

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

pyow-0.0.4.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyow-0.0.4-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file pyow-0.0.4.tar.gz.

File metadata

  • Download URL: pyow-0.0.4.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyow-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8e1911fa586c68dc213eb20fea374ffd2b17ead2bc96dd49e9ff78363fcce009
MD5 9f6d49b02ed5d8c9bbe0f8d6baedc68e
BLAKE2b-256 dae62160de3f74481e74743565543bcce68c386860d9e4b49425dead027cfdd5

See more details on using hashes here.

File details

Details for the file pyow-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pyow-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyow-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 852d73b39ea5cd57deb2d2e572220ac1a7b6278046e0a1e2d9d0a09b6b7781db
MD5 c4956c0987c5dde75933be333260950a
BLAKE2b-256 f929c2c1d9f4f7000cf2c6da82ac7630eafd8f96e1eae87624c0fd7c1758dbfe

See more details on using hashes here.

Supported by

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