Skip to content

Each inverts names #1639

@alganet

Description

@alganet

In 3.0, the Each validator is inverting names.

Consider this example:

test('Inner and outer names.', catchAll(
    fn() => v::named('my_file.txt', v::each(v::named('line', v::intType())))->assert(['a', 'b', 'c']),
    fn(string $message, string $fullMessage, array $messages) => expect()
        ->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
        - Each item in my_file.txt must be valid
          - `.0` must be an integer
          - `.1` must be an integer
          - `.2` must be an integer
        FULL_MESSAGE)
));

In this scenario, ['a', 'b', 'c'] is the return of some file('my_file.txt'), a text file with 3 lines containing each of those letters.

Currently, it does not pass, and generates Each item in line must be valid.

This example is clearer than the ones in the codebase that use Wrapper and Wrapped and are easy to overlook. I'm using my_file.txt and line as a real example (each line of my_file.txt must be XXX, a common type of validation).

Ideally, if the validator passed to Each has a name, we want this:

test('Inner and outer names.', catchAll(
    fn() => v::named('my_file.txt', v::each(v::named('line', v::intType())))->assert(['a', 'b', 'c']),
    fn(string $message, string $fullMessage, array $messages) => expect()
        ->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
        - Each item in my_file.txt must be valid
          - `.0` (<- line) must be an integer
          - `.1` (<- line) must be an integer
          - `.2` (<- line) must be an integer
        FULL_MESSAGE)
));

We could omit (<- line) in the messages if the validator passed does not have an explicit name.

That would allow us to have a descriptive message of the whole (in this case, the file) and also the part (each line) with clear keys visible as well.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions