import { registerDecorator, ValidationOptions } from 'class-validator';
import {
  ExistsInDatabaseConstraintOptions,
  ExistsInDatabaseValidator,
} from '../exists-in-database.validator';

export function IsExistInDatabase(
  entity: any,
  field: string,
  options: ExistsInDatabaseConstraintOptions = {},
  validationOptions?: ValidationOptions,
) {
  return function (object: Object, propertyName: string) {
    registerDecorator({
      name: 'IsExistInDatabase',
      target: object.constructor,
      propertyName,
      constraints: [entity, field, options, validationOptions],
      options: validationOptions,
      validator: ExistsInDatabaseValidator,
    });
  };
}
