Skip to content

Commit 1ecfd5b

Browse files
Merge pull request #1086 from fabian-hiller/feat-change-decimal-regex
Change DECIMAL_REGEX to support floats that start with a dot
2 parents 77f3302 + 99939ea commit 1ecfd5b

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

library/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ All notable changes to the library will be documented in this file.
3232
- Change `ISO_DATE_TIME_REGEX` and `ISO_TIMESTAMP_REGEX` to support space as separator (pull request #1064)
3333
- Change pipe tuple of `pipe` and `pipeAsync` to be readonly by default
3434
- Change `forward`, `forwardCheck`, `partialCheck` and `partialCheckAsync` to improve TypeScript performance (issue #987)
35+
- Change `DECIMAL_REGEX` to support floats that start with a dot (pull request #1086)
3536
- Refactor `bytes`, `maxBytes`, `minBytes` and `notBytes` action
3637
- Fix implementation of `nonOptional`, `nonOptionalAsync`, `nonNullable`, `nonNullableAsync`, `nonNullish` and `nonNullishAsync` schema in edge cases (issue #909)
3738
- Fix instantiation error for `any` in `PathKeys` type (issue #929)

library/src/actions/decimal/decimal.test.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,32 @@ describe('decimal', () => {
7373
expectNoActionIssue(action, ['00', '01', '12', '99']);
7474
});
7575

76+
test('for multiple digits', () => {
77+
expectNoActionIssue(action, ['1234', '0123456789']);
78+
});
79+
7680
test('for float numbers', () => {
7781
expectNoActionIssue(action, ['0.1', '123.456']);
7882
});
7983

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

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

88-
test('for multiple digits', () => {
89-
expectNoActionIssue(action, ['0123456789']);
92+
test('for floats starting with a dot', () => {
93+
expectNoActionIssue(action, ['.6', '.763']);
94+
});
95+
96+
test('for floats starting with number sign followed by a dot', () => {
97+
expectNoActionIssue(action, ['-.2', '-.922', '+.5', '+.452']);
98+
});
99+
100+
test('for numbers with leading 0s', () => {
101+
expectNoActionIssue(action, ['000', '000123', '000.123', '00012.3']);
90102
});
91103
});
92104

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

111-
test('for number seperators', () => {
123+
test('for invalid seperators', () => {
112124
expectActionIssue(action, baseIssue, ['1,000', '1_000', '1 000']);
113125
});
114126

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

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

123-
test('for floats starting with a dot', () => {
124-
expectActionIssue(action, baseIssue, ['.6', '.763']);
125-
});
126-
127-
test('for floats starting with number sign followed by a dot', () => {
128-
expectActionIssue(action, baseIssue, ['-.2', '-.922', '+.5', '+.452']);
129-
});
130-
131135
test('for floats with multiple dots', () => {
132136
expectActionIssue(action, baseIssue, [
133137
'1.2.3',

library/src/regex.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const CUID2_REGEX: RegExp = /^[a-z][\da-z]*$/u;
1717
/**
1818
* [Decimal](https://en.wikipedia.org/wiki/Decimal) regex.
1919
*/
20-
export const DECIMAL_REGEX: RegExp = /^[+-]?\d+(?:\.\d+)?$/u;
20+
// eslint-disable-next-line redos-detector/no-unsafe-regex -- false positive
21+
export const DECIMAL_REGEX: RegExp = /^[+-]?(?:\d*\.)?\d+$/u;
2122

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

0 commit comments

Comments
 (0)