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 1 commit
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
5 changes: 5 additions & 0 deletions library/src/schemas/date/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe('date', () => {
expect(() => parse(schema, {})).toThrowError();
});

test('should pass only valid dates', () => {
const error = "Date is invalid!";
expect(() => parse(date(error), new Date("Not a date"))).toThrowError(error);
});

test('should throw custom error', () => {
const error = 'Value is not a date!';
expect(() => parse(date(error), 123)).toThrowError(error);
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) || Number.isNaN(input.getTime())) {
fabian-hiller marked this conversation as resolved.
Show resolved Hide resolved
return getSchemaIssues(
info,
'type',
Expand Down
5 changes: 5 additions & 0 deletions library/src/schemas/date/dateAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe('dateAsync', () => {
await expect(parseAsync(schema, {})).rejects.toThrowError();
});

test('should pass only valid dates', async () => {
const error = "Date is invalid!";
await expect(parseAsync(dateAsync(error), new Date("Not a date"))).rejects.toThrowError(error);
});

test('should throw custom error', async () => {
const error = 'Value is not a date!';
await expect(parseAsync(dateAsync(error), 123)).rejects.toThrowError(error);
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) || Number.isNaN(input.getTime())) {
return getSchemaIssues(
info,
'type',
Expand Down
Loading