Skip to content
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

Error on invalid dates #214

Merged
merged 4 commits into from
Oct 20, 2023
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 @@ -6,6 +6,7 @@ All notable changes to the library will be documented in this file.

- Add `getRestAndDefaultArgs` utility function
- Add new `rest` argument to `object` and `objectAsync` schema
- Fix type check in `date` and `dateAsync` for invalid dates (pull request #214)
- Change `ObjectSchema` and `ObjectSchemaAsync` type
- Change type check in `tuple` and `tupleAsync` to be less strict
- Rename `ObjectShape` and `ObjectShapeAsync` types to `ObjectEntries` and `ObjectEntriesAsync`
Expand Down
1 change: 1 addition & 0 deletions library/src/schemas/date/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('date', () => {
expect(output).toEqual(input);
expect(() => parse(schema, 2023)).toThrowError();
expect(() => parse(schema, '2023-07-10')).toThrowError();
expect(() => parse(schema, new Date('Invalid Date'))).toThrowError();
expect(() => parse(schema, {})).toThrowError();
});

Expand Down
2 changes: 1 addition & 1 deletion library/src/schemas/date/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function date(
*/
_parse(input, info) {
// Check type of input
if (!(input instanceof Date)) {
if (!(input instanceof Date) || isNaN(input.getTime())) {
return getSchemaIssues(
info,
'type',
Expand Down
3 changes: 3 additions & 0 deletions library/src/schemas/date/dateAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('dateAsync', () => {
expect(output).toEqual(input);
await expect(parseAsync(schema, 2023)).rejects.toThrowError();
await expect(parseAsync(schema, '2023-07-10')).rejects.toThrowError();
await expect(
parseAsync(schema, new Date('Invalid Date'))
).rejects.toThrowError();
await expect(parseAsync(schema, {})).rejects.toThrowError();
});

Expand Down
2 changes: 1 addition & 1 deletion library/src/schemas/date/dateAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function dateAsync(
*/
async _parse(input, info) {
// Check type of input
if (!(input instanceof Date)) {
if (!(input instanceof Date) || isNaN(input.getTime())) {
return getSchemaIssues(
info,
'type',
Expand Down
2 changes: 1 addition & 1 deletion library/src/schemas/number/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function number(
*/
_parse(input, info) {
// Check type of input
if (typeof input !== 'number' || Number.isNaN(input)) {
if (typeof input !== 'number' || isNaN(input)) {
return getSchemaIssues(
info,
'type',
Expand Down
2 changes: 1 addition & 1 deletion library/src/schemas/number/numberAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function numberAsync(
*/
async _parse(input, info) {
// Check type of input
if (typeof input !== 'number' || Number.isNaN(input)) {
if (typeof input !== 'number' || isNaN(input)) {
return getSchemaIssues(
info,
'type',
Expand Down
Loading