Created
October 9, 2021 09:05
-
-
Save Houserqu/643369d1377f1ec35295ba86640762d6 to your computer and use it in GitHub Desktop.
class-validator
This file contains hidden or 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
/** | |
* 相对时间校验 | |
* @param property [数量,时间单位],时间单位参考 dayjs,例如 1 年前,[1, 'year'] | |
* @param validationOptions | |
* @returns | |
*/ | |
const IsRelativeDate = function (property: [number, string], validationOptions?: ValidationOptions) { | |
return function (object: Object, propertyName: string) { | |
registerDecorator({ | |
name: 'IsRelativeDate', | |
target: object.constructor, | |
propertyName, | |
constraints: [property], | |
options: validationOptions, | |
validator: { | |
validate(value: any, args: ValidationArguments) { | |
const [rule] = args.constraints; | |
const minStartTime = dayjs().subtract(rule[0], rule[1]) | |
.valueOf(); | |
return value * 1 > minStartTime; | |
}, | |
}, | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment