Skip to main content

Validator

This is a request handler validation tool for RESTful API endpoints.

How To Use

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

python -m pip install validrequest
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":
    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

Metadata

Release files for validrequest 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for validrequest 1.0.0
File Size Uploaded
validrequest-1.0.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for validrequest 1.0.0
File Interpreter ABI Platform
validrequest-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / validrequest-1.0.0.tar.gz

Download URL validrequest-1.0.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cec2efcbee2d1db9012499044e8e20daa2aedbefcfe58c794565a78613e2795b
BLAKE2b-256 checksum
How to use checksums
fd1a228ba25a5d0d4e75e0e25e571e409b9a93b69339660ea1bc9902cf1a6173
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / validrequest-1.0.0-py3-none-any.whl

Download URL validrequest-1.0.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31aeaecf2656a3feea1d8a03fcf935f418229446eafdfd9eeebdb6967b8cdd7f
BLAKE2b-256 checksum
How to use checksums
51b6038e4f70c7113c8347f3b7deadd3bd8634f62df9ed7210d9919aa6286058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page