summary refs log tree commit diff
path: root/src/dto
diff options
context:
space:
mode:
Diffstat (limited to 'src/dto')
-rw-r--r--src/dto/auth/LoginDto.js31
-rw-r--r--src/dto/auth/RegisterDto.js36
-rw-r--r--src/dto/auth/index.js2
-rw-r--r--src/dto/index.js1
4 files changed, 70 insertions, 0 deletions
diff --git a/src/dto/auth/LoginDto.js b/src/dto/auth/LoginDto.js
new file mode 100644

index 0000000..b675b4d --- /dev/null +++ b/src/dto/auth/LoginDto.js
@@ -0,0 +1,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 + }); + } + } +} diff --git a/src/dto/auth/RegisterDto.js b/src/dto/auth/RegisterDto.js new file mode 100644
index 0000000..40f1959 --- /dev/null +++ b/src/dto/auth/RegisterDto.js
@@ -0,0 +1,36 @@ +import { SafeNSoundError } from '#util/error.js'; +import Joi from 'joi'; + +export class RegisterDto { + static schema = new Joi.object({ + username: Joi.string().required(), + email: Joi.string().email().required(), + password: Joi.string().required(), + type: Joi.string().valid('user', 'monitor', 'admin').required() + }); + + username; + email; + password; + type = 'user'; + + static async create(data) { + const obj = new RegisterDto(); + for (const key of Object.keys(data)) { + if (key in obj) { + obj[key] = data[key]; + } + } + + try { + return await RegisterDto.schema.validateAsync(obj); + } catch (e) { + console.log(e); + throw new SafeNSoundError({ + errCode: 'JOI_VALIDATION_ERROR', + message: e.message, + validation_details: e.details + }); + } + } +} diff --git a/src/dto/auth/index.js b/src/dto/auth/index.js new file mode 100644
index 0000000..6d57d5e --- /dev/null +++ b/src/dto/auth/index.js
@@ -0,0 +1,2 @@ +export * from './LoginDto.js'; +export * from './RegisterDto.js'; diff --git a/src/dto/index.js b/src/dto/index.js new file mode 100644
index 0000000..fd4a05a --- /dev/null +++ b/src/dto/index.js
@@ -0,0 +1 @@ +export * from './auth/index.js';