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

Enhance NumberSchema to reject internal whitespace while allowing leading/trailing spaces #2236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stormfrazier22
Copy link

Description

This PR modifies the NumberSchema to enhance number validation, addressing an issue where strings with internal whitespace (e.g., "9 9") were incorrectly passing validation.

Changes:

  1. Updated the transform function in the NumberSchema constructor to:
    • Trim leading and trailing whitespace from string inputs
    • Use a regular expression to strictly validate the number format
    • Reject strings with internal whitespace or non-numeric characters

Behavior changes:

  • "99 " and " 99" now pass validation (trimmed to 99)
  • "9 9" now fails validation (becomes NaN)
  • "9.9" continues to pass validation (becomes 9.9)
  • "9,9" now fails validation (becomes NaN)

Implementation details:

parsed = parsed.trim();
if (!/^\d+(\.\d+)?$/.test(parsed)) return NaN;

…ue where a string like "9 9" would pass validation because all whitespace was being removed. This ensures that the string only contains digits and an optional decimal point, white still trimming leading and trailing whitespace
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.

1 participant