Skip to content
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

feat: [Validation] add field_exists rule #8123

Merged
merged 10 commits into from
Nov 19, 2023

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Oct 30, 2023

Needs #8131

Description
For #8006

  • add rule to check if the field exist in the data

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added enhancement PRs that improve existing functionalities 4.5 labels Oct 30, 2023
?string $error = null,
?string $field = null
): bool {
return array_key_exists($field, $data);
Copy link
Member Author

@kenjis kenjis Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we need to handle the key with dot array syntax?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I'm afraid so. See required_without above. May be worth having a private method for $data transformations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see your next PR - much better solution.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done.

@kenjis kenjis marked this pull request as draft October 31, 2023 06:05
@kenjis kenjis force-pushed the feat-validation-field_exists branch 2 times, most recently from 673ff74 to eda462a Compare November 8, 2023 07:46
@kenjis kenjis marked this pull request as ready for review November 8, 2023 08:00
@michalsn
Copy link
Member

michalsn commented Nov 8, 2023

I believe we would also need versions _with and _without.

Yes for dot array syntax. I see it's already done.

@kenjis
Copy link
Member Author

kenjis commented Nov 9, 2023

I believe we would also need versions _with and _without.

Really?
Do you have a use case?

@michalsn
Copy link
Member

Sorry, I forgot about this one.

Example... an API with a custom payload? Some optional fields depend on selected parameters. They're not required though.

I think I saw a request like this a long time ago, but I can't find it now.

@kenjis
Copy link
Member Author

kenjis commented Nov 12, 2023

I think we can use required_with in most cases.

Some optional fields depend on selected parameters. They're not required though.

If it is not required, then it is okay that the key does not exists in the data.
Also, if it depends on selected parameters, we need to write custom rule?

If it is required, and the value may be null, empty string, then we need field_exists_with.

@kenjis kenjis force-pushed the feat-validation-field_exists branch from f24c53a to cfa4fa1 Compare November 12, 2023 02:45
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I think starting here makes sense and if we find need for additional related rules they can follow.

@kenjis kenjis merged commit 8b72997 into codeigniter4:4.5 Nov 19, 2023
46 checks passed
@kenjis kenjis deleted the feat-validation-field_exists branch November 19, 2023 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.5 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants