Hey, guys,
Thanks for your effort! I found this package exactly is what I wanted. Here, I want to discuss several improvable functions or missing features.
How do you think about let Flask-Inputs also return the HTTP status code for error handling.
For now, Flask-Inputs only raise error message when validation failed. eg:
class CustomInputs(Inputs):
headers = {
'Content-Type': [
AnyOf(
('application/json', ),
message="Content-Type of Headers must be 'application/json'",
),
]
}
@app.route('/api', methods=('GET', 'POST')
def api_inference():
inputs = CustomInputs(request)
if not inputs.validate():
return jsonify(success=False, errors=inputs.errors)
Flask-Inputs works well and return a error message to client, if the request has a wrong Content-Type
. But the client still would receive status code 200
which indicates that the client's request was successfully received, understood, and accepted (according to RFC 2616). Actually, this request was just received but accepted. The correct status code should be 415
indicates that the origin server is refusing to service the request because the payload is in a format not supported.
Hence, what I expected is:
class CustomInputs(Inputs):
headers = {
'Content-Type': [
AnyOf(
('application/json', ),
message="Content-Type of Headers must be 'application/json'",
),
]
}
@app.route('/api', methods=('GET', 'POST')
def api_inference():
inputs = CustomInputs(request)
if not inputs.validate():
return jsonify(success=False, errors=inputs.errors), inputs.status_code
The major different is in second return variable of last line inputs.status_code
. This will make both client/server much easier to debug, check and log information.
I think this is really a valuable and necessary function.
Of course, there are also problems would be troubles when implementing this function:
(1) Priority of status code. If there are multiple wrong validations, which one should be returned. Two choices: a. Protocol sequence. Check headers first, then validate json or form or else things defined in HTTP protocol. Return first error status codes. b. Depending on implementation of codes, like follow valid_attrs = ['args', 'form', 'values', 'cookies', 'headers', 'json', 'rule']
attributes.
(2) The way to define or custom status code for specific validation. Where to define error status code. Here, I provide one solution. eg:
class CustomInputs(Inputs):
headers = {
'Content-Type': [
AnyOf(
('application/json', ),
message="Content-Type of Headers must be 'application/json'",
errorcode=415,
),
]
}
Now, validaors of Flask-Inputs are depending on wtforms
packages. If necessary, could we rewrite or reimplement ourself validators based on wtforms.validators
. (Yeah, sometimes, I ported several validators directly from source codes, becuase I didn't use package wtforms
in my codes.)
This probably isn't the best way. We can discuss to get a better solution to define status code for each validation.
Thanks for reading. Looking forwarding to knowing your opinions. Or whether developer accept pull request in this new feature.
Regards.