JSON schema не проходит валидацию
написал простой JSON api которое должно умножать цифры, но почему не работает когда отправить не полный запрос типа:
curl -X POST -d '{"token": 1, "a": 1}' http://localhost:6543/product
curl -X POST -d '{ }' http://localhost:6543/product
Он возвращает не ошибку 400, а 500. А иногда даже на правильный запрос отправляет или 400 или 500
Правильный запрос выглядит так:
curl -X POST -d '{"token": 1, "a": 1, "b": 1}' http://localhost:6543/product
Код:
from flask import Flask, request, abort, jsonify
from jsonschema import validate, ValidationError
POSITIVE_INTEGER_SCHEMA = {
"type": "integer",
"exclusiveMinimum": 0
}
def object_schema(*properties):
return {
"type": "object",
"properties": {key: POSITIVE_INTEGER_SCHEMA for key in properties},
"additionalProperties": False,
"requiredProperties": properties
}
INPUT_SCHEMA = object_schema("token", "a", "b")
OUTPUT_SCHEMA = object_schema("token", "product")
validate(instance={
"token": 1234567890,
"a": 4718923648912376,
"b": 4710943190713794
}, schema=INPUT_SCHEMA)
app = Flask(__name__)
@app.route('/product', methods=['POST'])
def product():
json = request.get_json(force=True)
if not json:
abort(400)
try:
validate(instance=json, schema=INPUT_SCHEMA)
except ValidationError as e:
print("invalid: ", e)
abort(400)
else:
token, a, b = (json[k] for k in ("token", "a", "b"))
r = {"token": token, "product": a*b}
validate(instance=r, schema=OUTPUT_SCHEMA)
return jsonify(r)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
server = make_server('', 6543, app)
server.serve_forever()