import { Injectable } from "@nestjs/common";
import { registerDecorator, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";
import { UsersService } from "src/modules/users/users.service";

@ValidatorConstraint({ name: 'IsUserEmailUnique', async: true })
@Injectable()
export class IsNameRule implements ValidatorConstraintInterface {
    constructor(private readonly usersService: UsersService) {}

    async validate(payload: string): Promise<boolean> {
        return /^[a-zA-Z0-9 ]*$/.test( payload )
    }

    defaultMessage() {
        return `Nama hanya diijinkan huruf dan angka.`;
    }
}

export function IsName(validationOptions?: ValidationOptions) {
    return function (object: any, propertyName: string) {
        registerDecorator({
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            constraints: [],
            validator: IsNameRule,
        })
    }
}