Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Validation function at model field definition #9

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

Validation function at model field definition #9

leplatrem opened this issue May 25, 2012 · 9 comments

Comments

@leplatrem
Copy link
Contributor

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
Copy link
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
Copy link
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
Copy link
Contributor Author

@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
Copy link
Member

almet commented Mar 20, 2013

yep, json schema.

@leplatrem
Copy link
Contributor Author

   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
Copy link
Contributor

Json Schema good parts:

@leplatrem
Copy link
Contributor Author

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
Copy link
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
Copy link
Contributor Author

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants