CSVUtils icon indicating copy to clipboard operation
CSVUtils copied to clipboard

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

Open xanish opened this issue 4 years ago • 4 comments

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.

xanish avatar Aug 17 '21 15:08 xanish

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.

codecov[bot] avatar Aug 18 '21 08:08 codecov[bot]

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

xanish avatar Aug 23 '21 06:08 xanish

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

hoshomoh avatar Aug 26 '21 08:08 hoshomoh

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

xanish avatar Sep 24 '21 10:09 xanish