Skip to content

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

Open
stormfrazier22 wants to merge 1 commit into
jquense:masterfrom
stormfrazier22:NumberAsStringFixWithWhiteSpace
Open

Enhance NumberSchema to reject internal whitespace while allowing leading/trailing spaces#2236
stormfrazier22 wants to merge 1 commit into
jquense:masterfrom
stormfrazier22:NumberAsStringFixWithWhiteSpace

Conversation

@stormfrazier22

Copy link
Copy Markdown

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