Server-Side Validation

536 views
Skip to first unread message

George Hemmings

unread,
Jun 25, 2012, 5:43:36 PM6/25/12
to backbon...@googlegroups.com
Hi

I'm just getting started with this library, does it support server-side validation?

Thanks
George

Charles Davison

unread,
Jun 25, 2012, 9:55:06 PM6/25/12
to backbon...@googlegroups.com
It's mainly for client side apps, particularly single page apps, though it could in theory be possible to use some of the functionatliy in a NodeJS server-side app.

George Hemmings

unread,
Jun 27, 2012, 8:14:17 AM6/27/12
to backbon...@googlegroups.com
I'm using it for client-side validation but I have server-side validation as well. Say I have a unique constraint on a column, this is validated on the server-side and I'm return a 422 error. I need to plug-in to the models errors and add in the server error so the forms shows it. How would I go about this?

Bruno Prieto Reis

unread,
Jun 28, 2012, 10:12:30 PM6/28/12
to backbon...@googlegroups.com
I was thinking about creating something like this project until now that I found it. Congratulations to the author for the good job.

Answering this question about server-side validation, one thing I think that is crucial for a project like this to work is to have a well known and defined schema. Why? Because it would allow servers to write their own validation according to the schema definition.

Let´s take as an example this JSON Schema:

http://tools.ietf.org/html/draft-zyp-json-schema-03
http://json-schema.org/

Since this is a common schema type (and I think a very good one, extensible and flexible), you can count on a lot of already defined validators on your server:

http://json-schema.org/implementations.html.

When you do not have a trusted schema definition, you cannot count on it to write any validators. Do you agree?

George Hemmings

unread,
Jun 29, 2012, 4:04:54 AM6/29/12
to backbon...@googlegroups.com
For external APIs I can see how this would be useful. However, my JSON API is internal, I know what validation rules will be applied on the server because I wrote them :)

I just want to add validation errors to my model when the server returns an error on the creation of a resource.

Bruno Reis

unread,
Jun 29, 2012, 7:28:09 AM6/29/12
to backbon...@googlegroups.com
Do you need the same schema and validation to be applied on both server and client?

2012/6/29 George Hemmings <g...@georgehemmings.com>

Jeff Garbers

unread,
Jul 29, 2012, 11:33:55 PM7/29/12
to backbon...@googlegroups.com
George, I'm just getting started, but I have something working like what you're talking about. I'm using the 'jsonschema' Python package to do server-side validation.  I set up my server side code to return a JSON-encoded entity that has the same structure as the error objects that Backbone generates itself during validation. If I'm remembering right, that structure is an array of error objects, and each object has a property where the name of the property is the name of the attribute that failed validation, and the value is the error message -- something like this:

    [{address: 'Must be less than 40 characters'}, {zip: 'Invalid zip code'}]

(Please double-check this - I'm pretty sure it's correct but, you know, I'm new, blah blah).

In any case, having received the error object at my 'error' callback from the 'save' call, I convert the responseText to an object (jQuery $.ajax will *not* automatically decode a returned entity of Content-Type:application/json if the status code indicates an error).  I then borrowed some logic from around line 250 of backbone-forms.js to set the error messages on the various fields -- here's my variant:

    // Given a form ana a list of errors, show the ones associated with 
    // form fields, and return the ones that weren't.
    function show_form_errors(form, errors) {
        var others = []
        _.each(errors, function(message, key) {
            if (form.fields[key]) {
                form.fields[key].setError(message)
            } else {
                others.push(message)
            }
        })
        return others
    }

"others" are the errors whose property names didn't correspond to field names. You'll have to decide what to do with those (I show 'em in a popup dialog).

I hope this is helpful, but please do take it with a grain of salt, as i've only been using Form for a couple of days. Perhaps more experienced folks here will jump in and help us both figure out a better way to do this.
Reply all
Reply to author
Forward
0 new messages