Skip to content

Adding requiredWith and requiredWithout conditional rules#267

Merged
willemwollebrants merged 6 commits into
vlucas:masterfrom
mangopeaches:required-conditionals
Aug 29, 2019
Merged

Adding requiredWith and requiredWithout conditional rules#267
willemwollebrants merged 6 commits into
vlucas:masterfrom
mangopeaches:required-conditionals

Conversation

@mangopeaches

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

bump?

@amodliszewski

Copy link
Copy Markdown

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

@vlucas vlucas left a comment

Copy link
Copy Markdown
Owner

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
Copy Markdown
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.

5 participants