geddy / model

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

Adding a better error message when a validation name is invalid. #171

Closed JohnPostlethwait closed 10 years ago

JohnPostlethwait commented 10 years ago

When you tried to add a validator on a property that was non-existent, like this:

var MyModel = function () {
  this.validatesPresent('bad_prop');
};

Gedddy Model would throw this error:

/usr/local/lib/node_modules/geddy/node_modules/model/lib/index.js:955
    reg[this.name].properties[name].validations[condition] =
                                   ^
TypeError: Cannot read property 'validations' of undefined

Which is not terribly helpful. When you add a validation for a non-existent property now, it will throw this error:

Error: Validation cannot be added for "bad_prop": property does
not exist on the MyModel model.
mde commented 10 years ago

There are unit tests that make sure you can create a valid model: https://github.com/geddy/model/blob/master/test/unit/create_user.js

The fixture for that one is here: https://github.com/geddy/model/blob/master/test/fixtures/user.js

You could add a separate test file that runs a test where you require the invalid model and ensure it throws.

run the tests by doing jake test and run just the unit tests by doing jake test[unit].

JohnPostlethwait commented 10 years ago

Cool, I'll add tests, thanks for pointing me in the right direction!

JohnPostlethwait commented 10 years ago

Added tests!

mde commented 10 years ago

Awesome, thanks!