Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com> Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com> Co-authored-by: Sebastian Rindom <seb@medusa-commerce.com>
27 lines
808 B
TypeScript
27 lines
808 B
TypeScript
import {
|
|
registerDecorator,
|
|
ValidationArguments,
|
|
ValidationOptions,
|
|
} from "class-validator"
|
|
|
|
export function IsISO8601Duration(validationOptions?: ValidationOptions) {
|
|
return function (object: any, propertyName: string): void {
|
|
registerDecorator({
|
|
name: "IsGreaterThan",
|
|
target: object.constructor,
|
|
propertyName: propertyName,
|
|
options: validationOptions,
|
|
validator: {
|
|
validate(value: any, args: ValidationArguments) {
|
|
const isoDurationRegex =
|
|
/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/
|
|
return isoDurationRegex.test(value)
|
|
},
|
|
defaultMessage(args?: ValidationArguments): string {
|
|
return `"${propertyName}" must be a valid ISO 8601 duration`
|
|
},
|
|
},
|
|
})
|
|
}
|
|
}
|