Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion dist/extras.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions dist/validator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* validator.js
* Guillaume Potier - <guillaume@wisembly.com>
* 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
*
*/
Expand Down Expand Up @@ -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 ?
Expand Down Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions dist/validator.min.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
8 changes: 8 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down