daybed icon indicating copy to clipboard operation
daybed copied to clipboard

Validation function at model field definition

Open leplatrem opened this issue 12 years ago • 9 comments

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 :(

leplatrem avatar May 25 '12 09:05 leplatrem

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: "..."
    }
]

AntoineCezar avatar Mar 20 '13 15:03 AntoineCezar

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

almet avatar Mar 20 '13 15:03 almet

@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 ?

leplatrem avatar Mar 20 '13 18:03 leplatrem

yep, json schema.

almet avatar Mar 20 '13 18:03 almet

   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 avatar Apr 29 '13 08:04 leplatrem

Json Schema good parts:

  • Automatic form generation: https://github.com/exavolt/onde Demo: http://exavolt.github.io/onde/#?schema_url=schemas/jquery-package.json
  • Form generation again: https://github.com/joshfire/jsonform
  • Schema editor: http://www.jsonschema.net/
  • Backbonejs integration https://github.com/redpie/backbone-schema

AntoineCezar avatar May 24 '13 14:05 AntoineCezar

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

leplatrem avatar May 25 '13 08:05 leplatrem

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.

AntoineCezar avatar May 28 '13 15:05 AntoineCezar

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.

leplatrem avatar May 30 '13 19:05 leplatrem