Skip to content

Commit

Permalink
just always require the rules array to have each rule as array, the R…
Browse files Browse the repository at this point in the history
…ulesBuilder adds convenience here
  • Loading branch information
philiplb committed Mar 27, 2016
1 parent 4b9ac67 commit 1ca989f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
15 changes: 5 additions & 10 deletions src/Valdi/Validator.php
Expand Up @@ -73,12 +73,8 @@ protected function isValidRule($validator, $parameters, $value) {
protected function isValidField($fieldRules, $value) {
$result = array();
foreach ($fieldRules as $rule) {
$name = $rule;
$parameters = array();
if (is_array($rule)) {
$parameters = $rule;
$name = array_shift($parameters);
}
$parameters = $rule;
$name = array_shift($parameters);
$valid = $this->isValidRule($name, $parameters, $value);
if (!$valid) {
$result[] = $name;
Expand Down Expand Up @@ -128,10 +124,9 @@ public function addValidator($name, ValidatorInterface $validator) {
*
* @param array $rules
* the validation rules: an array with a field name as key and an array
* of rules to use for this field; each rule is either a string with the
* validator name or an array with the validator name as first element and
* parameters as following elements; example:
* array('a' => array('required'), 'b' => array(array('min', 1)))
* of rules to use for this field; each rule is an array with the validator
* name as first element and parameters as following elements; example:
* array('a' => array(array('required')), 'b' => array(array('min', 1)))
* @param array $data
* the data to validate as a map
*
Expand Down
16 changes: 8 additions & 8 deletions tests/ValdiTests/ValidatorTest.php
Expand Up @@ -27,12 +27,12 @@ public function testIsValid() {
'f' => '',
);
$rules = array(
'a' => array('required'),
'a' => array(array('required')),
'b' => array(array('required')),
'c' => array('required'),
'd' => array('required', 'integer'),
'e' => array('required', 'integer', 'floating'),
'f' => array('required', 'integer'),
'c' => array(array('required')),
'd' => array(array('required'), array('integer')),
'e' => array(array('required'), array('integer'), array('floating')),
'f' => array(array('required'), array('integer')),
);
$read = $validator->isValid($rules, $data);
$expected = array(
Expand All @@ -51,7 +51,7 @@ public function testIsValid() {
'b' => 'b',
);
$rules = array(
'a' => array('required'),
'a' => array(array('required')),
'b' => array(array('required')),
);
$read = $validator->isValid($rules, $data);
Expand All @@ -65,7 +65,7 @@ public function testIsValid() {
public function testInvalidRule() {
$validator = new Validator();
$rules = array(
'a' => array('invalid')
'a' => array(array('invalid'))
);
try {
$read = $validator->isValid($rules, array());
Expand All @@ -82,7 +82,7 @@ public function testInvalidRule() {
public function testAddValidator() {
$validator = new Validator();
$rules = array(
'a' => array('test')
'a' => array(array('test'))
);
try {
$read = $validator->isValid($rules, array('a' => 1));
Expand Down

0 comments on commit 1ca989f

Please sign in to comment.