class-validate always return length = 0 when I try use it in service.
service
export class UserService extends BaseService<UserEntity> {
private validator
constructor (
@Inject('USER_RESPOSITORY')
private userRespository: typeof UserEntity
) {
super(userRespository)
this.validator = new Validator()
}
async create (user: CreateUserDto): Promise<UserEntity> {
const error = await this.validator.validate(user)
if (error.length > 0) {
throw Error('Invalid criterial!')
}
const hashedPassword = await bcrypt.hash(user.password, 10);
user.password = hashedPassword
const rs = await super.create(user)
return rs
}
}
controller:
@Post()
async create (@Body() user) {
return this.userService.create(user)
}
dto:
export class CreateUserDto {
@IsEmail()
email: string
@Length(8)
password: string
@IsOptional()
isSuperAdmin: number
}
value input: { "email": "nhathanluu", "password": "nhathan12" }
class-validate.validate() return empty array. It fine when I validate in controller