geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

Docs mistake with beforeValidate #243

Open OscarGodson opened 9 years ago

OscarGodson commented 9 years ago

I'm pretty sure this code in the docs doesn't work (its under https://github.com/geddy/model#lifecycle-events):

  this.beforeValidate = function (params) {
    params.name = params.name || 'Zerp Derp';
  };

Note that it's not returning anything nor does it set any params on any object like this.params. I didn't see any specs that actually check for this, but I could be missing it.

When I try that in my code my default params arent updated. If I have this code:

  this.beforeValidate = function (params) {
    params.permission_level = params.permission_level || 1;
  };

  this.defineProperties({
    permission_level: { type: 'number', required: true }
  });

I still get an error:

{ permission_level: '[[model.validatesPresent]]' }

For now I can use afterCreate and set it as required: false (since I'll be forcing a value if null). I can update the docs if the above is indeed incorrect. I couldn't figure out the right syntax tho.

mde commented 9 years ago

Weird, it looks like beforeValidate is getting called in the right place in the lifecycle:

https://github.com/geddy/model/blob/master/lib/index.js#L730

This is at the top, before it begins calling validateProperty on each prop. It should be working right.