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

Size rule should be aware of additional numeric rule #7

Open
jonwaldstein opened this issue Feb 9, 2023 · 2 comments
Open

Size rule should be aware of additional numeric rule #7

jonwaldstein opened this issue Feb 9, 2023 · 2 comments

Comments

@jonwaldstein
Copy link
Contributor

Currently, the Size rule assumes it will receive a string or number (int/float). This is problematic when validating request data that often comes in as string values. Intuitively, if the size rule is combined with numeric, then the value should be sanitized first as a number before validating.

Workaround by extending the Rule locally and adding sanitize based on value

 public function sanitize($value)
    {
        if (is_numeric($value)) {
            if (strpos($value, '.') !== false) {
                return (float)$value;
            }
  
            return (int)$value;
        }
  
        return $value;
    }

Proposed solution
Instead of sanitizing based on value, check if numeric or other number related rule is being used as well.

@JasonTheAdams
Copy link
Contributor

When building this, I made the conscious decision to rely more on types to inform how validations work. I understand that, practically, often times what's being validated is user input which comes in as a string, but now always. It also makes validation rules more complex as they're having to infer more from the value and sibling rules.

Currently, there's an integer rule which can be used before size to cast it properly. It sounds like if we add a float rule, that would help this use case.

Does that sound reasonable, @jonwaldstein?

@jonwaldstein
Copy link
Contributor Author

As we discussed, I like the idea of having min, max, and (maybe) size rules that correspond to numeric values - and for character length specific rules we use minLength, maxLength like native html rules work: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/minlength.

Another thing about the size rule I noticed, is that the constructor types the size as an integer. For the use case I need it for (possibility of floats) that size would need to just be numeric, get sanitized as int, float, or string - then do a comparison with the value (that would also need to be sanitized). I'm open to discussion about creating a new rule for these types of scenarios something like equals, same, digits, or is:value.

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

No branches or pull requests

2 participants