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

Adding requiredWith and requiredWithout conditional rules #267

Merged
merged 6 commits into from
Aug 29, 2019

Conversation

mangopeaches
Copy link
Contributor

Hey there,

I ran into a condition a few weeks back where I needed to validate that I had either one item or two other items present in my data set and I ended up running validation twice on two different rule sets. These required rules I've added accomplish the same thing I was looking to do, where you can conditionally required fields based on whether or not other fields are present or not present in the data set and now it can be done in a single rule set! I added these new conditionals to the docs and there should be unit tests to cover each case, but please let me know if I missed anything or you have any comments.

Thanks,
Tom

@mangopeaches
Copy link
Contributor Author

I just had a thought, I think it makes sense to force an array for the parameter set of fields we're conditionally requiring against and then use the third parameter in the rule as a boolean (strict mode, like other rules do already) which in the case of requiredWith would mean it's required if ALL of the fields in the list are present and for requiredWithout would mean it's required if ALL of the fields in the list are not present. I think this is added functionality that would be used with minimal changes to the existing code; I'll make this update real quick later tonight and add tests and update the docs. If you think that's not intuitive let me know and I will skip this change. Thanks

@mangopeaches
Copy link
Contributor Author

I just pushed another commit to implement those additional strict rules for each, now you can do the following:

  • requiredWith - requires the field if any other fields are present, not null, and not the empty string
  • requiredWith (strict) - requires the field if ALL of the other fields are present, not null, and not the empty string
  • requiredWithout - requires the field if any of the fields are NOT present
  • requiredWithout (strict) - requires the field if ALL of the other fields are NOT present

I updated the tests and docs to include the strict versions. I suppose they could theoretically be their own separate rules requiredWithAll and requiredWithoutAll, but not sure if it's really necessary. Let me know your thoughts or if you have any suggestions.

Thanks!
Tom

@mangopeaches mangopeaches mentioned this pull request Oct 16, 2018
@thomas-breese
Copy link

bump?

@amodliszewski
Copy link

I would love to remove similar rules that I've wrote for my project and use these instead. Any plan on merge?

Copy link
Owner

@vlucas vlucas left a comment

Choose a reason for hiding this comment

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

I like this feature. Looks good.

@willemwollebrants willemwollebrants merged commit a03072b into vlucas:master Aug 29, 2019
@willemwollebrants
Copy link
Collaborator

I could have sworn I already merged this, sorry for the delay and thanks for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants