
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
flask-jsonschema-validator
Advanced tools
Validate Flask JSON request data with schema files and route decorators
Validate Flask JSON request data with schema files and route decorators.
Daniel 'Vector' Kerr (vector@vector.id.au)
Refer to LICENSE.txt.
pip install flask-jsonschema-validator
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 )
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" ]
}
}
python main.py
POST /register HTTP/1.0
Content-Type: application/json
Content-Length: 76
{
"name": "fred",
"email": "fred@foo.com",
"password": "frediscool"
}
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 )
FAQs
Validate Flask JSON request data with schema files and route decorators
We found that flask-jsonschema-validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.