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 = 1
INVALID_TYPE = 2
INVALID_VALUE = 3
NUMBER_NOT_IN_RANGE = 4
LENGTH_NOT_IN_RANGE = 5
INVALID_SCHEMA = 6
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.