import { registerDecorator, ValidationOptions } from 'class-validator';
import {
  UniqueInDatabaseVarlidator,
  UniqueInDatabaseConstraintOptions,
} from '../unique-in-database.decorator';

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