spiral-project / daybed

Form validation and data storage API
http://daybed.rtfd.org/
BSD 3-Clause "New" or "Revised" License
53 stars 8 forks source link

Validation function at model field definition #9

Open leplatrem opened 12 years ago

leplatrem commented 12 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

@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 commented 11 years ago

yep, json schema.

leplatrem commented 11 years ago
   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/

AntoineCezar commented 11 years ago

Json Schema good parts:

leplatrem commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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.