Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a better error message when a validation name is invalid.
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. ```
- Loading branch information
John Postlethwait
committed
Feb 14, 2014
1 parent
75cffbc
commit 4c64337
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var assert = require('assert') | ||
, model = require('../../lib') | ||
, tests; | ||
|
||
|
||
function assertValidationErrorIsThrown (InvalidModel) { | ||
assert.throws( | ||
function () { | ||
model.register('InvalidModel', InvalidModel); | ||
}, | ||
/Validation\scannot\sbe\sadded/ | ||
); | ||
} | ||
|
||
|
||
tests = { | ||
"test instantiating a model with validatesPresent on a property that doesn't exist": function () { | ||
var InvalidModel = function () { | ||
this.validatesPresent('monkies'); | ||
}; | ||
|
||
assertValidationErrorIsThrown(InvalidModel); | ||
} | ||
|
||
, "test instantiating a model with validatesConfirmed on a property that doesn't exist": function () { | ||
var InvalidModel = function () { | ||
this.validatesConfirmed('monkies', 'realMonkies'); | ||
}; | ||
|
||
assertValidationErrorIsThrown(InvalidModel); | ||
} | ||
|
||
, "test instantiating a model with validatesAbsent on a property that doesn't exist": function () { | ||
var InvalidModel = function () { | ||
this.validatesAbsent('bananas'); | ||
}; | ||
|
||
assertValidationErrorIsThrown(InvalidModel); | ||
} | ||
|
||
, "test instantiating a model with validatesLength on a property that doesn't exist": function () { | ||
var InvalidModel = function () { | ||
this.validatesLength('gorillas', {is: 12}); | ||
}; | ||
|
||
assertValidationErrorIsThrown(InvalidModel); | ||
} | ||
|
||
, "test instantiating a model with validatesFormat on a property that doesn't exist": function () { | ||
var InvalidModel = function () { | ||
this.validatesFormat('chimpanzees', /hairy/); | ||
}; | ||
|
||
assertValidationErrorIsThrown(InvalidModel); | ||
} | ||
|
||
, "test instantiating a model with validatesWithFunction on a property that doesn't exist": function () { | ||
var InvalidModel = function () { | ||
this.validatesWithFunction('ocelot', function () {}); | ||
}; | ||
|
||
assertValidationErrorIsThrown(InvalidModel); | ||
} | ||
}; | ||
|
||
module.exports = tests; |