Skip to main content

Additional functions for weepy framework.

Project description

weelib

(experimental)

Library of additional functions for weepy framework.

Installation

Installation will not work - library has not been released, yet.
If you want to try functionality just clone git repo.


Run installation command (requires python>=3.10):
$ pip install weelib

Using

Import library and use its functions

import weelib

Functionality

Validator

This functionality provides data validation for lists, dicts and with extended class also decorator for validation requests HTTP BODY.
Simply inherit Validator class or RequestValidator class depending on what type validation you need.
Create class variables usin Rule pattern to create indivitual validation rules. When you want validate input simply create instance of your class or (with RequestValidator) use class instance as decorator.

Validator methods and properties:

  • __init__(self, data=None, exact_match) - class constructor accepts optional data parameter for automatic validation and exact_match parameter specifying whether the data can contain keys validator doesn't contain
  • __call__(self, data) - validate data (called automatically when data parameter passed into __init__) - returns tuple (status, error<None | ValidationError | str>, key<str | None>)
  • _init_validation_result - saved return_value from __call__ when data passed to __init__ directly ValidationError contents:

RequestValidator methods and properties:

  • __init__(self, exact_match) - decorator constructor - exact_match parameter is same as in Validator class
  • __call__(self, req, resp, *args, **kwargs) - method provides wrapper request data parsing - provides data parameter with class instance value to decorated function
  • _init_validation_result - saved return_value from __call__ when data passed to __init__ directly

Rule constructor parameters:

  • data_type - python type that testing value must match
  • optional - specifies whether rule is mandatory (boolean, default=False)
  • min - specifies min value for types int or float (int | float, optional)
  • max - specifies max value for types int or float (int | float, optional)
  • length - specifies length for str or bytes (int, optional)
  • min_length - specifies minimal length for str or bytes (int, optional)
  • max_length - specifies maximal length for str or bytes (int, optional)
  • regex - specifies regex to test for str or bytes (Pattern, optional)
  • check_method - specifies function (or lambda) for more complex value check with one parameter (value) that returns boolean (Callable, optional)

ValidationError provides enum of basic errors that may occur during validation -intended for extending to meet requirements of _validate wraps.
ValidationError contents:

from enum import Enum


class ValidatorError(Enum):
	MISSING_KEY = "MISSING_KEY"
	INVALID_TYPE = "INVALID_TYPE"
	INVALID_VALUE = "INVALID_VALUE"
	NUMBER_NOT_IN_RANGE = "NUMBER_NOT_IN_RANGE"
	LENGTH_NOT_IN_RANGE = "LENGTH_NOT_IN_RANGE"
	INVALID_SCHEMA = "INVALID_SCHEMA"

General example:

import re
from weelib.validator import Validator, Rule

password = "dude"


class MyDataValidator(Validator):
	# for clarity is regex simplified
	email = Rule(str, regex=re.compile(r".+\@.+\..+"))
	password = Rule(str, min_length=6)
	age = Rule(int, min=18)
	height = Rule(float, optional=True, min=1)

	# following method is optional
	def _validate(data):
		if data["password"] == password:
			return True, None, ""
		else:
			return False, "INVALID_PASSWORD", "password"


person = MyDataValidator({
	"email": "test@test.com",
	"password": "dude",
	"age": 25
})
print("email", person.email)
print("age", person.age)
print("height", person.height or "not specified")

POST data parser example:

import re
from weepy import ASGI, Route
from weelib.validator import RequestValidator, Rule

password = "dude"


class MyRequestDataValidator(RequestValidator):
	# for clarity is regex simplified
	email = Rule(str, regex=re.compile(r".+\@.+\..+"))
	password = Rule(str, min_length=6)
	age = Rule(int, min=18)
	height = Rule(float, optional=True)

	# following method is optional
	def _validate(data):
		if data["password"] == password:
			return True, None, ""
		else:
			return False, "INVALID_PASSWORD", "password"


application = ASGI(content_type="application/json", allow="*")


@Route("/", methods=["POST"])
@MyRequestDataValidator()
async def info(req, resp, data=None):
	return {
		"email": data.email,
		"password-length": len(data.password),
		"age": data.age,
		"height": data.height or "not specified"
	}

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

weelib-0.1.7.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

weelib-0.1.7-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file weelib-0.1.7.tar.gz.

File metadata

  • Download URL: weelib-0.1.7.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for weelib-0.1.7.tar.gz
Algorithm Hash digest
SHA256 86fbb464907b3786c49fd18f738ac8e64af5b7addc1d6f13ea726ab7d3ebeee2
MD5 9e7b2c87f0f20212bcd74a7a8ecd648b
BLAKE2b-256 c582622dfbabb8704ca531e9ed62053aa501e132ad92389e4c7f6b8f01847ae1

See more details on using hashes here.

File details

Details for the file weelib-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: weelib-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for weelib-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c1355e66aea499ec64585d9f189760f868e3a77223994a9bda35fab5a4aa98d9
MD5 8aeb5493ffde95bdd93bbaaa6890b531
BLAKE2b-256 d15a9f7d59e41b22a91a6f219e36c8c706c5364dbfa82da05d6aa2e31bc690e4

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