From 5cb63ce3495f4dad78acbb8f2c924bff3612e6e8 Mon Sep 17 00:00:00 2001 From: Philip Woods Date: Tue, 5 Aug 2014 17:17:14 -0400 Subject: [PATCH] Added Key( obj ) Assert to determine if a string is a property in a gien object --- README.md | 1 + dist/extras.js | 2 +- dist/validator.js | 22 ++++++++++++++++++++-- dist/validator.min.js | 4 ++-- src/validator.js | 18 ++++++++++++++++++ tests/tests.js | 8 ++++++++ 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bba50dd..7f066f8 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ new Assert().Regexp( value ); new Assert().Required(); new Assert().Unique(); new Assert().Unique( { key: value } ); +new Assert().Key( { key1: val1, ... } ); // in extras.js new Assert().Eql( object ); diff --git a/dist/extras.js b/dist/extras.js index c36c863..1d31687 100644 --- a/dist/extras.js +++ b/dist/extras.js @@ -1,5 +1,5 @@ /*! * Part of validator.js -* Version 1.0.0 - built Sun Aug 03 2014 17:42:31 +* Version 1.0.0 - built Tue Aug 05 2014 17:14:57 */ !function(a){"function"==typeof define&&define.amd?define(["validator"],a):"object"==typeof exports?module.exports=a(require("./validator.js")):a(window["undefined"!=typeof validatorjs_ns?validatorjs_ns:"Validator"])}(function(a){a.Assert.prototype.Eql=function(c){if(this.__class__="Eql","undefined"==typeof c)throw new Error("Equal must be instanciated with an Array or an Object");return this.eql=c,this.validate=function(c){var d="function"==typeof this.eql?this.eql(c):this.eql;if(!b.eql(d,c))throw new a.Violation(this,c,{eql:d});return!0},this},a.Assert.prototype.Mac=function(){return this.__class__="Mac",this.validate=function(b){var c=/^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$/i;if("string"!=typeof b)throw new a.Violation(this,b,{value:Validator.errorCode.must_be_a_string});if(!c.test(b))throw new a.Violation(this,b);return!0},this},a.Assert.prototype.IPv4=function(){return this.__class__="IPv4",this.validate=function(b){var c=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;if("string"!=typeof b)throw new a.Violation(this,b,{value:Validator.errorCode.must_be_a_string});if(!c.test(b))throw new a.Violation(this,b);return!0},this};var b={eql:function(a,b){if(a===b)return!0;if("undefined"!=typeof Buffer&&Buffer.isBuffer(a)&&Buffer.isBuffer(b)){if(a.length!==b.length)return!1;for(var c=0;c=0;d--)if(e[d]!=f[d])return!1;for(d=e.length-1;d>=0;d--)if(c=e[d],!this.eql(a[c],b[c]))return!1;return!0}catch(g){return!1}}}}); \ No newline at end of file diff --git a/dist/validator.js b/dist/validator.js index 0c33608..74a9195 100644 --- a/dist/validator.js +++ b/dist/validator.js @@ -1,7 +1,7 @@ /*! * validator.js * Guillaume Potier - -* Version 1.0.0 - built Sun Aug 03 2014 17:42:31 +* Version 1.0.0 - built Tue Aug 05 2014 17:14:57 * MIT Licensed * */ @@ -489,7 +489,7 @@ var result, validator = new Validator(), count = 0, failures = {}, groups = this.groups.length ? this.groups : group; if ( !_isArray( collection ) ) - throw new Violation( this, array, { value: Validator.errorCode.must_be_an_array } ); + throw new Violation( this, collection, { value: Validator.errorCode.must_be_an_array } ); for ( var i = 0; i < collection.length; i++ ) { result = this.constraint ? @@ -628,6 +628,24 @@ return this; }, + Key: function ( obj ) { + this.__class__ = 'Key'; + + if ( 'object' !== typeof obj ) + throw new Error( 'Must pass object to Key Assertion' ); + + this.obj = obj; + + this.validate = function ( value ) { + if ( true !== ( obj.hasOwnProperty( value ) ) ) + throw new Violation( this, value, { obj: this.obj } ); + + return true; + }; + + return this; + }, + Length: function ( boundaries ) { this.__class__ = 'Length'; diff --git a/dist/validator.min.js b/dist/validator.min.js index 7da6dc0..e8d661e 100644 --- a/dist/validator.min.js +++ b/dist/validator.min.js @@ -1,8 +1,8 @@ /*! * validator.js * Guillaume Potier - -* Version 1.0.0 - built Sun Aug 03 2014 17:42:31 +* Version 1.0.0 - built Tue Aug 05 2014 17:14:57 * MIT Licensed * */ -!function(){var a={},b=function(a){this.__class__="Validator",this.__version__="1.0.0",this.options=a||{},this.bindingKey=this.options.bindingKey||"_validatorjsConstraint"};b.prototype={constructor:b,validate:function(a,b,c){if("string"!=typeof a&&"object"!=typeof a)throw new Error("You must validate an object or a string");return"string"==typeof a||g(a)?this._validateString(a,b,c):this.isBinded(a)?this._validateBindedObject(a,b):this._validateObject(a,b,c)},bind:function(a,b){if("object"!=typeof a)throw new Error("Must bind a Constraint to an object");return a[this.bindingKey]=new c(b),this},unbind:function(a){return"undefined"==typeof a._validatorjsConstraint?this:(delete a[this.bindingKey],this)},isBinded:function(a){return"undefined"!=typeof a[this.bindingKey]},getBinded:function(a){return this.isBinded(a)?a[this.bindingKey]:null},_validateString:function(a,b,c){var f,h=[];g(b)||(b=[b]);for(var i=0;i=0;l--)"Required"!==k[l].__class__||(i=k[l].requiresValidation(b));if(this.has(h,a)||this.options.strict||i)try{this.has(h,this.options.strict||i?a:void 0)||(new e).HaveProperty(h).validate(a),c=this._check(h,a[h],b),(g(c)&&c.length>0||!g(c)&&!f(c))&&(d[h]=c)}catch(m){d[h]=m}}return f(d)?!0:d},add:function(a,b){if(b instanceof e||g(b)&&b[0]instanceof e)return this.nodes[a]=b,this;if("object"==typeof b&&!g(b))return this.nodes[a]=b instanceof c?b:new c(b),this;throw new Error("Should give an Assert, an Asserts array, a Constraint",b)},has:function(a,b){return b="undefined"!=typeof b?b:this.nodes,"undefined"!=typeof b[a]},get:function(a,b){return this.has(a)?this.nodes[a]:b||null},remove:function(a){var b=[];for(var c in this.nodes)c!==a&&(b[c]=this.nodes[c]);return this.nodes=b,this},_bootstrap:function(a){if(a instanceof c)return this.nodes=a.nodes;for(var b in a)this.add(b,a[b])},_check:function(a,b,d){if(this.nodes[a]instanceof e)return this._checkAsserts(b,[this.nodes[a]],d);if(g(this.nodes[a]))return this._checkAsserts(b,this.nodes[a],d);if(this.nodes[a]instanceof c)return this.nodes[a].check(b,d);throw new Error("Invalid node",this.nodes[a])},_checkAsserts:function(a,b,c){for(var d,e=[],f=0;f0},addGroup:function(a){return g(a)?this.addGroups(a):(this.hasGroup(a)||this.groups.push(a),this)},removeGroup:function(a){for(var b=[],c=0;c=a)throw new d(this,a,{threshold:this.threshold});return!0},this},GreaterThanOrEqual:function(a){if(this.__class__="GreaterThanOrEqual","undefined"==typeof a)throw new Error("Should give a threshold value");return this.threshold=a,this.validate=function(a){if(""===a||isNaN(Number(a)))throw new d(this,a,{value:b.errorCode.must_be_a_number});if(this.threshold>a)throw new d(this,a,{threshold:this.threshold});return!0},this},InstanceOf:function(a){if(this.__class__="InstanceOf","undefined"==typeof a)throw new Error("InstanceOf must be instanciated with a value");return this.classRef=a,this.validate=function(a){if(!0!=a instanceof this.classRef)throw new d(this,a,{classRef:this.classRef});return!0},this},Length:function(a){if(this.__class__="Length",!a.min&&!a.max)throw new Error("Lenth assert must be instanciated with a { min: x, max: y } object");return this.min=a.min,this.max=a.max,this.validate=function(a){if("string"!=typeof a&&!g(a))throw new d(this,a,{value:b.errorCode.must_be_a_string_or_array});if("undefined"!=typeof this.min&&this.min===this.max&&a.length!==this.min)throw new d(this,a,{min:this.min,max:this.max});if("undefined"!=typeof this.max&&a.length>this.max)throw new d(this,a,{max:this.max});if("undefined"!=typeof this.min&&a.length>>0;if(0===c)return-1;var d=0;if(arguments.length>1&&(d=Number(arguments[1]),d!=d?d=0:0!==d&&1/0!=d&&d!=-1/0&&(d=(d>0||-1)*Math.floor(Math.abs(d)))),d>=c)return-1;for(var e=d>=0?d:Math.max(c-Math.abs(d),0);c>e;e++)if(e in b&&b[e]===a)return e;return-1});var f=function(a){for(var b in a)return!1;return!0},g=function(a){return"[object Array]"===Object.prototype.toString.call(a)};"function"==typeof define&&define.amd?define(function(){return a}):"undefined"!=typeof module&&module.exports?module.exports=a:window["undefined"!=typeof validatorjs_ns?validatorjs_ns:"Validator"]=a}(); \ No newline at end of file +!function(){var a={},b=function(a){this.__class__="Validator",this.__version__="1.0.0",this.options=a||{},this.bindingKey=this.options.bindingKey||"_validatorjsConstraint"};b.prototype={constructor:b,validate:function(a,b,c){if("string"!=typeof a&&"object"!=typeof a)throw new Error("You must validate an object or a string");return"string"==typeof a||g(a)?this._validateString(a,b,c):this.isBinded(a)?this._validateBindedObject(a,b):this._validateObject(a,b,c)},bind:function(a,b){if("object"!=typeof a)throw new Error("Must bind a Constraint to an object");return a[this.bindingKey]=new c(b),this},unbind:function(a){return"undefined"==typeof a._validatorjsConstraint?this:(delete a[this.bindingKey],this)},isBinded:function(a){return"undefined"!=typeof a[this.bindingKey]},getBinded:function(a){return this.isBinded(a)?a[this.bindingKey]:null},_validateString:function(a,b,c){var f,h=[];g(b)||(b=[b]);for(var i=0;i=0;l--)"Required"!==k[l].__class__||(i=k[l].requiresValidation(b));if(this.has(h,a)||this.options.strict||i)try{this.has(h,this.options.strict||i?a:void 0)||(new e).HaveProperty(h).validate(a),c=this._check(h,a[h],b),(g(c)&&c.length>0||!g(c)&&!f(c))&&(d[h]=c)}catch(m){d[h]=m}}return f(d)?!0:d},add:function(a,b){if(b instanceof e||g(b)&&b[0]instanceof e)return this.nodes[a]=b,this;if("object"==typeof b&&!g(b))return this.nodes[a]=b instanceof c?b:new c(b),this;throw new Error("Should give an Assert, an Asserts array, a Constraint",b)},has:function(a,b){return b="undefined"!=typeof b?b:this.nodes,"undefined"!=typeof b[a]},get:function(a,b){return this.has(a)?this.nodes[a]:b||null},remove:function(a){var b=[];for(var c in this.nodes)c!==a&&(b[c]=this.nodes[c]);return this.nodes=b,this},_bootstrap:function(a){if(a instanceof c)return this.nodes=a.nodes;for(var b in a)this.add(b,a[b])},_check:function(a,b,d){if(this.nodes[a]instanceof e)return this._checkAsserts(b,[this.nodes[a]],d);if(g(this.nodes[a]))return this._checkAsserts(b,this.nodes[a],d);if(this.nodes[a]instanceof c)return this.nodes[a].check(b,d);throw new Error("Invalid node",this.nodes[a])},_checkAsserts:function(a,b,c){for(var d,e=[],f=0;f0},addGroup:function(a){return g(a)?this.addGroups(a):(this.hasGroup(a)||this.groups.push(a),this)},removeGroup:function(a){for(var b=[],c=0;c=a)throw new d(this,a,{threshold:this.threshold});return!0},this},GreaterThanOrEqual:function(a){if(this.__class__="GreaterThanOrEqual","undefined"==typeof a)throw new Error("Should give a threshold value");return this.threshold=a,this.validate=function(a){if(""===a||isNaN(Number(a)))throw new d(this,a,{value:b.errorCode.must_be_a_number});if(this.threshold>a)throw new d(this,a,{threshold:this.threshold});return!0},this},InstanceOf:function(a){if(this.__class__="InstanceOf","undefined"==typeof a)throw new Error("InstanceOf must be instanciated with a value");return this.classRef=a,this.validate=function(a){if(!0!=a instanceof this.classRef)throw new d(this,a,{classRef:this.classRef});return!0},this},Key:function(a){if(this.__class__="Key","object"!=typeof a)throw new Error("Must pass object to Key Assertion");return this.obj=a,this.validate=function(b){if(!0!==a.hasOwnProperty(b))throw new d(this,b,{obj:this.obj});return!0},this},Length:function(a){if(this.__class__="Length",!a.min&&!a.max)throw new Error("Lenth assert must be instanciated with a { min: x, max: y } object");return this.min=a.min,this.max=a.max,this.validate=function(a){if("string"!=typeof a&&!g(a))throw new d(this,a,{value:b.errorCode.must_be_a_string_or_array});if("undefined"!=typeof this.min&&this.min===this.max&&a.length!==this.min)throw new d(this,a,{min:this.min,max:this.max});if("undefined"!=typeof this.max&&a.length>this.max)throw new d(this,a,{max:this.max});if("undefined"!=typeof this.min&&a.length>>0;if(0===c)return-1;var d=0;if(arguments.length>1&&(d=Number(arguments[1]),d!=d?d=0:0!==d&&1/0!=d&&d!=-1/0&&(d=(d>0||-1)*Math.floor(Math.abs(d)))),d>=c)return-1;for(var e=d>=0?d:Math.max(c-Math.abs(d),0);c>e;e++)if(e in b&&b[e]===a)return e;return-1});var f=function(a){for(var b in a)return!1;return!0},g=function(a){return"[object Array]"===Object.prototype.toString.call(a)};"function"==typeof define&&define.amd?define(function(){return a}):"undefined"!=typeof module&&module.exports?module.exports=a:window["undefined"!=typeof validatorjs_ns?validatorjs_ns:"Validator"]=a}(); \ No newline at end of file diff --git a/src/validator.js b/src/validator.js index f3076de..9281cbf 100644 --- a/src/validator.js +++ b/src/validator.js @@ -628,6 +628,24 @@ return this; }, + Key: function ( obj ) { + this.__class__ = 'Key'; + + if ( 'object' !== typeof obj ) + throw new Error( 'Must pass object to Key Assertion' ); + + this.obj = obj; + + this.validate = function ( value ) { + if ( true !== ( obj.hasOwnProperty( value ) ) ) + throw new Violation( this, value, { obj: this.obj } ); + + return true; + }; + + return this; + }, + Length: function ( boundaries ) { this.__class__ = 'Length'; diff --git a/tests/tests.js b/tests/tests.js index b3d6ade..d0a0b9d 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -494,6 +494,14 @@ var Suite = function ( Validator, expect, extras ) { expect( validate( 7, assert ).show() ).to.eql( { assert: 'LessThanOrEqual', value: 7, violation: { threshold: 5 } } ); } ) + it( 'Key', function () { + assert = new Assert().Key( { info: null, test: null } ); + + expect( validate( 'info', assert ) ).to.be( true ); + expect( validate( 'test', assert ) ).to.be( true ); + expect( validate( 'key', assert ).show() ).to.eql( { assert: 'Key', value: 'key', violation: { obj: { info: null, test: null } } } ); + } ) + if ( !extras ) return;