Expose validation metadata
The new Validatable.validations mixin method returns the validations as an object indexed by property name.
This commit is contained in:
parent
d419ae80d0
commit
bed97a75cb
|
@ -1,4 +1,6 @@
|
|||
var util = require('util');
|
||||
var extend = util._extend;
|
||||
|
||||
/*!
|
||||
* Module exports
|
||||
*/
|
||||
|
@ -23,6 +25,18 @@ exports.Validatable = Validatable;
|
|||
function Validatable() {
|
||||
}
|
||||
|
||||
Validatable.validations = function() {
|
||||
var validations = {};
|
||||
(this._validations || []).forEach(function(v) {
|
||||
var key = v[0], validation = v[1], options = v[3];
|
||||
var copy = extend({}, validation);
|
||||
copy.options = options || {};
|
||||
validations[key] = validations[key] || [];
|
||||
validations[key].push(copy);
|
||||
});
|
||||
return validations;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate presence of one or more specified properties.
|
||||
* Requires a model to include a property to be considered valid; fails when validated field is blank.
|
||||
|
|
|
@ -130,6 +130,14 @@ describe('validations', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return validation metadata', function() {
|
||||
var expected = {name:[{validation: 'presence', options: {}}]};
|
||||
delete User._validations;
|
||||
User.validatesPresenceOf('name');
|
||||
var validations = User.validations();
|
||||
validations.should.eql(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue