pyschemavalidator
This package contains a decorator for endpoints in flask and a way to validate dictionary/JSON elements.
It omits the need to validate the data yourself and allow its usage by other kinds of APIs like graphql through the UniversalValidator.
Installation
Use pip to install the package from PyPI:
pip install pyschemavalidator
Usage
This package provides a flask route decorator to validate a JSON payload or dictionary elements.
from flask import Flask, make_response, jsonify, g, url_for
from pyschemavalidator import validate_param
# example imports
from models import Model
app = Flask(__name__)
@app.route('/invocations', methods=['POST'])
@validate_param(key="sepal_length", keytype=float, isrequired=True)
@validate_param(key="sepal_width", keytype=float, isrequired=True)
@validate_param(key="petal_length", keytype=float, isrequired=True)
@validate_param(key="petal_width", keytype=float, isrequired=True)
def register():
# if the payload is invalid, the request will be aborted with the appropriate error code
# do model inference
data = request.get_json(silent=True, force=False)
output = Model.predict(data.get('sepal_length'), data.get('sepal_width'), data.get('petal_length'), data.get('petal_width'))
return make_response(jsonify({"output:": output}), 200)
The payload is verified through the parameters set on the decorators. If the body does not meet the decorator's specifications it returns a standard response with the appropriate error code.
You can also use the package without the decorator style as below:
from pyschemavalidator.validators import UniversalValidator
# Creates a Universal validator to the dictionary/JSON elements
request_validator = UniversalValidator()
request_validator.add(key="example1", keytype=str, isrequired=True)
request_validator.add(key="example2", keytype=str, isrequired=False)
# Example data
data = {"example1": "test", "example2": "test"}
# Add some validations to the JSON/dictionary element
status_code, message = request_validator.validate(
tag1=data.get("example1"),
tag2=data.get("example2")
)
if status_code != 200: # It means that something got wrong with the validation
raise ValueError(message)
else
... # Do whatever you want
Mimetype checking
As of 1.2.0 this decorator uses flask.request.get_json(force=False) to get the data. This means the mimetype of the request has to be 'application/json'.
Error handling
On validation failure, the library calls flask.make_response and passes the error code and the message.
Testing
The following are the steps to create a virtual environment into a folder named "venv" and install the requirements.
# Create virtualenv
python3 -m venv venv
# activate virtualenv
source venv/bin/activate
# update packages
pip install --upgrade pip setuptools wheel
# install requirements
python setup.py install
Tests can be run with python setup.py test when the virtualenv is active.
Changelog
1.0.4 - Removed support to Python 2.7
1.0.3 - Fix description and README
1.0.2 - Fix the missing required parameter issue
1.0.1 - Fix the bonduaries issue
1.0.0 - First release
Metadata
Release files for pyschemavalidator 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyschemavalidator-1.0.4.tar.gz | 6.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyschemavalidator-1.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.8 kB
Release files / pyschemavalidator-1.0.4.tar.gz
| Download URL | pyschemavalidator-1.0.4.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1198e6098f8fd0cba9792005416fc682bb8e613a591f5eb982815b81fc9c8f02
|
|
BLAKE2b-256 checksum How to use checksums |
9bee60b137c691fcfcaf31dff08989090c16d7dd2a320c18dbc8bec1a67659fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
|
Release files / pyschemavalidator-1.0.4-py3-none-any.whl
| Download URL | pyschemavalidator-1.0.4-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14ec4e98d61834ac0f582b0fbf4170c2702b8b243c629a4999110c287221f2b1
|
|
BLAKE2b-256 checksum How to use checksums |
56dda32fbede2ee4e183047e787fdacc59154800ea12a16ddc7f8dec7299bd76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
|
Security for pyschemavalidator 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.