Skip to main content

Flask JSON Schema Validator

Validate Flask JSON request data with schema files and route decorators.

Author

Daniel 'Vector' Kerr (vector@vector.id.au)

License

Refer to LICENSE.txt.

Installation

pip install flask-jsonschema-validator

Sample Usage

Python Code

main.py

from flask import Flask, request, jsonify
from flask_jsonschema_validator import JSONSchemaValidator

app = Flask()
JSONSchemaValidator( app = app, root = "schemas" )

# Define a normal flask route, and then apply the `validate` decorator.
# Look for the `users.json` file, and use the `register` key as the schema source.
@app.route( '/register', methods = [ 'POST' ] )
@app.validate( 'users', 'register' )
def routeRegister():
  user = request.json
  return jsonify( user )

if __name__ == '__main__':
  app.run( port = 8080 )

Schema File

schemas/users.json

{
  "register": {
    "type": "object",
    "properties": {
      "name": { "type": "string", "minLength": 2, "maxLength": 100 },
      "email": { "type": "string", "format": "email" },
      "password": { "type": "string", "minLength": 8, "maxLength": 32 }
    },
    "required": [ "name", "email", "password" ]
  }
}

Run Server

python main.py

POST Data to Server

POST /register HTTP/1.0
Content-Type: application/json
Content-Length: 76

{
	"name": "fred",
	"email": "fred@foo.com",
	"password": "frediscool"
}

Handling Validation Errors

If the data validates correctly then the server will respond with the POST data as a JSON object.

If the data fails to validate, a jsonschema.ValidationError exception will be raised.

To handle the exception, you could register a Flask errorhandler. For example:

import jsonschema
from flask import Response

@app.errorhandler( jsonschema.ValidationError )
def onValidationError( e ):
  return Response( "There was a validation error: " + str( e ), 400 )

Metadata

Release files for flask-jsonschema-validator 0.0.4

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

Source distribution (sdist)

Source distribution for flask-jsonschema-validator 0.0.4
File Size Uploaded Provenance Actions
flask-jsonschema-validator-0.0.4.tar.gz 3.3 kB Without provenance Details for flask-jsonschema-validator-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-jsonschema-validator 0.0.4
File Interpreter ABI Platform Actions
flask_jsonschema_validator-0.0.4-py3-none-any.whl Python 3 none any Details for flask_jsonschema_validator-0.0.4-py3-none-any.whl

Total release size: 8.5 kB

Release files / flask-jsonschema-validator-0.0.4.tar.gz

Download URL flask-jsonschema-validator-0.0.4.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fe1ee91fa16c6bc3eccf53ec6aa60f19dc0c65501210910adb562179ce66c0d5
BLAKE2b-256 checksum
How to use checksums
bdb035f9f26d6efb6171da6236c07e5cbf73b66467c8954e983389d4656cbffa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

Release files / flask_jsonschema_validator-0.0.4-py3-none-any.whl

Download URL flask_jsonschema_validator-0.0.4-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
767439d796b36000283be84318dd25fca79dc6562f4acd4c6221ff09d19bc776
BLAKE2b-256 checksum
How to use checksums
03040dc5605ba099d5984791c5a7faf046285bd27eca0f13b2de20be42869800
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

Security for flask-jsonschema-validator 0.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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files No provenance

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