-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Precognitive validation with nested arrays doesn't throw validation error #45405
[9.x] Precognitive validation with nested arrays doesn't throw validation error #45405
Conversation
Thanks so much for providing a failing test @morloderex. I've addressed the issue and will summarise things here. Issue 1It is possible with Precognition to specify the validation rules to run from the client, via a
The backend then runs a filter over the validation rules. $initialRules = $formRequest->rules();
$only = explode(',', $request->header('Precognition-Validate-Only'));
$rules = collect($initialRules)->only($only)->all(); The problem is that public function rules()
{
return [
'members.*.name' => 'required|string',
'members.*.email' => 'required|email',
];
} So we are unable, from the client, to get validation errors for The proposed fix for this is:
It is unfortunate to have to double set the validation rules. I could look at expanding them manually, but I feel it is best to just let the validator handle things internally - especially because then we don't need to worry about handling Issue 2When using the This will sound like the solution we have used for issue 1, however we are using Validator::make([], ['escaped\.dot' => 'required'])->getRules()
// [
// "escaped9LQiXghrbE8XL1Z8dot" => [
// "required",
// ],
// ] The introduction of Validator::make([], ['escaped\.dot' => 'required'])->getRulesWithoutPlaceholders()
// [
// "escaped\.dot" => [
// "required",
// ],
// ] So now when using
The ability to filter escaped dots was already possible via all the other means of using Precognition, such as FormRequests, $request->validate(), etc. So this fix standardises the behaviour across everything. A note on escaped keysUsing escaped keys with Precognition does mean that you need to escape your key in:
However, the unescaped key is to be used in:
This is not a change, just some context I'm putting here for others. |
{ | ||
return collect($this->rules) | ||
->mapWithKeys(fn ($value, $key) => [ | ||
str_replace($this->dotPlaceholder, '\\.', $key) => $value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not using the existing method for replacing placeholders, as it does not restore the backslash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ready for review.
@timacdonald thank a lot for taking a look at this. This bug has bitten me a bit. I hope it can be fixed with the next release. |
Thanks! |
Hello everybody,
I belive i have found a bug on the way the
filterPrecognitiveRules
method on theCanBePrecognitive
trait works. The reason is that the stringnested.*.name
have not being parsed to the corispondingnested.0.name
yet because the validator instance only looks afternested.*.name
and builds the rules array based on the incoming data turningnested.*.name
into n number of elements. This means that when we try to filter for only keys containingnested
andnested.0.name
withinfilterPrecognitiveRules
our rules haven't been parsed yet which mean that the rule for keynested.0.name
just get dropped. And never actually validated.I have attached a test case for this bug.