# Validator
This is a request handler validation tool for RESTful API endpoints.
## How To Use
```python
from validrequest import validator, ValidationError
from typing import Dict, Any
def request_handler(request, response, next):
# Note: type and state must be the first rules in that order
# i.e. type (string,float,integer,number,dict)|state (required,sometimes)
validation_rules: Dict[str, str] = {
"q": "string|required|max:100",
"timestamp": "float|sometimes|min:1|max:13",
"comment": "string|required|min:10",
"code": "integer|required",
"version": "float|sometimes",
"metadata": "dict|required",
"timeSince": "number|required"
}
request_parameters: Dict[str, Any] = request.body
try:
validator(validation_rules, request_parameters)
return response.status(200).send("Validation successful.")
except ValidationError as e:
return next({ "message": e })
except Exception:
return next({ "message": "Something else went wrong." })
# You can also use a decorator (@validate) - see demo.py for an example
```
Refer to the ```demo.py``` file for further implementations.
## How to Install
```bash
python -m pip install validrequest
```
```bash
python -m pip install git+https://github.com/ableinc/validrequest.git
```
## Important - Migrating from v0.0.3 to v1.0.0
**Breaking Changes** in Version 1.0.0 - Please carefully review the changes below
- import library name has changed from "validator" to "validrequest":
```python
from validrequest import validate, validator, ValidationError
```
- @validate decorator now accepts 'request' or 'req' as Request argument name
- parse_level was changed to payload_level for @validate decorator
- error callback must be named "next" for @validate decorator - it will not accept any other argument
- error callback will return a string error message with error type - it will no longer return JSON
- strict declaration has been removed - validation rules will always be checked against required and ignored for sometimes
- 'number' is now an acceptable type in validation rule - this can be used for fields that may be either integer or float
- Acceptable types in validation rules:
- Integer: int | integer | number
- Float: float | number
- String: str | string
- Dictionary: dict | dictionary | object
- Boolean: bool | boolean
Raw data
{
"_id": null,
"home_page": "https://github.com/ableinc/validrequest",
"name": "validrequest",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "validation tool,validation,api,restful,endpoints,ableinc",
"author": "AbleInc - Jaylen Douglas",
"author_email": "douglas.jaylen@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/fd/1a/228ba25a5d0d4e75e0e25e571e409b9a93b69339660ea1bc9902cf1a6173/validrequest-1.0.0.tar.gz",
"platform": null,
"description": "# Validator\n\nThis is a request handler validation tool for RESTful API endpoints.\n\n## How To Use\n\n```python\nfrom validrequest import validator, ValidationError\nfrom typing import Dict, Any\n\ndef request_handler(request, response, next):\n # Note: type and state must be the first rules in that order\n # i.e. type (string,float,integer,number,dict)|state (required,sometimes)\n validation_rules: Dict[str, str] = {\n \"q\": \"string|required|max:100\",\n \"timestamp\": \"float|sometimes|min:1|max:13\",\n \"comment\": \"string|required|min:10\",\n \"code\": \"integer|required\",\n \"version\": \"float|sometimes\",\n \"metadata\": \"dict|required\",\n \"timeSince\": \"number|required\"\n }\n request_parameters: Dict[str, Any] = request.body\n try:\n validator(validation_rules, request_parameters)\n return response.status(200).send(\"Validation successful.\")\n except ValidationError as e:\n return next({ \"message\": e })\n except Exception:\n return next({ \"message\": \"Something else went wrong.\" })\n\n # You can also use a decorator (@validate) - see demo.py for an example\n```\nRefer to the ```demo.py``` file for further implementations.\n\n## How to Install\n\n```bash\npython -m pip install validrequest\n```\n\n```bash\npython -m pip install git+https://github.com/ableinc/validrequest.git\n```\n\n## Important - Migrating from v0.0.3 to v1.0.0\n\n**Breaking Changes** in Version 1.0.0 - Please carefully review the changes below\n\n- import library name has changed from \"validator\" to \"validrequest\":\n ```python\n from validrequest import validate, validator, ValidationError\n ```\n- @validate decorator now accepts 'request' or 'req' as Request argument name\n- parse_level was changed to payload_level for @validate decorator\n- error callback must be named \"next\" for @validate decorator - it will not accept any other argument\n- error callback will return a string error message with error type - it will no longer return JSON\n- strict declaration has been removed - validation rules will always be checked against required and ignored for sometimes\n- 'number' is now an acceptable type in validation rule - this can be used for fields that may be either integer or float\n- Acceptable types in validation rules:\n - Integer: int | integer | number\n - Float: float | number\n - String: str | string\n - Dictionary: dict | dictionary | object\n - Boolean: bool | boolean\n",
"bugtrack_url": null,
"license": "GNU General Public License v3 (GPLv3)",
"summary": "A request handler validation tool for RESTful API endpoints.",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/ableinc/validrequest"
},
"split_keywords": [
"validation tool",
"validation",
"api",
"restful",
"endpoints",
"ableinc"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "51b6038e4f70c7113c8347f3b7deadd3bd8634f62df9ed7210d9919aa6286058",
"md5": "2c4302fe942be788459b7643fd2eac34",
"sha256": "31aeaecf2656a3feea1d8a03fcf935f418229446eafdfd9eeebdb6967b8cdd7f"
},
"downloads": -1,
"filename": "validrequest-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2c4302fe942be788459b7643fd2eac34",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7991,
"upload_time": "2023-11-04T15:33:43",
"upload_time_iso_8601": "2023-11-04T15:33:43.898834Z",
"url": "https://files.pythonhosted.org/packages/51/b6/038e4f70c7113c8347f3b7deadd3bd8634f62df9ed7210d9919aa6286058/validrequest-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fd1a228ba25a5d0d4e75e0e25e571e409b9a93b69339660ea1bc9902cf1a6173",
"md5": "80763fb1ebc43f85ae0f2d8b4dd9a9fd",
"sha256": "cec2efcbee2d1db9012499044e8e20daa2aedbefcfe58c794565a78613e2795b"
},
"downloads": -1,
"filename": "validrequest-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "80763fb1ebc43f85ae0f2d8b4dd9a9fd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4354,
"upload_time": "2023-11-04T15:33:46",
"upload_time_iso_8601": "2023-11-04T15:33:46.381658Z",
"url": "https://files.pythonhosted.org/packages/fd/1a/228ba25a5d0d4e75e0e25e571e409b9a93b69339660ea1bc9902cf1a6173/validrequest-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-04 15:33:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ableinc",
"github_project": "validrequest",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "validrequest"
}