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

Extended validators to allow array values, dependent field validations and added new rules #22

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

xanish
Copy link

@xanish xanish commented Aug 17, 2021

Hi,

I am planning to use the library on one of my projects. Thought I'd share some changes here as well to help out the community.

  • Added support for array of values in rule parameters
    • The current ParameterizedRuleInterface is restrictive in terms of having to specify exact number of values which a rule would receive
    • The new ArrayParameterizedRuleInterface aims to relax the constraints and allow rules to make use of multiple parameter values
    • Added new rules like in and required_if using the above interface
  • Added support for dependent field validations
    • The passes() function now receives the current validation row which allows for dependent field validation checks
    • Added new rule required_if which is based on dependent field validation
  • Added new rules
    • alpha
    • alpha_num
    • in:foo,bar,baz
    • integer
    • numeric
    • required
    • required_if:field,value1,value2
    • min:min_val
    • max:max_val
  • Following rules now silently pass if the field value is empty. Since we now have a explicit required rule, we can easily ignore empty cells unless required by user explicitly
    • alpha, alpha_num, in, integer, numeric and url will now return true for null / empty string values
  • Modified between rule to work for both strings and numbers, min and max rules will also work for both strings and numbers
  • Added test cases for new rules, updated existing ones
  • Updated documentation with new rules

Let me know if any change is required.

@codecov
Copy link

codecov bot commented Aug 18, 2021

Codecov Report

Merging #22 (0045669) into master (34afb02) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##              master       #22   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity        94       141   +47     
===========================================
  Files              9        20   +11     
  Lines            230       320   +90     
===========================================
+ Hits             230       320   +90     
Impacted Files Coverage Δ
src/Helpers/FormatsMessages.php 100.00% <100.00%> (ø)
src/Rules/Alpha.php 100.00% <100.00%> (ø)
src/Rules/AlphaNum.php 100.00% <100.00%> (ø)
src/Rules/AsciiOnly.php 100.00% <100.00%> (ø)
src/Rules/Between.php 100.00% <100.00%> (ø)
src/Rules/ClosureValidationRule.php 100.00% <100.00%> (ø)
src/Rules/In.php 100.00% <100.00%> (ø)
src/Rules/Integer.php 100.00% <100.00%> (ø)
src/Rules/Max.php 100.00% <100.00%> (ø)
src/Rules/MaxLength.php 100.00% <100.00%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34afb02...0045669. Read the comment docs.

@xanish
Copy link
Author

xanish commented Aug 23, 2021

Hi, @hoshomoh, can you help me with this. Checks still haven't completed it seems.

@hoshomoh
Copy link
Owner

This is quite the change. I will take some time this coming weekend to review. Great initiative.

@xanish
Copy link
Author

xanish commented Sep 24, 2021

Made a few changes recently

  • Added an optional trimming function to trim row values if needed
  • Reverted Min, Max and Between to only work for Numbers as this would've caused issues since more information was needed to figure out whether we should check for length or the exact value of the field. (Ideally to make these generalised we would have to check if the attribute already has a number or integer validation then validate for value and if not then validate for length)
  • Since I removed the Length checks from above rules added MinLength and MaxLength validators

* parsed in the same order and format as allowedParameters().
* This will aid in mapping our parameters to their placeholders.
*/
public function parseParameterValues(array $parameters): array;
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is only useful when there a single parameter is allowed. Instead of doing this, I would rather update

protected function replaceParameterPlaceholder(

protected function replaceParameterPlaceholder(
    string $message,
    array $allowedParameters,
    array $parameters
): string {
    $hasMultipleAllowedParameter = count($allowedParameters) > 1;
    $search = $hasMultipleAllowedParameter ? $allowedParameters : $allowedParameters[0];
    $replace = $hasMultipleAllowedParameter ? $parameters : implode(',', $parameters);

    return str_replace($search, $replace, $message);
}

Copy link
Author

Choose a reason for hiding this comment

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

Yes, this works as well. I just wanted to keep it open in case someone needed multiple parameters.

@@ -88,6 +89,14 @@ protected function makeReplacements(
);
}

if ($rule instanceof ArrayParameterizedRuleInterface) {
Copy link
Owner

Choose a reason for hiding this comment

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

See my comment above

* parsed in the same order and format as allowedParameters().
* This will aid in mapping our parameters to their placeholders.
*/
public function parseParameterValues(array $parameters): array
Copy link
Owner

Choose a reason for hiding this comment

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

See my comment above

* parsed in the same order and format as allowedParameters().
* This will aid in mapping our parameters to their placeholders.
*/
public function parseParameterValues(array $parameters): array
Copy link
Owner

Choose a reason for hiding this comment

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

I am not sure why this was needed at all. Maybe I am not seeing something. My guess is this will be used like ["requiredIf:field,value"]

Copy link
Author

Choose a reason for hiding this comment

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

Yes, for my use case I had a couple of fields which had to be made required in case some target column had one of the values specified in :other_values.

Copy link
Owner

Choose a reason for hiding this comment

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

I see. It seems we can't run away from supporting array parameters. Let's do the then. I will make a fix to support array parameters like so, all rules can accept parameters in one of the following ways

  • ["requiredIf:field,value"]
  • ["requiredIf" => ["field" => "value"]
  • ["requiredIf" => ["field1" => "value1", "field2" => "value2"]
  • ["requiredIf" => ["field1" => ["value1", "value2"]]

What do you think? I will try to push this over the weekend.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good.

@@ -357,6 +370,13 @@ protected function passesParameterCheck($rule, array $parameters): bool
return $parameterCount === $ruleParameterCount;
}

if ($rule instanceof ArrayParameterizedRuleInterface) {
Copy link
Owner

Choose a reason for hiding this comment

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

This no longer needed

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

2 participants