Comparing version 0.2.0 to 0.2.2
@@ -37,2 +37,3 @@ /*! | ||
naturalNonZero: /^[1-9][0-9]*$/i, | ||
luhn: /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/, | ||
url: /^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/ | ||
@@ -47,2 +48,6 @@ }, | ||
contains: function (obj, prop, value) { | ||
if (obj[prop].indexOf(value) < 0) throw new ValidatorMessage('contains', {property: prop, value: value}); | ||
}, | ||
date: function (obj, prop) { | ||
@@ -64,2 +69,18 @@ if (!(obj[prop] instanceof Date)) throw new ValidatorMessage('date', {property: prop}); | ||
in: function (obj, prop, array) { | ||
var isIn = false; | ||
for (var key in array) { | ||
if (array[key] === obj[prop]) isIn = true; | ||
} | ||
if (!isIn) throw new ValidatorMessage('in', {property: prop, arrayString: array.toString()}); | ||
}, | ||
json: function (obj, prop) { | ||
try { | ||
JSON.parse(obj[prop]); | ||
} catch (e) { | ||
throw new ValidatorMessage('json', {property: prop}); | ||
} | ||
}, | ||
length: function (obj, prop, options) { | ||
@@ -88,2 +109,10 @@ var v = (obj[prop] || '').length, arr = [], errs = [], n; | ||
lowercase: function (obj, prop) { | ||
if (obj[prop].toLowerCase() !== obj[prop]) throw new ValidatorMessage('lowercase', { property: prop }); | ||
}, | ||
matches: function (obj, prop, regexp) { | ||
if (!regexp.test(obj[prop])) throw new ValidatorMessage('matches', { property: prop, regexp: regexp }); | ||
}, | ||
NaN: function (obj, prop) { | ||
@@ -139,2 +168,6 @@ if (!isNaN(obj[prop])) throw new ValidatorMessage('NaN', {property: prop}); | ||
if (!('string' === typeof obj[prop])) throw new ValidatorMessage('string', {property: prop}); | ||
}, | ||
uppercase: function (obj, prop) { | ||
if (obj[prop].toUpperCase() !== obj[prop]) throw new ValidatorMessage('uppercase', { property: prop }); | ||
} | ||
@@ -176,2 +209,5 @@ | ||
}, | ||
contains: { | ||
en: '{{property}} must contain \'{{value}}\'' | ||
}, | ||
date: { | ||
@@ -198,5 +234,11 @@ en: '{{property}} must be a date' | ||
}, | ||
in: { | ||
en: '{{property}} must be in {{arrayString}}' | ||
}, | ||
integer: { | ||
en: '{{property}} must be a valid integer' | ||
}, | ||
json: { | ||
en: '{{property}} must be valid JSON' | ||
}, | ||
lengthEqualTo: { | ||
@@ -223,2 +265,11 @@ en: '{{property}} must have {{num}} character(s)' | ||
}, | ||
lowercase: { | ||
en: '{{property}} must be lowercase' | ||
}, | ||
luhn: { | ||
en: '{{property}} must be a valid credit card number' | ||
}, | ||
matches: { | ||
en: '{{property}} must match the following regular expression: {{regexp}}' | ||
}, | ||
NaN: { | ||
@@ -248,2 +299,5 @@ en: '{{property}} must not be a number' | ||
}, | ||
uppercase: { | ||
en: '{{property}} must be uppercase' | ||
}, | ||
url: { | ||
@@ -250,0 +304,0 @@ en: '{{property}} must be a valid URL' |
@@ -22,2 +22,2 @@ /*! | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/(function(e){function t(e){function i(e,t){this.name=e,this.options=t}ValidatorTypes={alpha:/^[a-z]+$/i,alphaNumeric:/^[a-z0-9]+$/i,alphaNumericDash:/^[a-z0-9_\-]+$/i,alphaNumericUnderscore:/^[a-z0-9_]+$/i,email:/^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,6}$/i,integer:/^\-?[0-9]+$/,natural:/^[0-9]+$/i,naturalNonZero:/^[1-9][0-9]*$/i,url:/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/},Validators={array:function(e,t){if(!(e[t]instanceof Array))throw new i("array",{property:t})},date:function(e,t){if(!(e[t]instanceof Date))throw new i("date",{property:t})},exists:function(e,t){if("undefined"==typeof e[t])throw new i("exists",{property:t})},"boolean":function(e,t){if("boolean"!=typeof e[t])throw new i("boolean",{property:t})},fn:function(e,t){if("function"!=typeof e[t])throw new i("fn",{property:t})},length:function(e,t,n){var r=(e[t]||"").length,s=[],o=[],u;n.greaterThan&&s.push([n.greaterThan,r>n.greaterThan,"lengthGreaterThan"]),n.lessThan&&s.push([n.lessThan,r<n.lessThan,"lengthLessThan"]),n.greaterThanOrEqualTo&&s.push([n.greaterThanOrEqualTo,r>=n.greaterThanOrEqualTo,"lengthGreaterThanOrEqualTo"]),n.lessThanOrEqualTo&&s.push([n.lessThanOrEqualTo,r<=n.lessThanOrEqualTo,"lengthLessThanOrEqualTo"]),n.equalTo&&s.push([n.equalTo,r==n.equalTo,"lengthEqualTo"]);for(var a in s)s[a][1]||o.push(new i(s[a][2],{property:t,num:s[a][0]}));if(o.length)throw o},NaN:function(e,t){if(!isNaN(e[t]))throw new i("NaN",{property:t})},number:function(e,t){if("number"!=typeof e[t])throw new i("number",{property:t})},object:function(e,t){if("object"!=typeof e[t])throw new i("object",{property:t})},operator:function(e,t,n){var r=e[t],s=[],o=[],u,a;for(var f in n)u="string"==typeof n[f],a=u?e[n[f]]:n[f],"greaterThan"===f?s.push([a,r>a,"greaterThan",u,n[f]]):"lessThan"===f?s.push([a,r<a,"lessThan",u,n[f]]):"greaterThanOrEqualTo"===f?s.push([a,r>=a,"greaterThanOrEqualTo",u,n[f]]):"lessThanOrEqualTo"===f?s.push([a,r<=a,"lessThanOrEqualTo",u,n[f]]):"equalTo"===f&&s.push([a,r==a,"equalTo",u,n[f]]);for(var f in s)s[f][1]||o.push(new i(s[f][2],{lh:t,rh:s[f][3]?s[f][4]:s[f][0]}));if(o.length)throw o},presence:function(e,t){if(e[t]==null||e[t]=="")throw new i("presence",{property:t})},regexp:function(e,t){if(!(e[t]instanceof RegExp))throw new i("regexp",{property:t})},string:function(e,t){if("string"!=typeof e[t])throw new i("string",{property:t})}},ValidationMatch=function(e,t,n){if(!ValidatorTypes[n].test(e[t]))throw new i(n,{property:t})};for(var t in ValidatorTypes)Validators[t]=function(e){return function(t,n){ValidationMatch(t,n,e)}}(t);var n={alpha:{en:"{{property}} must contain only letters"},alphaNumeric:{en:"{{property}} must contain only letters and numbers"},alphaNumericDash:{en:"{{property}} must contain only letters, numbers, and dashes"},alphaNumericUnderscore:{en:"{{property}} must contain only letters, numbers, and underscores"},array:{en:"{{property}} must be an array"},"boolean":{en:"{{property}} must be a boolean"},date:{en:"{{property}} must be a date"},equalTo:{en:"{{lh}} must be equal to {{rh}}"},email:{en:"{{property}} must be a valid e-mail address"},exists:{en:"{{property}} must exist"},fn:{en:"{{property}} must be a function"},greaterThan:{en:"{{lh}} must be greater than {{rh}}"},greaterThanOrEqualTo:{en:"{{lh}} must be greater than or equal to {{rh}}"},integer:{en:"{{property}} must be a valid integer"},lengthEqualTo:{en:"{{property}} must have {{num}} character(s)"},lengthGreaterThan:{en:"{{property}} must be greater than {{num}} character(s)"},lengthGreaterThanOrEqualTo:{en:"{{property}} must be greater than or equal to {{num}} character(s)"},lengthLessThan:{en:"{{property}} must be less than {{num}} character(s)"},lengthLessThanOrEqualTo:{en:"{{property}} must be less than or equal to {{num}} character(s)"},lessThan:{en:"{{lh}} must be less than {{rh}}"},lessThanOrEqualTo:{en:"{{lh}} must be less than or equal to {{rh}}"},NaN:{en:"{{property}} must not be a number"},natural:{en:"{{property}} must be a positive number"},naturalNonZero:{en:"{{property}} must be a positive number and not be zero"},number:{en:"{{property}} must be a number"},object:{en:"{{property}} must be an object"},presence:{en:"{{property}} is required"},regexp:{en:"{{property}} must be a regular expression"},string:{en:"{{property}} must be a string"},url:{en:"{{property}} must be a valid URL"}},r=function(e,t){"string"==typeof e&&(e=Validators[e]),this._validator=e,this._options=t,this._ifAll=[],this._unlessAll=[],"object"==typeof t&&(t.if&&(this.if(t.if),delete t.if),t.unless&&(this.unless(t.unless),delete t.unless),t.invalid&&(this.invalid(t.invalid),delete t.invalid))};r.prototype={"if":function(){if(arguments.length==2){var e=arguments[0],t=arguments[1],n;"string"==typeof e&&(e=[e]);for(var r in e)n=(this._props||{})[e[r]],n&&(n.if||(n.if=[]),n.if.push(t),this._props[e[r]]=n)}else this._ifAll.push(arguments[0]);return this},unless:function(){if(arguments.length==2){var e=arguments[0],t=arguments[1],n;"string"==typeof e&&(e=[e]);for(var r in e)n=(this._props||{})[e[r]],n&&(n.unless||(n.unless=[]),n.unless.push(t),this._props[e[r]]=n)}else this._unlessAll.push(arguments[0]);return this},invalid:function(){if(arguments.length==2){var e=arguments[0],t=arguments[1],n;"string"==typeof e&&(e=[e]);for(var r in e)n=(this._props||{})[e[r]],n&&(n.invalid=t,this._props[e[r]]=n)}else this._invalidAll=arguments[0];return this},prop:function(e,t){"string"==typeof e&&(e=[e]);var n,r;for(var i in e)n=e[i],this._props||(this._props={}),this._props[n]||(this._props[n]={}),"object"==typeof t&&(t.if&&(this.if(n,t.if),delete t.if),t.unless&&(this.unless(n,t.unless),delete t.unless),t.invalid&&(this.invalid(n,t.invalid),delete t.invalid));return this},run:function(e){var t=this,n=[],r=t._options||{},i,o;for(var a in this._ifAll){i=t._ifAll[a];if(!i(e))return t}for(var a in this._unlessAll){i=t._unlessAll[a];if(!!i(e))return t}if(t._props){var f=[],l;for(name in t._props)l=t._props[name],f.push(function(){if(l.if)for(var r in l.if){i=l.if[r];if(!i(e))return}if(l.unless)for(var r in l.unless){i=l.unless[r];if(!!i(e))return}return u.Promise.method(t._validator)(e,name,t._options).catch(function(t,r){return function(i){if(r.invalid)try{r.invalid(e)}catch(s){i=s}n.push([i,t])}}(name,l))}());o=u.Promise.all(f)}else o=u.Promise.method(t._validator)(e,t._options).catch(function(e){n.push([e])});return o.then(function(){var r=[],i;if(n.length){if(t._invalidAll)try{t._invalidAll(e)}catch(o){n=[o]}for(var u in n){i=n[u],i[0]instanceof Array||(i[0]=[i[0]]);for(var a in i[0])r=r.concat(new s(i[0][a],i[1]))}throw r}return t})}},i.prototype={toString:function(){var e=this,t=n[e.name][u.locale];return t.replace(/\{\{([a-zA-Z]+)\}\}/g,function(t,n){return e.options[n]})}};var s=function(e,t){this.error=e,this.prop=t};s.prototype={toString:function(){return this.error.toString()}};var o=function(e,t){this.errors=e,this.obj=t};o.prototype={toString:function(){var e=[],t;for(var n in this.errors)t=this.errors[n],e.push(t.toString());return e.join(", ")},toJSON:function(){var e={},t,n;for(var r in this.errors)t=this.errors[r],(n=t.prop)?(e[n]||(e[n]=[]),e[n].push(t.toString())):(e.base||(e.base=[]),e.base.push(t.toString()));return e}};var u=function(){this.validations=[],this.propInvalids={},arguments[0]&&this.rule.apply(this,arguments)};return u.prototype={rule:function(){var e,t,n,i;if(arguments.length>1)if("function"==typeof arguments[0])t=arguments[0],n=arguments[1];else{e=arguments[0];if("object"==typeof arguments[1]){for(var s in arguments[1])this.rule(e,s,arguments[1][s]);return this}t=arguments[1],n=arguments[2]}else{if("object"==typeof arguments[0]){for(var s in arguments[0])this.rule(s,arguments[0][s]);return this}t=arguments[0]}return i=new r(t,n),e&&i.prop(e),this.validations.push(i),this},"if":function(){var e;for(var t in this.validations)e=this.validations[t],e.if.apply(e,arguments);return this},unless:function(){var e;for(var t in this.validations)e=this.validations[t],e.unless.apply(e,arguments);return this},invalid:function(){var e,t,n;if(arguments.length==2){t=arguments[0],e=arguments[1],"string"==typeof t&&(t=[t]);for(var r in t)n=t[r],this.propInvalids[n]=e}else e=arguments[0],this._invalid=e;return this},run:function(e){var t=[],n=this;return u.Promise.map(n.validations,function(n){return n.run(e).catch(function(e){e instanceof Array?t=t.concat(e):t.push(e)})}).then(function(){if(t.length){if(n._invalid)try{n._invalid(i)}catch(r){t=[r]}else for(var i in n.propInvalids){for(var s in t)error=t[s],i==error.prop&&t.splice(s,1);try{n.propInvalids[i](e)}catch(r){t.push(r)}}throw new o(t,e)}return n})}},u.locale="en",u.Promise=e,u}"function"==typeof define&&define.amd?define(["bluebird"],t):"object"==typeof exports?module.exports=t(require("bluebird")):e.Credible=t(e.Promise)})(this); | ||
*/(function(e){function t(e){function i(e,t){this.name=e,this.options=t}ValidatorTypes={alpha:/^[a-z]+$/i,alphaNumeric:/^[a-z0-9]+$/i,alphaNumericDash:/^[a-z0-9_\-]+$/i,alphaNumericUnderscore:/^[a-z0-9_]+$/i,email:/^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,6}$/i,integer:/^\-?[0-9]+$/,natural:/^[0-9]+$/i,naturalNonZero:/^[1-9][0-9]*$/i,luhn:/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/,url:/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/},Validators={array:function(e,t){if(!(e[t]instanceof Array))throw new i("array",{property:t})},contains:function(e,t,n){if(e[t].indexOf(n)<0)throw new i("contains",{property:t,value:n})},date:function(e,t){if(!(e[t]instanceof Date))throw new i("date",{property:t})},exists:function(e,t){if("undefined"==typeof e[t])throw new i("exists",{property:t})},"boolean":function(e,t){if("boolean"!=typeof e[t])throw new i("boolean",{property:t})},fn:function(e,t){if("function"!=typeof e[t])throw new i("fn",{property:t})},"in":function(e,t,n){var r=!1;for(var s in n)n[s]===e[t]&&(r=!0);if(!r)throw new i("in",{property:t,arrayString:n.toString()})},json:function(e,t){try{JSON.parse(e[t])}catch(n){throw new i("json",{property:t})}},length:function(e,t,n){var r=(e[t]||"").length,s=[],o=[],u;n.greaterThan&&s.push([n.greaterThan,r>n.greaterThan,"lengthGreaterThan"]),n.lessThan&&s.push([n.lessThan,r<n.lessThan,"lengthLessThan"]),n.greaterThanOrEqualTo&&s.push([n.greaterThanOrEqualTo,r>=n.greaterThanOrEqualTo,"lengthGreaterThanOrEqualTo"]),n.lessThanOrEqualTo&&s.push([n.lessThanOrEqualTo,r<=n.lessThanOrEqualTo,"lengthLessThanOrEqualTo"]),n.equalTo&&s.push([n.equalTo,r==n.equalTo,"lengthEqualTo"]);for(var a in s)s[a][1]||o.push(new i(s[a][2],{property:t,num:s[a][0]}));if(o.length)throw o},lowercase:function(e,t){if(e[t].toLowerCase()!==e[t])throw new i("lowercase",{property:t})},matches:function(e,t,n){if(!n.test(e[t]))throw new i("matches",{property:t,regexp:n})},NaN:function(e,t){if(!isNaN(e[t]))throw new i("NaN",{property:t})},number:function(e,t){if("number"!=typeof e[t])throw new i("number",{property:t})},object:function(e,t){if("object"!=typeof e[t])throw new i("object",{property:t})},operator:function(e,t,n){var r=e[t],s=[],o=[],u,a;for(var f in n)u="string"==typeof n[f],a=u?e[n[f]]:n[f],"greaterThan"===f?s.push([a,r>a,"greaterThan",u,n[f]]):"lessThan"===f?s.push([a,r<a,"lessThan",u,n[f]]):"greaterThanOrEqualTo"===f?s.push([a,r>=a,"greaterThanOrEqualTo",u,n[f]]):"lessThanOrEqualTo"===f?s.push([a,r<=a,"lessThanOrEqualTo",u,n[f]]):"equalTo"===f&&s.push([a,r==a,"equalTo",u,n[f]]);for(var f in s)s[f][1]||o.push(new i(s[f][2],{lh:t,rh:s[f][3]?s[f][4]:s[f][0]}));if(o.length)throw o},presence:function(e,t){if(e[t]==null||e[t]=="")throw new i("presence",{property:t})},regexp:function(e,t){if(!(e[t]instanceof RegExp))throw new i("regexp",{property:t})},string:function(e,t){if("string"!=typeof e[t])throw new i("string",{property:t})},uppercase:function(e,t){if(e[t].toUpperCase()!==e[t])throw new i("uppercase",{property:t})}},ValidationMatch=function(e,t,n){if(!ValidatorTypes[n].test(e[t]))throw new i(n,{property:t})};for(var t in ValidatorTypes)Validators[t]=function(e){return function(t,n){ValidationMatch(t,n,e)}}(t);var n={alpha:{en:"{{property}} must contain only letters"},alphaNumeric:{en:"{{property}} must contain only letters and numbers"},alphaNumericDash:{en:"{{property}} must contain only letters, numbers, and dashes"},alphaNumericUnderscore:{en:"{{property}} must contain only letters, numbers, and underscores"},array:{en:"{{property}} must be an array"},"boolean":{en:"{{property}} must be a boolean"},contains:{en:"{{property}} must contain '{{value}}'"},date:{en:"{{property}} must be a date"},equalTo:{en:"{{lh}} must be equal to {{rh}}"},email:{en:"{{property}} must be a valid e-mail address"},exists:{en:"{{property}} must exist"},fn:{en:"{{property}} must be a function"},greaterThan:{en:"{{lh}} must be greater than {{rh}}"},greaterThanOrEqualTo:{en:"{{lh}} must be greater than or equal to {{rh}}"},"in":{en:"{{property}} must be in {{arrayString}}"},integer:{en:"{{property}} must be a valid integer"},json:{en:"{{property}} must be valid JSON"},lengthEqualTo:{en:"{{property}} must have {{num}} character(s)"},lengthGreaterThan:{en:"{{property}} must be greater than {{num}} character(s)"},lengthGreaterThanOrEqualTo:{en:"{{property}} must be greater than or equal to {{num}} character(s)"},lengthLessThan:{en:"{{property}} must be less than {{num}} character(s)"},lengthLessThanOrEqualTo:{en:"{{property}} must be less than or equal to {{num}} character(s)"},lessThan:{en:"{{lh}} must be less than {{rh}}"},lessThanOrEqualTo:{en:"{{lh}} must be less than or equal to {{rh}}"},lowercase:{en:"{{property}} must be lowercase"},luhn:{en:"{{property}} must be a valid credit card number"},matches:{en:"{{property}} must match the following regular expression: {{regexp}}"},NaN:{en:"{{property}} must not be a number"},natural:{en:"{{property}} must be a positive number"},naturalNonZero:{en:"{{property}} must be a positive number and not be zero"},number:{en:"{{property}} must be a number"},object:{en:"{{property}} must be an object"},presence:{en:"{{property}} is required"},regexp:{en:"{{property}} must be a regular expression"},string:{en:"{{property}} must be a string"},uppercase:{en:"{{property}} must be uppercase"},url:{en:"{{property}} must be a valid URL"}},r=function(e,t){"string"==typeof e&&(e=Validators[e]),this._validator=e,this._options=t,this._ifAll=[],this._unlessAll=[],"object"==typeof t&&(t.if&&(this.if(t.if),delete t.if),t.unless&&(this.unless(t.unless),delete t.unless),t.invalid&&(this.invalid(t.invalid),delete t.invalid))};r.prototype={"if":function(){if(arguments.length==2){var e=arguments[0],t=arguments[1],n;"string"==typeof e&&(e=[e]);for(var r in e)n=(this._props||{})[e[r]],n&&(n.if||(n.if=[]),n.if.push(t),this._props[e[r]]=n)}else this._ifAll.push(arguments[0]);return this},unless:function(){if(arguments.length==2){var e=arguments[0],t=arguments[1],n;"string"==typeof e&&(e=[e]);for(var r in e)n=(this._props||{})[e[r]],n&&(n.unless||(n.unless=[]),n.unless.push(t),this._props[e[r]]=n)}else this._unlessAll.push(arguments[0]);return this},invalid:function(){if(arguments.length==2){var e=arguments[0],t=arguments[1],n;"string"==typeof e&&(e=[e]);for(var r in e)n=(this._props||{})[e[r]],n&&(n.invalid=t,this._props[e[r]]=n)}else this._invalidAll=arguments[0];return this},prop:function(e,t){"string"==typeof e&&(e=[e]);var n,r;for(var i in e)n=e[i],this._props||(this._props={}),this._props[n]||(this._props[n]={}),"object"==typeof t&&(t.if&&(this.if(n,t.if),delete t.if),t.unless&&(this.unless(n,t.unless),delete t.unless),t.invalid&&(this.invalid(n,t.invalid),delete t.invalid));return this},run:function(e){var t=this,n=[],r=t._options||{},i,o;for(var a in this._ifAll){i=t._ifAll[a];if(!i(e))return t}for(var a in this._unlessAll){i=t._unlessAll[a];if(!!i(e))return t}if(t._props){var f=[],l;for(name in t._props)l=t._props[name],f.push(function(){if(l.if)for(var r in l.if){i=l.if[r];if(!i(e))return}if(l.unless)for(var r in l.unless){i=l.unless[r];if(!!i(e))return}return u.Promise.method(t._validator)(e,name,t._options).catch(function(t,r){return function(i){if(r.invalid)try{r.invalid(e)}catch(s){i=s}n.push([i,t])}}(name,l))}());o=u.Promise.all(f)}else o=u.Promise.method(t._validator)(e,t._options).catch(function(e){n.push([e])});return o.then(function(){var r=[],i;if(n.length){if(t._invalidAll)try{t._invalidAll(e)}catch(o){n=[o]}for(var u in n){i=n[u],i[0]instanceof Array||(i[0]=[i[0]]);for(var a in i[0])r=r.concat(new s(i[0][a],i[1]))}throw r}return t})}},i.prototype={toString:function(){var e=this,t=n[e.name][u.locale];return t.replace(/\{\{([a-zA-Z]+)\}\}/g,function(t,n){return e.options[n]})}};var s=function(e,t){this.error=e,this.prop=t};s.prototype={toString:function(){return this.error.toString()}};var o=function(e,t){this.errors=e,this.obj=t};o.prototype={toString:function(){var e=[],t;for(var n in this.errors)t=this.errors[n],e.push(t.toString());return e.join(", ")},toJSON:function(){var e={},t,n;for(var r in this.errors)t=this.errors[r],(n=t.prop)?(e[n]||(e[n]=[]),e[n].push(t.toString())):(e.base||(e.base=[]),e.base.push(t.toString()));return e}};var u=function(){this.validations=[],this.propInvalids={},arguments[0]&&this.rule.apply(this,arguments)};return u.prototype={rule:function(){var e,t,n,i;if(arguments.length>1)if("function"==typeof arguments[0])t=arguments[0],n=arguments[1];else{e=arguments[0];if("object"==typeof arguments[1]){for(var s in arguments[1])this.rule(e,s,arguments[1][s]);return this}t=arguments[1],n=arguments[2]}else{if("object"==typeof arguments[0]){for(var s in arguments[0])this.rule(s,arguments[0][s]);return this}t=arguments[0]}return i=new r(t,n),e&&i.prop(e),this.validations.push(i),this},"if":function(){var e;for(var t in this.validations)e=this.validations[t],e.if.apply(e,arguments);return this},unless:function(){var e;for(var t in this.validations)e=this.validations[t],e.unless.apply(e,arguments);return this},invalid:function(){var e,t,n;if(arguments.length==2){t=arguments[0],e=arguments[1],"string"==typeof t&&(t=[t]);for(var r in t)n=t[r],this.propInvalids[n]=e}else e=arguments[0],this._invalid=e;return this},run:function(e){var t=[],n=this;return u.Promise.map(n.validations,function(n){return n.run(e).catch(function(e){e instanceof Array?t=t.concat(e):t.push(e)})}).then(function(){if(t.length){if(n._invalid)try{n._invalid(i)}catch(r){t=[r]}else for(var i in n.propInvalids){for(var s in t)error=t[s],i==error.prop&&t.splice(s,1);try{n.propInvalids[i](e)}catch(r){t.push(r)}}throw new o(t,e)}return n})}},u.locale="en",u.Promise=e,u}"function"==typeof define&&define.amd?define(["bluebird"],t):"object"==typeof exports?module.exports=t(require("bluebird")):e.Credible=t(e.Promise)})(this); |
{ | ||
"name": "credible", | ||
"version": "0.2.0", | ||
"version": "0.2.2", | ||
"author": "Noah Portes Chaikin <noah.porteschaikin@carrotcreative.com>", | ||
@@ -5,0 +5,0 @@ "description": "Unopinionated validation framework for node and the browser.", |
@@ -204,4 +204,8 @@ credible | ||
Property must be a boolean (`true` or `false` onlyq). | ||
Property must be a boolean (`true` or `false` only). | ||
#### `contains` | ||
Property must contain the string specified in the second argument. | ||
#### `date` | ||
@@ -223,2 +227,11 @@ | ||
#### `in` | ||
Property must be in specified array. | ||
```javascript | ||
credible | ||
.rule( { state: { in: ['approved', 'pending'] } } ); | ||
``` | ||
#### `integer` | ||
@@ -228,2 +241,6 @@ | ||
#### `json` | ||
Property must be a valid JSON string. | ||
#### `length` | ||
@@ -243,2 +260,14 @@ | ||
#### `lowercase` | ||
Property must contain all lowercase letters. | ||
#### `luhn` | ||
Property must be a valid credit card number. | ||
#### `matches` | ||
Property must match `RegExp` object specified in second argument. | ||
#### `natural` | ||
@@ -290,2 +319,6 @@ | ||
#### `uppercase` | ||
Property must contain all uppercase letters. | ||
#### `url` | ||
@@ -292,0 +325,0 @@ |
140
test/test.js
@@ -722,2 +722,142 @@ var Credible = require('../') | ||
describe('in', function () { | ||
it('should test that a property is in specified array', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('state', 'in', ['approved', 'pending']) | ||
.run({state: 'foo'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('state must be in approved,pending'); | ||
}) | ||
.then(function () { | ||
return credible.run({state: 'approved'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
describe('lowercase', function () { | ||
it('should test that a property is lowercase', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('name', 'lowercase') | ||
.run({name: 'Foo'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('name must be lowercase'); | ||
}) | ||
.then(function () { | ||
return credible.run({name: 'foo'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
describe('uppercase', function () { | ||
it('should test that a property is uppercase', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('name', 'uppercase') | ||
.run({name: 'Foo'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('name must be uppercase'); | ||
}) | ||
.then(function () { | ||
return credible.run({name: 'FOO'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
describe('luhn', function () { | ||
it('should test that a property is a valid credit card number', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('cc', 'luhn') | ||
.run({cc: '2143'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('cc must be a valid credit card number'); | ||
}) | ||
.then(function () { | ||
return credible.run({cc: '4444444444444444'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
describe('json', function () { | ||
it('should test that a property is valid JSON', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('json', 'json') | ||
.run({json: 'foo'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('json must be valid JSON'); | ||
}) | ||
.then(function () { | ||
return credible.run({json: '{"foo": "bar"}'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
describe('contains', function () { | ||
it('should test that a property contains a string', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('name', 'contains', 'noah') | ||
.run({name: 'foo'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('name must contain \'noah\''); | ||
}) | ||
.then(function () { | ||
return credible.run({name: 'noah portes chaikin'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
describe('matches', function () { | ||
it('should test that a property matches a regexp', function (ok) { | ||
var credible = new Credible() | ||
credible | ||
.rule('email', 'matches', /^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,6}$/i) | ||
.run({email: 'foo'}) | ||
.catch(function (e) { | ||
should(e.toString()).equal('email must match the following regular expression: /^[a-z0-9._%+\\-]+@[a-z0-9.\\-]+\\.[a-z]{2,6}$/i'); | ||
}) | ||
.then(function () { | ||
return credible.run({email: 'noah.porteschaikin@carrotcreative.com'}); | ||
}) | ||
.then(function () { | ||
ok(); | ||
}); | ||
}); | ||
}); | ||
}) |
72707
1457
327