Я пока перечислю - умру.
Надо научиться писать changelog постепенно.
This commit is contained in:
2024-10-19 02:12:37 +04:00
parent ad730e0943
commit 5fe5d56ca9
83 changed files with 3796 additions and 1502 deletions

View File

@@ -0,0 +1,39 @@
import {
registerDecorator,
ValidationArguments,
ValidationOptions,
} from "class-validator";
// noinspection FunctionNamingConventionJS
export function NullIf(
canBeNull: (cls: object) => boolean,
validationOptions?: ValidationOptions,
) {
return function (object: object, propertyName: string) {
registerDecorator({
name: "nullIf",
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [canBeNull],
validator: {
validate(value: any, args: ValidationArguments) {
const canBeNullFunc: (cls: object) => boolean =
args.constraints[0];
const canBeNull = canBeNullFunc(args.object);
const currentValue = value;
// Логика валидации: если одно из полей null, то другое тоже должно быть null
return canBeNull
? currentValue !== null
: currentValue === null;
},
defaultMessage(args: ValidationArguments) {
return `${args.property} must be ${args.property === null ? "non-null" : "null"}!`;
},
},
});
};
}