Skip to content

Instantly share code, notes, and snippets.

@Houserqu
Created October 9, 2021 09:05
Show Gist options
  • Save Houserqu/643369d1377f1ec35295ba86640762d6 to your computer and use it in GitHub Desktop.
Save Houserqu/643369d1377f1ec35295ba86640762d6 to your computer and use it in GitHub Desktop.
class-validator
/**
* 相对时间校验
* @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