Validation function at model field definition #9

Open
leplatrem opened this Issue May 25, 2012 · 9 comments

Projects

None yet

3 participants

@leplatrem
Member

Allow users to define their own validation function in the posted JSON model definition :

fields: [
        {
            type: "int",
            name: "age",
            description: "Your mother age",
            validation: "if (value < 15) { 
                                return 'Seems a bit young.';
                             }
                             else (value > 120) {
                                return 'Can't be so old.';
                             }
                             return null;",
        },

Most appropriate language seems to be Javascript (since JSON).

Javascript syntax must be checked during model definition.

Validation function should be executed on model post. In my opinion, server side (thus in python) :

http://renesd.blogspot.fr/search/label/shitjs

https://bitbucket.org/pypy/lang-js/src/de89ec32a7dc/js/javascript-interpreter.txt

http://code.google.com/p/pyv8/ : extra dep :(

@AntoineCezar
Contributor

And I prefer this to be agnostic even if it will not solve all the cases.

Exemple 1:

fields: [
    {
        type: "int",
        name: "age",
        description: "Your mother age",
        max: 120,
        min: 15
    },

Exemple 2:

fields: [
    {
        type: "int",
        name: "age",
        description: "Your mother age",
        in: [2, 3, 5],
    },

If age is not valid it returns a 400 type response:

errors: [
    {
        field: "age",
        message: "..."
    }
]
@almet
Member
almet commented Mar 20, 2013

I'm wondering if we shouldn't just use Javascript schema here, I've heard that it's not too complicated to parse; I don't know if that's the same story for its generation

@leplatrem
Member

@AntoineCezar The two examples you gave are already covered by either RangeField or EnumField.

With validation functions I had something more elaborate, even something like cross fields validation !

@ametaireau Do you mean JSON schema ?

@almet
Member
almet commented Mar 20, 2013

yep, json schema.

@leplatrem
Member
   validation: "return (fields.single || fields.wedding_date > 1900)"

Parsed and executed in python, or JS executed server-side with http://code.google.com/p/pyv8/

@leplatrem
Member

Seems interesting indeed, but it looks like a different story : "replacing current formalism for model definitions by JSON schema".
This one was dedicated to "custom validation logic between fields". Should I rename it and create an another one?

BTW, transforming current JSON to Backbone forms is currently done like this:
https://github.com/leplatrem/daybed-map/blob/master/backbone-daybed.js#L151-L204

Nothing extra is required for models. Except we want to have stuff like derived types (colour stored in string field) :
https://github.com/leplatrem/daybed-map/blob/master/app.js#L23-L31

@AntoineCezar
Contributor

The title is "Validation function at model field definition" not "custom validation logic between fields". So It contains the problem and the solution. Renaming may be necessary.

I'd like to avoid a language specific solution, but if there no other way let's do it in JavaScript.

@leplatrem
Member

Completely agree with what's being said here.

Regarding the original issue, adding custom validation between fields at model definition level is probably a bad idea. @AntoineCezar pointed this out with the "password confirmation" example (two fields in form, validated together, but not relevant in the model itself).

BTW Chris Mcdonough said decoupling models from forms was a good idea :) http://pyvideo.org/video/1407/about-django-from-the-pyramid-guy

This custom validation between fields may then be left to client-side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment