-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for date validation
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Result } from 'true-myth'; | ||
|
||
import { input, make, output } from '../../tests/helpers'; | ||
import { IsDate } from '../nestjs-swagger-dto'; | ||
|
||
describe('IsDate', () => { | ||
describe('single date', () => { | ||
class Test { | ||
@IsDate({ format: 'date' }) | ||
date!: Date; | ||
} | ||
|
||
it('accepts date', async () => { | ||
expect(await input(Test, { date: '2011-12-30' })).toStrictEqual( | ||
Result.ok(make(Test, { date: new Date('2011-12-30') })) | ||
); | ||
}); | ||
|
||
it('rejects invalid date', async () => { | ||
expect(await input(Test, { date: '2011-13-13' })).toStrictEqual( | ||
Result.err('date is not a valid Date') | ||
); | ||
|
||
expect(await input(Test, { date: 'not a date' })).toStrictEqual( | ||
Result.err('date is not formatted as `yyyy-mm-dd`') | ||
); | ||
|
||
expect(await input(Test, { date: '2011/12/02' })).toStrictEqual( | ||
Result.err('date is not formatted as `yyyy-mm-dd`') | ||
); | ||
}); | ||
|
||
// it('transforms to plain', async () => { | ||
// const dto = make(Test, { booleanField: true }); | ||
// expect(output(dto)).toStrictEqual({ booleanField: true }); | ||
// }); | ||
|
||
// it('rejects everything else', async () => { | ||
// const testValues: unknown[] = [ | ||
// { booleanField: 'true' }, | ||
// { booleanField: 'false' }, | ||
// { booleanField: 'abc' }, | ||
// { booleanField: 0 }, | ||
// { booleanField: [] }, | ||
// { booleanField: {} }, | ||
// { booleanField: null }, | ||
// {}, | ||
// ]; | ||
|
||
// for (const testValue of testValues) { | ||
// expect(await input(Test, testValue)).toStrictEqual( | ||
// Result.err('booleanField must be a boolean value') | ||
// ); | ||
// } | ||
// }); | ||
}); | ||
|
||
describe('single date-time', () => { | ||
class Test { | ||
@IsDate({ format: 'date-time' }) | ||
date!: Date; | ||
} | ||
|
||
it('accepts date-time', async () => { | ||
expect(await input(Test, { date: '2017-06-01T18:43:26.000Z' })).toStrictEqual( | ||
Result.ok(make(Test, { date: new Date('2017-06-01T18:43:26.000Z') })) | ||
); | ||
}); | ||
|
||
it('rejects invalid date-time', async () => { | ||
expect(await input(Test, { date: '2011-13-13' })).toStrictEqual( | ||
Result.err('date is not ISO8601 format') | ||
); | ||
|
||
expect(await input(Test, { date: 'not a date' })).toStrictEqual( | ||
Result.err('date is not ISO8601 format') | ||
); | ||
|
||
expect(await input(Test, { date: '2011/12/02' })).toStrictEqual( | ||
Result.err('date is not ISO8601 format') | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Transform } from 'class-transformer'; | ||
import { IsDate as IsDateCV, isDateString } from 'class-validator'; | ||
|
||
import { Base, compose, noop } from '../core'; | ||
|
||
const crudeDateRegex = /^\d{4}-\d{2}-\d{2}$/; | ||
|
||
// TODO: array support | ||
export const IsDate = ({ | ||
format, | ||
...base | ||
}: Omit<Base<Date>, 'isArray'> & { format: 'date' | 'date-time' }): PropertyDecorator => | ||
compose( | ||
{ type: 'string', format }, | ||
base, | ||
format === 'date' | ||
? Transform(({ key, value }) => { | ||
if (!crudeDateRegex.test(value)) { | ||
return new Error(`${key} is not formatted as \`yyyy-mm-dd\``); | ||
} | ||
|
||
const date = new Date(value); | ||
if (isNaN(date.getTime())) { | ||
return new Error(`${key} is not a valid Date`); | ||
} | ||
|
||
return date; | ||
}) | ||
: Transform(({ key, value }) => { | ||
if (!isDateString(value, { strict: true })) { | ||
return new Error(`${key} is not ISO8601 format`); | ||
} | ||
|
||
return new Date(value); | ||
}), | ||
IsDateCV({ message: ({ value }) => value.message }) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters