Search code examples
pythonpython-3.xvalidationsanic

sanic-validation Python AttributeError: 'Request' object has no attribute 'raw_args'


I am trying to use request validation package sanic-validation in a simple Sanic webserver.

Using sanic 20.3.0, sanic-validation 0.4.4 and Python 3.8.3.

from sanic import Sanic
from sanic.response import json
from sanic_validation import validate_args

app = Sanic()

query_schema = {
    'name': {'type': 'string', 'required': True}
}

@app.route('/query', methods=['GET'])
@validate_args(query_schema)
async def query(request):
    print(request.args)
    name = request.args['name'][0]

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

However, sending a GET request to the /query endpoint results in the following error in the Python terminal:

Traceback (most recent call last):
  File "/Users/n/opt/anaconda3/envs/test/lib/python3.8/site-packages/sanic/app.py", line 971, in handle_request
    response = handler(request, *args, **kwargs)
  File "/Users/n/opt/anaconda3/envs/test/lib/python3.8/site-packages/sanic_validation/decorators.py", line 59, in wrapper
    validation_passed = validator.validate(request.raw_args)
AttributeError: 'Request' object has no attribute 'raw_args'

Why is this error occuring, and how can we fix this? Thanks!


Solution

  • It seems that you use an old version of the sanic-validation repository. As you can see in your exception:

    line 59, in wrapper AttributeError: 'Request' object has no attribute 'raw_args'

    Basically, the error says that it doesn't familiar with the raw_args attribute in the request object. I've checked the code of the decorator in the sanic-validation repository, and it seems that they changed it to query_args attribute instead (also in line 59).

    So, maybe you should reinstall that package or you can just copy the code from GitHub and post it locally.