Reputation: 79
I'm trying to enable flask_inputs with JSONSchema validation for a Restful API. The code looks like this:
from flask_inputs import Inputs
from flask_inputs.validators import JsonSchema
def GetValidSchema():
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'}
}
}
return schema
class ApiResource(resource)
class ApiInputs(Inputs):
schema = InputsValidation.GetValidSchema() # this returns the JSON schema object
json = [JsonSchema(schema=schema)]
def post(self):
inputs =self.ApiInputs(request)
print inputs.validate()
api.add_resource(ApiResource,'/test')
I really need some help as I've tried to trace out the WT-Forms but unfortunately I cannot get a clear conclusion why I'm getting the following error:
File "/home/seelview/run.py", line 153, in post
inputs.validate()
File "/usr/lib/python2.7/site-packages/flask_inputs/inputs.py", line 76, in validate
if not form.validate():
File "/usr/lib/python2.7/site-packages/wtforms/form.py", line 152, in validate
if not field.validate(self, extra):
File "/usr/lib/python2.7/site-packages/wtforms/fields/core.py", line 204, in validate
stop_validation = self._run_validation_chain(form, chain)
File "/usr/lib/python2.7/site-packages/wtforms/fields/core.py", line 225, in _run_validation_chain
validator(form, self)
TypeError: 'str' object is not callable
Any idea what I'm doing wrong or what I'm missing ?
Upvotes: 1
Views: 3154
Reputation: 168716
You are polluting the ApiInputs
namespace with something other than a validator. Try this instead:
class ApiInputs(Inputs):
json = [JsonSchema(schema=GetValidSchema())]
Sample program:
from flask import Flask, request
from flask_restful import Resource, Api
from flask_inputs import Inputs
from flask_inputs.validators import JsonSchema
app = Flask(__name__)
api = Api(app)
def GetValidSchema():
return {
'type': 'object',
'properties': {
'name': {'type': 'string'}}}
@app.route('/')
def clickme():
return '''
<form action="/test" method="POST">
<input type=submit value="Click Me" />
</form>'''
class ApiResource(Resource):
class ApiInputs(Inputs):
json = [JsonSchema(schema=GetValidSchema())]
def post(self):
inputs = self.ApiInputs(request)
print inputs.validate()
api.add_resource(ApiResource, '/test')
if __name__ == '__main__':
app.run(debug=True)
Upvotes: 2