summary refs log tree commit diff
path: root/src/dto/auth/LoginDto.js
blob: b675b4d07819d4e785575c0412d11596caffca93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { SafeNSoundError } from '#util/error.js';
import Joi from 'joi';

export class LoginDto {
    static schema = new Joi.object({
        username: Joi.string().required(),
        email: Joi.string().email().required(),
        password: Joi.string().required()
    }).or('username', 'email');

    username;
    password;

    async Create({ username, email, password }) {
        this.username = username ?? email;
        this.password = password;

        try {
            return await LoginDto.schema.validateAsync(this, {
                abortEarly: true
            });
        } catch (e) {
            console.log(e);
            throw new SafeNSoundError({
                errCode: 'JOI_VALIDATION_ERROR',
                message: e.message,
                validation_details: e.details
            });
        }
    }
}