1 files changed, 37 insertions, 0 deletions
diff --git a/src/dto/auth/AuthDto.js b/src/dto/auth/AuthDto.js
new file mode 100644
index 0000000..14e09ae
--- /dev/null
+++ b/src/dto/auth/AuthDto.js
@@ -0,0 +1,37 @@
+import { SafeNSoundError } from '#util/error.js';
+import Joi from 'joi';
+
+/**
+ * Generic authentication DTO.
+ */
+export class AuthDto {
+ static schema = new Joi.object({
+ username: Joi.string().required(),
+ email: Joi.string().email().required(),
+ password: Joi.string().required()
+ }).or('username', 'email');
+
+ username;
+ email;
+ password;
+
+ static async create(data) {
+ const obj = new AuthDto();
+ for (const key of Object.keys(data)) {
+ if (key in obj) {
+ obj[key] = data[key];
+ }
+ }
+
+ try {
+ return await AuthDto.schema.validateAsync(obj);
+ } catch (e) {
+ console.log(e);
+ throw new SafeNSoundError({
+ errCode: 'JOI_VALIDATION_ERROR',
+ message: e.message,
+ validation_details: e.details
+ });
+ }
+ }
+}
|