Skip to content

Commit

Permalink
fix: add typesafety for dateFomat validator
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Sep 23, 2023
1 parent eb4e968 commit 6b954f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"casl",
"CHECKINGS",
"cloudinary",
"DDTHH",
"decapitalize",
"deserialization",
"DOESNT",
Expand Down Expand Up @@ -39,6 +40,7 @@
"rabbitmq",
"Requote",
"rubiin",
"SSSZ",
"UNAUTHORISED"
]
}
16 changes: 15 additions & 1 deletion src/common/decorators/validation/is-date-format.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import { isArray } from "helper-fns";

/* It validates that a date is in a given format */


export type DateFormats =
| "yyyy-MM-dd" // ISO 8601 Format
| "dd/MM/yyyy"
| "MM/dd/yyyy"
| "yyyy-MM-dd HH:mm:ss" // Full Date and Time (Timestamp)
| "yyyy-MM-dd" // Date Only
| "yyyy/MM/dd" // Alternate Date Format
| "yyyy.MM.dd" // Alternate Date Format
| "MM-dd-yyyy" // Alternate Date Format
| "dd MMM yyyy" // Alternate Date Format
| "yyyy-MM-ddTHH:mm:ss" // ISO Date-Time Format
| "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; // ISO Date-Time String Format

@ValidatorConstraint({ async: true })
class IsDateInFormatConstraint implements ValidatorConstraintInterface {
async validate(value: string | string[], arguments_: ValidationArguments) {
Expand All @@ -32,7 +46,7 @@ class IsDateInFormatConstraint implements ValidatorConstraintInterface {
}

export const IsDateInFormat = (
format: string,
format: DateFormats,
validationOptions?: ValidationOptions,
): PropertyDecorator => {
return function (object: Record<string, any>, propertyName: string | symbol) {
Expand Down

0 comments on commit 6b954f7

Please sign in to comment.