Validation
Validation copied to clipboard
[2.0, not critical] Nested validation, probably incorrect indentation in getFullMessage for AllOf rule
Hello! Let's look at this simple example:
$input = [
'first' => 'deer',
'second' => 'moose',
'third' => 'cat',
];
$v = new Validator();
$v->addRule(
new Rules\KeySet(
new Rules\Key('first', new Rules\AllOf(
new Rules\AlwaysInvalid,
new Rules\AlwaysInvalid
)),
new Rules\Key('second', new Rules\AllOf(
new Rules\AlwaysInvalid
)),
new Rules\Key('third', new Rules\AllOf(
new Rules\AlwaysInvalid,
new Rules\AlwaysInvalid
))
)
);
try {
$v->assert($input);
} catch (NestedValidationException $ex) {
echo $ex->getFullMessage();
}
It provides following ouput:
- All of the required rules must pass for
{ "first": "deer", "second": "moose", "third": "cat" }
- All of the required rules must pass for first
- first is always invalid
- first is always invalid
- second is always invalid
- All of the required rules must pass for third
- third is always invalid
- third is always invalid
So, when it goes to report errors for AllOf
(maybe for all the composites), which contains only one rule, it fails indentation
I am not actually sure if that's an serious error, because who on the earth will have AllOf rule with only one rule in it? :D ...well, my rules builder did once :D
Not sure, but at least this behavior could be mentioned in documentation Or, maybe, even fixed, if it will not take much time Or whatever, just keep it in mind while refactoring
How is progress of AllOf
's refactoring? I mean, have you done some big part already? Or someone (maybe, even me) can take this rule and refactor it?
Sorry for not pull requesting, i am still working full-time on our stuff...
Thanks for reporting! 🐼
I'm gonna see if I can fix it probably next week.
The AllOf
's refactoring is not up to speed as I would like: https://github.com/henriquemoody/Validation/tree/rule/allOf
There are quite some tricky steps because it influences the whole library.
I think this error also happens on 1.1
Also, if our first set of rules will contain only one rule, then indentation will be ruined completely:
$v->addRule(
new Rules\KeySet(
new Rules\Key('first', new Rules\AllOf(
new Rules\AlwaysInvalid
)),
new Rules\Key('second', new Rules\AllOf(
new Rules\AlwaysInvalid,
new Rules\AlwaysInvalid
)),
new Rules\Key('third', new Rules\AllOf(
new Rules\AlwaysInvalid,
new Rules\AlwaysInvalid
))
)
);
Our report becomes:
- All of the required rules must pass for
{ "first": "deer", "second": "moose", "third": "cat" }
- first is always invalid
- All of the required rules must pass for second
- second is always invalid
- second is always invalid
- All of the required rules must pass for third
- third is always invalid
- third is always invalid
Hi @StarveTheEgo!
I'm gonna see if I can fix it probably next week.
We both know how that wenr, don't we? I'm sorry for taking so long to reply. Life has changed a lot since 2019.
I'm happy to tell you that I finally changed that behaviour. I've recently refactored how the library works, and that's the reason why it got better.
In the next MAJOR version, this code:
$validator = Validator::create(
new Rules\KeySet(
new Rules\Key('first', new Rules\AllOf(
new Rules\AlwaysInvalid(),
new Rules\AlwaysInvalid()
)),
new Rules\Key('second', new Rules\AlwaysInvalid()),
new Rules\Key('third', new Rules\AllOf(
new Rules\AlwaysInvalid(),
new Rules\AlwaysInvalid()
))
)
);
try {
$validator->assert([
'first' => 'deer',
'second' => 'moose',
'third' => 'cat',
]);
} catch (ValidatorException $ex) {
echo $ex->getFullMessage();
}
Will output the following:
- All of the required rules must pass for `["first": "deer", "second": "moose", "third": "cat"]`
- All of the required rules must pass for first
- first is always invalid
- first is always invalid
- second is always invalid
- All of the required rules must pass for third
- third is always invalid
- third is always invalid
As you can see, the indentation is finally correct! As expected, the second
key is on the same level as the others!
I'm working hard to release that version as soon as possible. You can play with it in the main
branch if you want to.
I will close this ticket because of how old the ticket is, also before I've fixed it, although that will only be really fixed in version 3.0.
Feel free to comment here, even though the ticket is closed!
Oh, you did it! Very cool of you, sir :-)
That year, I managed to create own validation lib (but this is private package for the company, unfortunately), while taking your library as an example !
I am glad you managed to handle this issue, and i am also glad you kept track of this issue up to nowadays!
I am having a new team now though, and we cannot use that library i created there
I will definitely take a look at new version of your validator, because we need some
Nice! And good luck with your new team!