Skip to content

Add validation adding array nodes to Constraint#62

Open
ricardogama wants to merge 1 commit intoguillaumepotier:masterfrom
seegno-forks:enhancement/validate-constraint-array-nodes
Open

Add validation adding array nodes to Constraint#62
ricardogama wants to merge 1 commit intoguillaumepotier:masterfrom
seegno-forks:enhancement/validate-constraint-array-nodes

Conversation

@ricardogama
Copy link

@ricardogama ricardogama commented Nov 25, 2016

This PR improves validation of array nodes when instantiating or adding nodes to a Constraint object, since the following is currently allowed:

var constraint = new Constraint({
  foo: [new Assert().Required, 'foobar']
});

Although an error is thrown when validating against the constraint, the error could be previously thrown when instantiating it and with a more adequate message.

With such motivation these changes lead to the following behaviour:

  • If an array node is provided with the first element's type not being Assert an error is thrown with the following message:

    Should give at least one Assert on array nodes

    This behaviour was already implemented but this message seems more adequate than the fallback message.

  • If any element's type of an array node is other than Object, Assert or Constraint an error is thrown with the following message:

    Should give Assert or Constraint on array nodes

  • If there is more than one element with Object or Constraint type on an array node an error is thrown with the following message:

    Should not give more than one constraint on array nodes

  • The fallback error message for providing invalid nodes has been improved/fixed to:

    Should give an Assert, an Asserts array or Constraint

These changes do not break the current suite and tests were added for all cases, including a missing test for the fallback error message.

@ricardogama ricardogama force-pushed the enhancement/validate-constraint-array-nodes branch 3 times, most recently from 49755a4 to c8c54ac Compare November 28, 2016 14:04
@ricardogama ricardogama force-pushed the enhancement/validate-constraint-array-nodes branch from c8c54ac to 36bcc79 Compare November 28, 2016 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant