lets check flask parameters
Project description
Flask-value-checker :mag_right:
Imaging web form checking, but now imagine that it was easy and comfy
Example usage
from flask_value_checker import Invigilator
from flask import Flask, request
invigilator = Invigilator()
@app.route('/abc', methods=['POST'])
@invigilator.check(
'POST',
'''
username : str/lenlim(5, 15)
password : str/lenlim(8, inf)
stayLoggedIn : str/accept(['on'])/optional
'''
)
def abc():
stay_logged_in = request.form.get('stayLoggedIn', 'off')
return f'hi {request.form['username']}, stay logged in: {stay_logged_in}'
app.run()
example default error
Note: this error can be customized
{
"error": {
"code": "MALFORMED_OR_MISSING_PARAMETERS",
"message": "one or more fields we're either missing or malformed",
"fields": {
"email" : "missing parameter, parameter is required",
"firstName" : "string length must be between 5 and inf",
"age" : "parameter has to be of type 'int'"
...
}
}
}
function docs :notebook_with_decorative_cover: :notebook: :closed_book: :blue_book:
Invigilator(err_function=None)
- Type :
function
orNone
- Description : the function that displays the final error to the webpage, must be written the the way a standard flask function is written, (although you may wanna check out Flask.Response, and return that instead of a tuple like
(error, 400)
) - Example
def custom_error_shower(errors):
return Response(
json.dumps({"errors": errors,}), status=400, mimetype="application/json"
)
Invigilator.check(http_methods, checker_str)
http_methods:
- Type :
str
orlist of strs
- Description : HTTP methods to check for,
NOTE: if the http method to check for is not present in methods, the decorated function will be called normally and no checks will be performed
- Example :
'GET'
,'POST'
,['GET', 'POST']
field name attribute docs
all top attributes (str, int, float) should not have any parameters,
Note: top attributes should be placed first, then its sub attributes should be placed
str
lenlim(min, max)
the minimum and maximum length the fields string can be
- min :
int
or the valueinf
, the minimum accepted string length - max :
int
or the valueinf
(see example-usage), the maximum accepted string length
optional
is the attribute optional ?
accept(accepted_vals)
values that can be accepted when using the field name
- accepted_vals:
list of strings
, the acceptable values for the parameter
int and float
int specifies that the number must be an integer,
float specifies that it can be decimal,
both attributes have the same sub-attributes
lim(min, max)
the limits that the numeric values can range between
- min :
float
or the valueinf
, the minimum accepted numeric value - max :
float
or the valueinf
(see bigger-full-example), the maximum accepted numeric value
optional
is the attribute optional ?
checker_str
- Type :
str
- Description : the form attributes and their restrictions written in the prescribed format, See Here
Guide :metal:
Writing parameters:
- different parameters are separated by a newline
- the parameter and its rules are separated by an
:
- rule conditions are separated by an
/
- the first condition should be the type of the required value,
str
,int
orfloat
- condition arguments are put in brackets
()
example:
score : float/lim(0, 11.5)/optional
bigger full example
@app.route('/abc')
@invigilator.check(
'POST',
'''
firstName : str/lenlim(1, 15)
middleName : str/optional
lastName : str/optional
stayLoggedIn : str/accept(['on'])/optional
email : str
password : str/lenlim(8, inf)
phone : str/lenlim(8, 15)
# number will have to be an Int,
# but it'll have to be greater than
# 18, not including 18
age : int/lim(18.9, inf)
score : float/lim(0, 10) # can be a decimal value
'''
)
def abc():
some_content
Dev-docs
- codestyle : black
- documentation style : numpydoc
- HTTP-Returns extra to Numpydoc, that is similar to Return, but is represented as follows
'''
HTTP-Returns
------------
400
on failure, the response will be similar to,
{
"error": {
"code": "MALFORMED_OR_MISSING_PARAMETERS",
"message": "one or more fields we're either missing or malformed",
"fields": {
"email" : "missing parameter, parameter is required",
"firstName" : "name has to be under 15 characters",
"age" : "parameter has to be of type 'int'"
...
}
}
}
*
or whatever the original function returns
*
'''
i.e.
<return code>
<details>
<return message>
Project details
Release history Release notifications | RSS feed
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 flask-value-checker-1.1.tar.gz
.
File metadata
- Download URL: flask-value-checker-1.1.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2330453dc5e18e1d03f371572babf5ccd0ca14f533c32ce58dd5e984d338fab0 |
|
MD5 | 040437fde2621e9c7fd4873109733e9a |
|
BLAKE2b-256 | 4fae6fb00e884883110c4fb829257af050f186e5d4d4779855ce810535cfff44 |
File details
Details for the file flask_value_checker-1.1-py3-none-any.whl
.
File metadata
- Download URL: flask_value_checker-1.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c4a785606843842363d8ac126691573213068721d3f63b65eea625356fb5ea1 |
|
MD5 | 005827b0638b29aa4fc0e4235257505d |
|
BLAKE2b-256 | 69ae74845a883a717efe7ffc8158c86b721acd0f4d005269343fe8a7a0559eda |