Expose validation metadata

The new Validatable.validations mixin method returns the validations as
an object
indexed by property name.
This commit is contained in:
Fabien Franzen 2014-08-19 15:41:55 +02:00
parent d419ae80d0
commit bed97a75cb
2 changed files with 22 additions and 0 deletions

View File

@ -1,4 +1,6 @@
var util = require('util'); var util = require('util');
var extend = util._extend;
/*! /*!
* Module exports * Module exports
*/ */
@ -23,6 +25,18 @@ exports.Validatable = Validatable;
function 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. * 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. * Requires a model to include a property to be considered valid; fails when validated field is blank.

View File

@ -130,6 +130,14 @@ describe('validations', function () {
done(); 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);
});
}); });
}); });