dryvPacks

@dryv/class-validatorv0.7.0

class-validator DTO classes with field limits, formats and invariants.

class-validator DTOsValidationInjectPublished 7 days ago2 downloads

dryv pack add @dryv/class-validator
719 BRaw ↗
typescript
import { registerDecorator, type ValidationOptions } from "class-validator";

/**
 * Validates a rule over the whole object, reported on the decorated property.
 * Generated for schema invariants, such as "confirmPassword must equal password".
 */
export function Satisfies<T extends object>(
  rule: (object: T) => boolean,
  options?: ValidationOptions,
): PropertyDecorator {
  return (target, propertyName) => {
    registerDecorator({
      name: "satisfies",
      target: target.constructor,
      propertyName: String(propertyName),
      ...(options === undefined ? {} : { options }),
      validator: {
        validate: (_value, args) => args !== undefined && rule(args.object as T),
      },
    });
  };
}