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
37 changes: 35 additions & 2 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,52 @@
},

add: function ( node, object ) {
if ( object instanceof Assert || ( _isArray( object ) && object[ 0 ] instanceof Assert ) ) {
if ( object instanceof Assert ) {
this.nodes[ node ] = object;

return this;
}

if ( _isArray( object ) ) {
if ( object[ 0 ] instanceof Assert === false )
throw new Error( 'Should give at least one Assert on array nodes', object );

var clone = []; // Mutating the array can cause side effects.
var hasConstraint = false;

for ( var i = 0; i < object.length; i++ ) {
var constraint = object[ i ];
var isAssert = constraint instanceof Assert;
var isConstraint = constraint instanceof Constraint;

if ( !isAssert && !_isPlainObject( constraint ) && !isConstraint )
throw new Error( 'Should give Assert or Constraint on array nodes', object );

if ( isAssert ) {
clone[ i ] = constraint;

continue;
}

if ( hasConstraint )
throw new Error( 'Should not give more than one constraint on array nodes', object );

clone[ i ] = isConstraint ? constraint : new Constraint( constraint, this.options );
hasConstraint = true;
}

this.nodes[ node ] = clone;

return this;
}

if ( 'object' === typeof object && !_isArray( object ) ) {
this.nodes[ node ] = object instanceof Constraint ? object : new Constraint( object, this.options );

return this;
}

throw new Error( 'Should give an Assert, an Asserts array, a Constraint', object );
throw new Error( 'Should give an Assert, an Asserts array or Constraint', object );
},

has: function ( node, nodes ) {
Expand Down
42 changes: 42 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,48 @@ var Suite = function ( validatorjs, expect, AssertExtra ) {
expect( myConstraint.has( 'foo' ) ).to.be( true );
} )

it( 'should throw an error adding an invalid node', function () {
try {
new Constraint().add( 'foo', 'bar' );
expect().fails();
} catch ( err ) {
expect( err.message ).to.be( 'Should give an Assert, an Asserts array or Constraint' );
}
} )

it( 'should throw an error adding an array without a first assert', function () {
try {
new Constraint().add( 'foo', [] );
expect().fails();
} catch ( err ) {
expect( err.message ).to.be( 'Should give at least one Assert on array nodes' );
}
} )

it( 'should throw an error adding an array with an invalid node', function () {
try {
new Constraint().add( 'foo', [ new Assert().Required(), 'bar' ] );
expect().fails();
} catch ( err ) {
expect( err.message ).to.be( 'Should give Assert or Constraint on array nodes' );
}
} )

it( 'should throw an error adding an array with more than one Constraint node', function () {
try {
new Constraint().add( 'foo', [ new Assert().Required(), new Constraint(), new Constraint() ] );
expect().fails();
} catch ( err ) {
expect( err.message ).to.be( 'Should not give more than one constraint on array nodes' );
}
} )

it( 'should add a node: Array', function () {
var myConstraint = new Constraint();
myConstraint.add( 'foo', [ new Assert().Required(), new Constraint( { bar: new Assert().Required() } ), new Assert().NotBlank() ] );
expect( myConstraint.has( 'foo' ) ).to.be( true );
} )

it( 'should add a node: Assert', function () {
var myConstraint = new Constraint();
myConstraint.add( 'foo', new Assert().Length( { min: 10 } ) );
Expand Down