Skip to content

Change DECIMAL_REGEX to support floats that start with a dot #1086

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

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ All notable changes to the library will be documented in this file.
- Change `ISO_DATE_TIME_REGEX` and `ISO_TIMESTAMP_REGEX` to support space as separator (pull request #1064)
- Change pipe tuple of `pipe` and `pipeAsync` to be readonly by default
- Change `forward`, `forwardCheck`, `partialCheck` and `partialCheckAsync` to improve TypeScript performance (issue #987)
- Change `DECIMAL_REGEX` to support floats that start with a dot (pull request #1086)
- Refactor `bytes`, `maxBytes`, `minBytes` and `notBytes` action
- Fix implementation of `nonOptional`, `nonOptionalAsync`, `nonNullable`, `nonNullableAsync`, `nonNullish` and `nonNullishAsync` schema in edge cases (issue #909)
- Fix instantiation error for `any` in `PathKeys` type (issue #929)
Expand Down
32 changes: 18 additions & 14 deletions library/src/actions/decimal/decimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,32 @@ describe('decimal', () => {
expectNoActionIssue(action, ['00', '01', '12', '99']);
});

test('for multiple digits', () => {
expectNoActionIssue(action, ['1234', '0123456789']);
});

test('for float numbers', () => {
expectNoActionIssue(action, ['0.1', '123.456']);
});

test('for number signs', () => {
expectNoActionIssue(action, ['+1', '-1', '+123', '-123']);
expectNoActionIssue(action, ['+1', '-1', '+123', '-123', '+001', '-001']);
});

test('for float numbers with a number sign', () => {
test('for floats with a number sign', () => {
expectNoActionIssue(action, ['-2.0', '-52.61', '+4.0', '-11.31']);
});

test('for multiple digits', () => {
expectNoActionIssue(action, ['0123456789']);
test('for floats starting with a dot', () => {
expectNoActionIssue(action, ['.6', '.763']);
});

test('for floats starting with number sign followed by a dot', () => {
expectNoActionIssue(action, ['-.2', '-.922', '+.5', '+.452']);
});

test('for numbers with leading 0s', () => {
expectNoActionIssue(action, ['000', '000123', '000.123', '00012.3']);
});
});

Expand All @@ -108,26 +120,18 @@ describe('decimal', () => {
expectActionIssue(action, baseIssue, [' 1', '1 ', ' 1 ', '1 2']);
});

test('for number seperators', () => {
test('for invalid seperators', () => {
expectActionIssue(action, baseIssue, ['1,000', '1_000', '1 000']);
});

test('for exponential numbers', () => {
test('for scientific notation', () => {
expectActionIssue(action, baseIssue, ['1e3', '1e-3', '1e+3']);
});

test('for floats ending with a dot', () => {
expectActionIssue(action, baseIssue, ['1.', '342.']);
});

test('for floats starting with a dot', () => {
expectActionIssue(action, baseIssue, ['.6', '.763']);
});

test('for floats starting with number sign followed by a dot', () => {
expectActionIssue(action, baseIssue, ['-.2', '-.922', '+.5', '+.452']);
});

test('for floats with multiple dots', () => {
expectActionIssue(action, baseIssue, [
'1.2.3',
Expand Down
3 changes: 2 additions & 1 deletion library/src/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const CUID2_REGEX: RegExp = /^[a-z][\da-z]*$/u;
/**
* [Decimal](https://en.wikipedia.org/wiki/Decimal) regex.
*/
export const DECIMAL_REGEX: RegExp = /^[+-]?\d+(?:\.\d+)?$/u;
// eslint-disable-next-line redos-detector/no-unsafe-regex -- false positive
export const DECIMAL_REGEX: RegExp = /^[+-]?(?:\d*\.)?\d+$/u;

/**
* [Digits](https://en.wikipedia.org/wiki/Numerical_digit) regex.
Expand Down