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

Allow building rules using prefixes #1453

Merged
1 commit merged into from
Mar 24, 2024
Merged

Conversation

henriquemoody
Copy link
Member

@henriquemoody henriquemoody commented Mar 20, 2024

This PR introduce the allowing to create rules with the following prefixes.

I'm not so sure about that, and before I merge that to master, I thought about gathering some feedback first.

Here's how it will look like:

v::keyEquals('foo', 12)->assert(['foo' => 10]);
// foo must equal 12

v::propertyBetween('foo', 1, 3)->assert((object) ['foo' => 5]);
// foo must be between 1 and 3


v::lengthGreaterThan(3)->assert('foo');
// The length of "foo" must be greater than 3

v::maxOdd()->assert([1, 2, 3, 4]);
// As the maximum of `[1, 2, 3, 4]`, 4 must be an odd number

v::minEven()->assert([1, 2, 3]);
// As the minimum from `[1, 2, 3]`, 1 must be an even number

v::notBetween(1, 3)->assert(2);
// 2 must not be between 1 and 3


v::nullOrBoolType()->assert('string');
// "string" must be of type boolean

v::nullOrBoolType()->assert(null);
// 👆 doesn't throw any exception


v::undefOrUrl()->assert('string');
// "string" must be a URL

v::undefOrUrl()->assert(null);
// 👆 doesn't throw any exception

v::undefOrUrl()->assert('');
// 👆 doesn't throw any exception

Any thoughts?

Copy link

codecov bot commented Mar 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.70%. Comparing base (9eafe52) to head (97b243d).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1453      +/-   ##
============================================
+ Coverage     94.65%   94.70%   +0.04%     
- Complexity      950      962      +12     
============================================
  Files           201      202       +1     
  Lines          2172     2189      +17     
============================================
+ Hits           2056     2073      +17     
  Misses          116      116              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@henriquemoody henriquemoody force-pushed the prefix/length branch 2 times, most recently from ab2c761 to 6c7f790 Compare March 24, 2024 15:51
Although helpful, the changes in the Min, Max, and Length rules made
using those rules more verbose. This commit will simplify their use by
allowing users to use them as prefixes.

Because I was creating prefixes for those rules, I made other cool
prefixes. Doing that is scary because it will generate more code to
support, and I would have liked to avoid that. However, that's a
valuable addition, and it's worth the risk.

I might reconsider that in the future, but for now, that looks like a
good idea.

Signed-off-by: Henrique Moody <[email protected]>
@henriquemoody henriquemoody closed this pull request by merging all changes into Respect:main in 97b243d Mar 24, 2024
@henriquemoody henriquemoody deleted the prefix/length branch March 24, 2024 16:08
@henriquemoody
Copy link
Member Author

Still open for comments, of course. I can always revert the changes.

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

Successfully merging this pull request may close these issues.

1 participant