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 list
s, dict
s 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 optionaldata
parameter for automatic validation andexact_match
parameter specifying whether the data can contain keys validator doesn't contain__call__
(self, data) - validate data (called automatically whendata
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 inValidator
class__call__
(self, req, resp, *args, **kwargs) - method provides wrapper request data parsing - providesdata
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
- pythontype
that testing value must matchoptional
- specifies whether rule is mandatory (boolean
, default=False)min
- specifies min value for typesint
orfloat
(int
|float
, optional)max
- specifies max value for typesint
orfloat
(int
|float
, optional)length
- specifies length forstr
orbytes
(int
, optional)min_length
- specifies minimal length forstr
orbytes
(int
, optional)max_length
- specifies maximal length forstr
orbytes
(int
, optional)regex
- specifies regex to test forstr
orbytes
(Pattern
, optional)check_method
- specifiesfunction
(orlambda
) for more complex value check with one parameter (value) that returnsboolean
(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
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86fbb464907b3786c49fd18f738ac8e64af5b7addc1d6f13ea726ab7d3ebeee2 |
|
MD5 | 9e7b2c87f0f20212bcd74a7a8ecd648b |
|
BLAKE2b-256 | c582622dfbabb8704ca531e9ed62053aa501e132ad92389e4c7f6b8f01847ae1 |
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1355e66aea499ec64585d9f189760f868e3a77223994a9bda35fab5a4aa98d9 |
|
MD5 | 8aeb5493ffde95bdd93bbaaa6890b531 |
|
BLAKE2b-256 | d15a9f7d59e41b22a91a6f219e36c8c706c5364dbfa82da05d6aa2e31bc690e4 |