Sanic: Is there validator in sanic or had extensions already?

Created on 17 Mar 2017  路  8Comments  路  Source: sanic-org/sanic

I think it is maybe like a restful validator. not binding a form.

Most helpful comment

I'm glad to adapt https://github.com/guyskk/validr for sanic, validr is the fastest validation library currently (cython implement version will release these days).

All 8 comments

Could you provide an example of what you would like to do?

I had a try like this.

from sanic import Sanic
from sanic.response import json
from validator.validator import validator
from request_validators import MyValidator

def error_handle(request):
    return json({'this': 'is error handle'})

app = Sanic()

@app.route("/")
async def index(request):
    return json({"hello": "world"})

@app.route('/form')
@validator(MyValidator(), error_handle) # error_handle is not required
async def form(request):
    return json({'a': 'a'})

MyValidator like this.

from validator.validator import ValidatorClass
from validator.types import StringType
from validator.types import IntegerType
from validator.types import RequiredType

class MyValidator(ValidatorClass):
    name = [
        StringType(),
        RequiredType()
    ]
    age  = [
        IntergerType()
    ]

the main validator like this.

from functools import wraps
from sanic.response import json

def validate(validator_class, request):
    """
    handle validator_class.
    """
    errors = {}
    # here do validators...

    if len(errors):
        return False, errors
    return True, errors

def validator(validator_class, error_handle_func=None):
    """
    Add validator to a request.
    validator_class is required,
    error_handle_func is not required,
    if no error_handle_func, it will return json errors.
    """
    def decorate(func):

        @wraps(func)
        def wrapper(request, *args, **kwargs):

            is_valid, errors = validate(validator_class, request)
            if is_valid:
                return func(request, *args, **kwargs)

            elif error_handle_func:
                return error_handle_func(request)

            else:
                return json(errors)

        return wrapper
    return decorate

how about it?

i used schematic for this (http://schematics.readthedocs.io/en/latest/) is a lot like what you want, the only problem i found is that schematics is not async and doesnt support asyncio.

I've a file where i define all my models, and after that in controllers all you have to do is import that file, instantiate that class passing the request.form and validate it. If you need more info tell me

It would be very nice to implement something like schematics in sanic, i'll try to take a look and share it here

I had read about it quickly. and, it is binding to a model as many orm always do, it is a good choice, but, when I only want to vaildate a single form or some litter fields, maybe it is not simple and restful enough?.

Just make a PR on webargs to include sanic support.

I'm glad to adapt https://github.com/guyskk/validr for sanic, validr is the fastest validation library currently (cython implement version will release these days).

Is this still an issue? Seems like 2 solutions provided which are unrelated to issues with actual Sanic. Or is this a feature request?

I found this little badboy when looking into this issue.
It suits me for now and it is built on cerbus which seems to be quite powerful.

I'm considering adding support for validating URL parameters as well.

Was this page helpful?
0 / 5 - 0 ratings