dependencies and bug fixes
This commit is contained in:
parent
998ebc9dca
commit
e98e9689fb
31
lib/rules.js
31
lib/rules.js
|
@ -6,7 +6,7 @@ var Rules = function(param, rules)
|
|||
this.rules = rules;
|
||||
};
|
||||
|
||||
Rules.prototype.Error = function(message, rule)
|
||||
Rules.prototype.Error = function(message, rule, value)
|
||||
{
|
||||
switch(typeof(this.rules.error))
|
||||
{
|
||||
|
@ -28,23 +28,32 @@ Rules.prototype.Error = function(message, rule)
|
|||
break;
|
||||
}
|
||||
|
||||
return message.replace(/%p/, this.param).replace(/%v/, value.toString()).replace(/%r/, rule);
|
||||
if(_.isString(value))
|
||||
message = message.replace(/%v/, value.toString());
|
||||
|
||||
if(_.isString(rule))
|
||||
message = message.replace(/%r/, rule);
|
||||
|
||||
return message.replace(/%p/, this.param).replace(/%v/, "value").replace(/%r/, "rule");
|
||||
};
|
||||
|
||||
Rules.prototype.apply = function(value)
|
||||
{
|
||||
if(_.isUndefined(value) && !_is.Undefined(this.rules['default']))
|
||||
if(_.isUndefined(value) && !_.isUndefined(this.rules['default']))
|
||||
{
|
||||
value = this.rules['default'];
|
||||
}
|
||||
|
||||
if(this.rules.required)
|
||||
{
|
||||
if(this.rules.required && (typeof(value) === "undefined" || value === null))
|
||||
if(this.rules.required && _.isUndefined(value))
|
||||
{
|
||||
throw this.Error("%p is a required parameter", "required", value);
|
||||
}
|
||||
}
|
||||
// if value is nore required and is undefined, no more rules need to be run
|
||||
else if(_.isUndefined(value))
|
||||
return value;
|
||||
|
||||
if(this.rules.filters)
|
||||
{
|
||||
|
@ -120,7 +129,7 @@ Rules.prototype.check = function(value)
|
|||
{
|
||||
var args = this.rules.properties[property];
|
||||
|
||||
if(!checks[property].apply(null, Array.isArray(args) ? [value].concat(args) : [value, args]))
|
||||
if(!checks[property].apply(null, [value, args]))
|
||||
throw this.Error("%p failed %r with %v", property);
|
||||
}
|
||||
else if(typeof(this.rules.properties[property]) === "function")
|
||||
|
@ -153,7 +162,7 @@ var checks =
|
|||
|
||||
'in': function(value, list)
|
||||
{
|
||||
return list.indexOf(value) == -1;
|
||||
return list.indexOf(value) != -1;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -164,6 +173,16 @@ var is =
|
|||
return typeof(value) == 'string';
|
||||
},
|
||||
|
||||
'alphanum': function(value)
|
||||
{
|
||||
return /^[a-z0-9]+$/i.test(value);
|
||||
},
|
||||
|
||||
'alpha': function(value)
|
||||
{
|
||||
return /^[a-z]+$/i.test(value);
|
||||
},
|
||||
|
||||
'object': function(value)
|
||||
{
|
||||
return typeof(value) == 'object';
|
||||
|
|
|
@ -6,12 +6,12 @@ var Schema = function(schema)
|
|||
this.schema = schema;
|
||||
};
|
||||
|
||||
Schema.prototype.validate = function(_form)
|
||||
Schema.prototype.validate = function(data)
|
||||
{
|
||||
var form = typeof(_form) == "object" ? _form : {};
|
||||
var params = Object.keys(this.schema);
|
||||
var errors = {};
|
||||
var values = {};
|
||||
var params = Object.keys(this.schema);
|
||||
var errors = {};
|
||||
var values = {};
|
||||
var get = typeof(data) == "function" ? data : function(p) { return data[p]; };
|
||||
|
||||
for(var i = 0; i < params.length; i++)
|
||||
{
|
||||
|
@ -19,10 +19,10 @@ Schema.prototype.validate = function(_form)
|
|||
|
||||
try
|
||||
{
|
||||
values[params[i]] = (rules.create(params[i], schema)).apply(form[params[i]]);
|
||||
values[params[i]] = (rules.create(params[i], schema)).apply(get(params[i]));
|
||||
|
||||
// does this rule contain even more rules?
|
||||
if(typeof(schema.schema) == "object" && !_.isArray(schema.schema) && Object.keys(schema.schema).length)
|
||||
// does this rule contain embedded schemas
|
||||
if(typeof(schema.schema) == "object" && !_.isArray(schema.schema) && Object.keys(schema.schema).length && !_.isUndefined(values[params[i]]))
|
||||
{
|
||||
if(schema.type == "object")
|
||||
{
|
||||
|
@ -30,7 +30,7 @@ Schema.prototype.validate = function(_form)
|
|||
{
|
||||
try
|
||||
{
|
||||
values[params[i]][param] = rules.create(params[i] + "." + param, schema.schema).apply(form[params[i]][param]);
|
||||
values[params[i]][param] = rules.create(params[i] + "." + param, schema.schema).apply(get(params[i])[param]);
|
||||
}
|
||||
catch(error)
|
||||
{
|
||||
|
@ -38,7 +38,7 @@ Schema.prototype.validate = function(_form)
|
|||
}
|
||||
});
|
||||
}
|
||||
else if(schema.type == "array" && Array.isArray(values[params[i]]))
|
||||
else if(schema.type == "array")
|
||||
{
|
||||
values[params[i]].forEach(function(value, index)
|
||||
{
|
||||
|
@ -63,7 +63,17 @@ Schema.prototype.validate = function(_form)
|
|||
return {data:values, errors:errors, valid:Object.keys(errors).length == 0};
|
||||
};
|
||||
|
||||
exports.types = rules.types;
|
||||
exports.properties = rules.properties;
|
||||
exports.filters = rules.filters;
|
||||
|
||||
exports.create = function(schema) { return new Schema(schema); }
|
||||
exports.types = rules.types;
|
||||
exports.properties = rules.properties;
|
||||
exports.filters = rules.filters;
|
||||
|
||||
exports.middleware = function(schema)
|
||||
{
|
||||
return function(req, res, next)
|
||||
{
|
||||
req.form = new Schema(schema).validate(req.route.method == "post" ? req.body : req.query);
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "schemajs",
|
||||
"description": "extensible object validator",
|
||||
"version": "0.1",
|
||||
"version": "0.1.1",
|
||||
"author": "eleith",
|
||||
"contributors": [],
|
||||
"repository":
|
||||
|
@ -9,7 +9,10 @@
|
|||
"type": "git",
|
||||
"url": "http://github.com/eleith/schemajs.git"
|
||||
},
|
||||
"dependencies": {},
|
||||
"engine": ["node >= 0.3.8"],
|
||||
"dependencies":
|
||||
{
|
||||
"underscore": ">= 1.1.7"
|
||||
},
|
||||
"engine": ["node >= 0.4.8"],
|
||||
"main": "schema"
|
||||
}
|
||||
|
|
Reference in New Issue