Massimo's method may be better -- I haven't personally experimented with the 
restful stuff yet. 

But you can still use SQLFORM or SQLFORM.factory with form.accepts() to run 
the validators. You just don't display the form anywhere if you don't need 
to. You also need to pass formname=None to accepts(). Doing this, 
form.accepts() will run your values through the validators, transform them 
as needed, and populate form.errors based on the GET and POST variables 
provided by whatever is calling your web service.

Here's a simple example I cobbled together that adds two numbers. Example 
use: http://localhost:8000/app/controller/add?val1=3.14&val2=2.718

def add():
    form = SQLFORM.factory(
        Field('val1', 'double', requires=IS_FLOAT_IN_RANGE(0, 10)),
        Field('val2', 'double', requires=IS_FLOAT_IN_RANGE(0, 10))
    )

    if form.accepts(request.vars, formname=None):
        return form.vars.val1 + form.vars.val2

    else:
        errors = "Errors:\n"
        for fieldname in form.errors:
            errors += "%s: %s\n" % (fieldname, form.errors[fieldname])

        raise HTTP(400, errors)


Cheers,
Kevin

Reply via email to