Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validate group argument to options object #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ With same objects than above, just by adding validation groups:
phone: is( 'edit' ).notBlank()
};

validator.validate( object, constraint, 'edit' );
validator.validate( object, constraint, {
group: 'edit'
});
```

will return `true` in this case `{ firstname: [ Violation ], phone: [ Violation ] }`.
Expand All @@ -116,7 +118,7 @@ validate against Asserts that do not have a validation group.

```js
validator.bind( object, constraint );
validator.validate( object, groups );
validator.validate( object, options );
```

Under the hood, by default, a `_validatorjsConstraint` key will be created in object
Expand Down
51 changes: 26 additions & 25 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@
constructor: Validator,

/*
* Validate string: validate( string, Assert, string ) || validate( string, [ Assert, Assert ], [ string, string ] )
* Validate object: validate( object, Constraint, string ) || validate( object, Constraint, [ string, string ] )
* Validate binded object: validate( object, string ) || validate( object, [ string, string ] )
* Validate string: validate( string, Assert, object ) || validate( string, [ Assert, Assert ], object )
* Validate object: validate( object, Constraint, object )
* Validate binded object: validate( object, object )
*/
validate: function ( objectOrString, AssertsOrConstraintOrGroup, group ) {
validate: function ( objectOrString, AssertsOrConstraintOrOptions, options ) {
if ( 'string' !== typeof objectOrString && 'object' !== typeof objectOrString )
throw new Error( 'You must validate an object or a string' );

// string / array validation
if ( 'string' === typeof objectOrString || _isArray(objectOrString) )
return this._validateString( objectOrString, AssertsOrConstraintOrGroup, group );
return this._validateString( objectOrString, AssertsOrConstraintOrOptions, options );

// binded object validation
if ( this.isBinded( objectOrString ) )
return this._validateBindedObject( objectOrString, AssertsOrConstraintOrGroup );
return this._validateBindedObject( objectOrString, AssertsOrConstraintOrOptions );

// regular object validation
return this._validateObject( objectOrString, AssertsOrConstraintOrGroup, group );
return this._validateObject( objectOrString, AssertsOrConstraintOrOptions, options );
},

bind: function ( object, constraint ) {
Expand Down Expand Up @@ -75,7 +75,7 @@
return this.isBinded( object ) ? object[ this.bindingKey ] : null;
},

_validateString: function ( string, assert, group ) {
_validateString: function ( string, assert, options ) {
var result, failures = [];

if ( !_isArray( assert ) )
Expand All @@ -85,7 +85,7 @@
if ( ! ( assert[ i ] instanceof Assert) )
throw new Error( 'You must give an Assert or an Asserts array to validate a string' );

result = assert[ i ].check( string, group, string );
result = assert[ i ].check( string, options && options.group, string );

if ( true !== result )
failures.push( result );
Expand All @@ -94,21 +94,22 @@
return failures.length ? failures : true;
},

_validateObject: function ( object, constraint, group ) {
_validateObject: function ( object, constraint, options ) {
if ( 'object' !== typeof constraint )
throw new Error( 'You must give a constraint to validate an object' );

if ( constraint instanceof Assert )
return constraint.check( object, group, object );
return constraint.check( object, options && options.group, object );


if ( constraint instanceof Constraint )
return constraint.check( object, group, object );
return constraint.check( object, options, object );

return new Constraint( constraint ).check( object, group );
return new Constraint( constraint ).check( object, options );
},

_validateBindedObject: function ( object, group ) {
return object[ this.bindingKey ].check( object, group, object);
_validateBindedObject: function ( object, options ) {
return object[ this.bindingKey ].check( object, options, object);
}
};

Expand Down Expand Up @@ -179,12 +180,12 @@
return false;
},

check: function ( object, group ) {
check: function ( object, options ) {
var result, failures = {};

// check all constraint nodes.
for ( var property in this.nodes ) {
var isRequired = this.isRequired( property, group, this.options.deepRequired );
var isRequired = this.isRequired( property, options && options.group, this.options.deepRequired );

if ( ! this.has( property, object ) && ! this.options.strict && ! isRequired ) {
continue;
Expand All @@ -196,7 +197,7 @@
new Assert().HaveProperty( property ).validate( object );
}

result = this._check( property, object[ property ], group, object );
result = this._check( property, object[ property ], options, object );

// check returned an array of Violations or an object mapping Violations
if ( ( _isArray( result ) && result.length > 0 ) || ( !_isArray( result ) && !_isEmptyObject( result ) ) ) {
Expand Down Expand Up @@ -256,18 +257,18 @@
this.add( node, data[ node ] );
},

_check: function ( node, value, group, context ) {
_check: function ( node, value, options, context ) {
// Assert
if ( this.nodes[ node ] instanceof Assert )
return this._checkAsserts( value, [ this.nodes[ node ] ], group, context );
return this._checkAsserts( value, [ this.nodes[ node ] ], options && options.group, context );

// Asserts
if ( _isArray( this.nodes[ node ] ) )
return this._checkAsserts( value, this.nodes[ node ], group, context );
return this._checkAsserts( value, this.nodes[ node ], options && options.group, context );

// Constraint -> check api
if ( this.nodes[ node ] instanceof Constraint )
return this.nodes[ node ].check( value, group, context );
return this.nodes[ node ].check( value, options, context );

throw new Error( 'Invalid node', this.nodes[ node ] );
},
Expand Down Expand Up @@ -575,15 +576,15 @@
this.constraint = _isPlainObject( assertOrConstraint ) ? new Constraint( assertOrConstraint ) : assertOrConstraint;

this.validate = function ( collection, group ) {
var result, validator = new Validator(), count = 0, failures = {}, groups = this.groups.length ? this.groups : group;
var result, validator = new Validator(), count = 0, failures = {}, options = { group: this.groups.length ? this.groups : group };

if ( !_isArray( collection ) )
throw new Violation( this, collection, { value: Validator.errorCode.must_be_an_array } );

for ( var i = 0; i < collection.length; i++ ) {
result = this.constraint ?
validator.validate( collection[ i ], this.constraint, groups ) :
validator.validate( collection[ i ], groups );
validator.validate( collection[ i ], this.constraint, options ) :
validator.validate( collection[ i ], options );

if ( !_isEmptyObject( result ) )
failures[ count ] = result;
Expand Down
34 changes: 17 additions & 17 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,19 +1036,19 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
it( 'should use groups for validation', function() {
var asserts = [ new Assert().Length( { min: 4 } ).addGroup( 'bar' ), new Assert().Length( { min: 8 } ).addGroup( 'baz' ), new Assert().Length( { min: 2 } ) ];
expect( validator.validate( 'foo', asserts ) ).to.be( true );
expect( validator.validate( 'foo', asserts, 'bar' ) ).not.to.be( true );
expect( validator.validate( 'foofoo', asserts, 'bar' ) ).to.be( true );
expect( validator.validate( 'foofoo', asserts, 'baz' ) ).not.to.be( true );
expect( validator.validate( 'foofoofoo', asserts, 'baz' ) ).to.be( true );
expect( validator.validate( 'foo', asserts, { group: 'bar' } ) ).not.to.be( true );
expect( validator.validate( 'foofoo', asserts, { group: 'bar' } ) ).to.be( true );
expect( validator.validate( 'foofoo', asserts, { group: 'baz' } ) ).not.to.be( true );
expect( validator.validate( 'foofoofoo', asserts, { group: 'baz' } ) ).to.be( true );
} )

it( 'should use numbers as groups for validation', function () {
var asserts = [ new Assert().Length( { min: 4 } ).addGroup( 512 ), new Assert().Length( { min: 8 } ).addGroup( 1024 ), new Assert().Length( { min: 2 } ) ];
expect( validator.validate( 'foo', asserts ) ).to.be( true );
expect( validator.validate( 'foo', asserts, 512 ) ).not.to.be( true );
expect( validator.validate( 'foofoo', asserts, 512 ) ).to.be( true );
expect( validator.validate( 'foofoo', asserts, 1024 ) ).not.to.be( true );
expect( validator.validate( 'foofoofoo', asserts, 1024 ) ).to.be( true );
expect( validator.validate( 'foo', asserts, { group: 512 } ) ).not.to.be( true );
expect( validator.validate( 'foofoo', asserts, { group: 512 } ) ).to.be( true );
expect( validator.validate( 'foofoo', asserts, { group: 1024 } ) ).not.to.be( true );
expect( validator.validate( 'foofoofoo', asserts, { group: 1024 } ) ).to.be( true );
} )
})

Expand Down Expand Up @@ -1293,14 +1293,14 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
expect( result ).not.to.have.key( 'name' );
expect( result ).not.to.have.key( 'phone' );

var result = validator.validate( object, constraint, 'edit' );
var result = validator.validate( object, constraint, { group: 'edit' } );
expect( result ).not.to.eql( {} );
expect( result ).not.to.have.key( 'email' );
expect( result ).not.to.have.key( 'firstname' );
expect( result ).not.to.have.key( 'name' );
expect( result ).to.have.key( 'phone' );

var result = validator.validate( object, constraint, [ 'edit', 'foo' ] );
var result = validator.validate( object, constraint, { group: [ 'edit', 'foo' ] } );
expect( result ).not.to.eql( {} );
expect( result ).not.to.have.key( 'email' );
expect( result ).to.have.key( 'firstname' );
Expand Down Expand Up @@ -1419,7 +1419,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
} )

it( 'should validate against all Asserts with "Any" group', function () {
var result = validator.validate( object, constraint, 'Any' );
var result = validator.validate( object, constraint, { group: 'Any' } );
expect( result ).to.have.key( 'foo' );
expect( result ).to.have.key( 'bar' );
expect( result ).to.have.key( 'baz' );
Expand All @@ -1428,7 +1428,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
} )

it( 'should validate only a specific validation group', function () {
var result = validator.validate( object, constraint, 'foo' );
var result = validator.validate( object, constraint, { group: 'foo' } );
expect( result ).to.have.key( 'foo' );
expect( result ).not.to.have.key( 'bar' );
expect( result ).not.to.have.key( 'baz' );
Expand All @@ -1437,7 +1437,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
} )

it( 'should validate only two specific validation groups', function () {
var result = validator.validate( object, constraint, [ 'foo', 'baz' ] );
var result = validator.validate( object, constraint, { group: [ 'foo', 'baz' ] } );
expect( result ).to.have.key( 'foo' );
expect( result ).to.have.key( 'bar' );
expect( result ).not.to.have.key( 'baz' );
Expand All @@ -1446,7 +1446,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
} )

it( 'should validate more validation groups', function () {
var result = validator.validate( object, constraint, [ 'foo', 'qux', 'bar' ] );
var result = validator.validate( object, constraint, { group: [ 'foo', 'qux', 'bar' ] } );
expect( result ).to.have.key( 'foo' );
expect( result ).not.to.have.key( 'bar' );
expect( result ).not.to.have.key( 'baz' );
Expand All @@ -1455,7 +1455,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
} )

it( 'should validate groups with "Default"', function () {
var result = validator.validate( object, constraint, [ 'foo', 'Default' ] );
var result = validator.validate( object, constraint, { group: [ 'foo', 'Default' ] } );
expect( result ).to.have.key( 'foo' );
expect( result ).not.to.have.key( 'bar' );
expect( result ).to.have.key( 'baz' );
Expand All @@ -1466,7 +1466,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
it( 'should validate groups with binded object', function () {
validator.bind( object, constraint );

var result = validator.validate( object, [ 'foo', 'Default' ] );
var result = validator.validate( object, { group: [ 'foo', 'Default' ] } );
expect( result ).to.have.key( 'foo' );
expect( result ).not.to.have.key( 'bar' );
expect( result ).to.have.key( 'baz' );
Expand All @@ -1477,7 +1477,7 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
it( 'should validate groups in Collection constraint', function () {
validator.bind( object, constraint );
var nestedObj = { foo: null, items: [ object, object ] },
result = validator.validate( nestedObj, { items: new Assert().Collection() }, [ 'foo', 'Default' ] );
result = validator.validate( nestedObj, { items: new Assert().Collection() }, { group: [ 'foo', 'Default' ] } );

expect( result ).to.have.key( 'items' );
expect( result.items[ 0 ] ).to.have.key( '0' );
Expand Down