Permalink
Browse files

Convenience API for validations in property-defs not creating full va…

…lidations
  • Loading branch information...
1 parent f050abe commit 9e896659808aa3f295e371bd2dc57759f01c2bc7 @mde mde committed Jul 18, 2013
Showing with 50 additions and 23 deletions.
  1. +37 −10 lib/index.js
  2. +13 −13 lib/validators.js
View
@@ -944,7 +944,7 @@ utils.mixin(model, new (function () {
for (var p in validations) {
validator = model.validators[p]
- rule = utils.mixin({},validations[p],{scenario: opts.scenario});
+ rule = utils.mixin({}, validations[p], {scenario: opts.scenario});
if (typeof validator != 'function') {
throw new Error(p + ' is not a valid validator');
@@ -1073,10 +1073,8 @@ model.ModelDefinitionBase = function (name) {
// Default to 'create' and 'update' only for scenarios
opts.on = opts.on || ['create', 'update'];
- reg[this.name].properties[name].validations[condition] = {
- reference: reference
- , opts: opts
- };
+ reg[this.name].properties[name].validations[condition] =
+ new model.ValidationDescription(condition, reference, opts);
};
// For each of the validators, create a validatesFooBar from
@@ -1165,25 +1163,54 @@ model.ModelDescription = function (name) {
};
model.PropertyDescription = function (name, datatype, o) {
- var opts = o || {};
+ var opts = o || {}
+ , validations = {}
+ , validationOpts = utils.mixin({}, opts);
+
+ delete validationOpts.required;
+ delete validationOpts.length;
+ delete validationOpts.format;
+
this.name = name;
this.datatype = datatype;
this.options = opts;
- var validations = {};
+
+ // Creates results similar to `this.validates`, above in ModelDefinitionBase
+ // Would be great to remove the duplication of logic
for (var p in opts) {
if (opts.required || opts.length) {
- validations.present = true;
+ validations.present =
+ new model.ValidationDescription('present', null, validationOpts);
}
if (opts.length) {
- validations.length = opts.length;
+ if (typeof opts.length == 'object') {
+ // {min: 1, max: 2} or {is: 3}
+ validations.length =
+ new model.ValidationDescription('length', null,
+ utils.mixin(opts.length, validationOpts));
+ }
+ // 1 or '1'
+ else {
+ validations.length =
+ new model.ValidationDescription('length', null,
+ utils.mixin({is: opts.length}, validationOpts));
+ }
}
if (opts.format) {
- validations.format = opts.format;
+ validations.format =
+ new model.ValidationDescription('length', opts.format,
+ validationOpts);
}
}
this.validations = validations;
};
+model.ValidationDescription = function (type, reference, opts) {
+ this.type = type;
+ this.reference = reference;
+ this.opts = opts;
+};
+
module.exports = model;
// Load last, these depend on index.js
View
@@ -38,7 +38,7 @@ baseValidators = {
var msg;
if (utils.isEmpty(val)) {
//'Field "' + name + '" is required.';
- msg = rule.message || i18n.getText('model.validatesPresent',
+ msg = rule.opts.message || i18n.getText('model.validatesPresent',
{name: name}, locale);
}
return msg;
@@ -47,8 +47,8 @@ baseValidators = {
absent: function (name, val, params, rule, locale) {
var msg;
if (val) {
- //return rule.message || 'Field "' + name + '" must not be filled in.';
- msg = rule.message || i18n.getText('model.validatesAbsent',
+ //return rule.opts.message || 'Field "' + name + '" must not be filled in.';
+ msg = rule.opts.message || i18n.getText('model.validatesAbsent',
{name: name}, locale);
}
return msg;
@@ -58,9 +58,9 @@ baseValidators = {
var qual = rule.reference
, msg;
if (val != params[qual]) {
- //return rule.message || 'Field "' + name + '" and field "' + qual +
+ //return rule.opts.message || 'Field "' + name + '" and field "' + qual +
// '" must match.';
- msg = rule.message || i18n.getText('model.validatesConfirmed',
+ msg = rule.opts.message || i18n.getText('model.validatesConfirmed',
{name: name, qual: qual}, locale);
}
return msg;
@@ -69,8 +69,8 @@ baseValidators = {
format: function (name, val, params, rule, locale) {
var msg;
if (!rule.reference.test(val)) {
- //return rule.message || 'Field "' + name + '" is not correctly formatted.';
- msg = rule.message || i18n.getText('model.validatesFormat',
+ //return rule.opts.message || 'Field "' + name + '" is not correctly formatted.';
+ msg = rule.opts.message || i18n.getText('model.validatesFormat',
{name: name}, locale);
}
return msg;
@@ -88,7 +88,7 @@ baseValidators = {
// If a specific length is wanted, there has to be a value
// in the first place
if (!val) {
- return rule.message || i18n.getText('model.validatesPresent', {name: name}, locale);
+ return rule.opts.message || i18n.getText('model.validatesPresent', {name: name}, locale);
}
// Validate that there's a opts to check against
@@ -106,7 +106,7 @@ baseValidators = {
if (!isNaN(numVal)) {
validQualifier = true;
if (val.length !== numVal) {
- msg = rule.message || i18n.getText('model.validatesExactLength',
+ msg = rule.opts.message || i18n.getText('model.validatesExactLength',
{name: name, is: qual.is}, locale);
}
}
@@ -115,7 +115,7 @@ baseValidators = {
if (!isNaN(numVal)) {
validQualifier = true;
if (val.length < numVal) {
- msg = rule.message || i18n.getText('model.validatesMinLength',
+ msg = rule.opts.message || i18n.getText('model.validatesMinLength',
{name: name, min: qual.min}, locale);
}
}
@@ -125,7 +125,7 @@ baseValidators = {
if (!isNaN(numVal)) {
validQualifier = true;
if (val.length > numVal) {
- msg = rule.message || i18n.getText('model.validatesMaxLength',
+ msg = rule.opts.message || i18n.getText('model.validatesMaxLength',
{name: name, max: qual.max}, locale);
}
}
@@ -147,8 +147,8 @@ baseValidators = {
'" must be a function.');
}
if (!func(val, params)) {
- //return rule.message || 'Field "' + name + '" is not valid.';
- msg = rule.message || i18n.getText('model.validatesWithFunction',
+ //return rule.opts.message || 'Field "' + name + '" is not valid.';
+ msg = rule.opts.message || i18n.getText('model.validatesWithFunction',
{name: name}, locale);
}
return msg;

0 comments on commit 9e89665

Please sign in to comment.