Skip to content

Each overwriting messages #1334

@tchari

Description

@tchari

When you validate the elements of an array (or iterable) using each, the messages get "overwritten" when you get the exception message.

    try {
      Validator::notEmpty()->iterableType()->each(
        Validator::key('street', Validator::stringType()->notEmpty())
        ->key('region', Validator::stringType()->notEmpty())
        ->key('country', Validator::stringType()->notEmpty())
        ->key('other', Validator::nullable(Validator::notEmpty()->stringType()), false)
      )->assert(
        [
          ['region' => 'Oregon', 'country' => 'USA', 'other' => 123],
          ['street' => '', 'region' => 'Oregon', 'country' => 'USA'],
          ['street' => 123, 'region' => 'Oregon', 'country' => 'USA'],
        ]
      );
    } catch (NestedValidationException $e) {
      var_dump($e->getMessages());
    }

The above var_dump results in

array(1) {
  ["each"]=>
  string(29) "street must be of type string"
}

However, I would have expected

array(1) {
  ["each"]=>
  array(3) {
    [0]=>
    array(2) {
      ['street']=>
      string(22) "street must be present"
      ['other'] =>
      string(28) "other must be of type string"
    }
    [1]=>
    array(1) {
      ['street']=>
      string(24) "street must not be empty"
    }
    [2]=>
    array(1) {
      ['street']=>
      string(29) "street must be of type string"
    }
  }
}

It is a little alarming that no exception is present for the first erroneous 'other' field.
Is there a different rule for this?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions