jquense / yup

Dead simple Object schema validation
MIT License
22.86k stars 932 forks source link

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

Open stormfrazier22 opened 3 months ago

stormfrazier22 commented 3 months ago

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:

Implementation details:


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