Skip to content

Commit

Permalink
Better default for numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
smonn committed Jun 27, 2022
1 parent 962c4c3 commit c27d5de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function applyNumberConfig(
let parsedValue = config.parser(value);
if (
config.default !== undefined &&
(parsedValue === undefined || parsedValue === null)
(parsedValue === undefined ||
parsedValue === null ||
Number.isNaN(parsedValue))
) {
parsedValue = config.default;
}
Expand Down
18 changes: 18 additions & 0 deletions test/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ test('number default', async () => {
isValid: true,
field: undefined,
});
assert.equal(await validate(null), {
state: 'valid',
value: 5,
isValid: true,
field: undefined,
});
assert.equal(await validate(Number.NaN), {
state: 'valid',
value: 5,
isValid: true,
field: undefined,
});
assert.equal(await validate(123), {
state: 'valid',
value: 123,
isValid: true,
field: undefined,
});
});

test.run();

0 comments on commit c27d5de

Please sign in to comment.