diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 274bdb5..b3e0114 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -15,6 +15,7 @@ const login = async (req, res, next) => { const create = async (req, res, next) => { const { displayName, email, password, image } = req.body; const response = await userService.create({ displayName, email, password, image }); + if (response.error) return next(response.error); if (response.status === 409) { const err = new Error(response.message); err.status = response.status; diff --git a/src/migrations/20230914143828-create-users.js b/src/migrations/20230914143828-create-users.js index f5aa41e..15c1aa0 100644 --- a/src/migrations/20230914143828-create-users.js +++ b/src/migrations/20230914143828-create-users.js @@ -22,7 +22,7 @@ module.exports = { type: Sequelize.STRING, }, image: { - allowNull: false, + allowNull: true, type: Sequelize.STRING, }, }); diff --git a/src/models/User.js b/src/models/User.js index 2573d74..eec9de2 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -23,7 +23,7 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.STRING, }, image: { - allowNull: false, + allowNull: true, type: DataTypes.STRING, }, }, diff --git a/src/services/userService.js b/src/services/userService.js index aae6c7d..e38eab9 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -1,6 +1,6 @@ const jwt = require('jsonwebtoken'); const { User } = require('../models'); -const { loginValidation } = require('./validations'); +const { loginValidation, createUserValidation } = require('./validations'); const { JWT_SECRET } = process.env; const jwtConfig = { @@ -34,6 +34,8 @@ const login = async ({ email, password }) => { }; const create = async ({ displayName, email, password, image }) => { + const error = createUserValidation({ displayName, email, password }); + if (error) return { error }; const userExists = await User.findOne({ where: { email } }); if (userExists) { return { diff --git a/src/services/validations/createUserValidation.js b/src/services/validations/createUserValidation.js new file mode 100644 index 0000000..763f96c --- /dev/null +++ b/src/services/validations/createUserValidation.js @@ -0,0 +1,8 @@ +const { createUserSchema } = require('../../utils/schemas'); + +const validateCreateUser = (body) => { + const { error } = createUserSchema.validate(body); + return error; +} + +module.exports = validateCreateUser; diff --git a/src/services/validations/index.js b/src/services/validations/index.js index 9195bce..756219b 100644 --- a/src/services/validations/index.js +++ b/src/services/validations/index.js @@ -1,6 +1,7 @@ const loginValidation = require('./loginValidation'); - +const createUserValidation = require('./createUserValidation'); module.exports = { loginValidation, + createUserValidation, }; \ No newline at end of file diff --git a/src/utils/schemas.js b/src/utils/schemas.js index 20cdfc4..e8cd7e4 100644 --- a/src/utils/schemas.js +++ b/src/utils/schemas.js @@ -12,6 +12,30 @@ const loginSchema = Joi.object({ }), }); +const createUserSchema = Joi.object({ + displayName: Joi.string().min(8).required() + .messages({ + 'string.empty': 'Some required fields are missing', + 'any.min': 'displayName must be at least 8 characters long', + 'any.required': 'Some required fields are missing', + }), + email: Joi.string().regex(/^[a-z0-9.]+@[a-z0-9]+\.[a-z]+(\.[a-z]+)?$/i).required() + .messages({ + 'string.empty': 'Some required fields are missing', + 'string.pattern.base': '"email" must be a valid email', + 'any.required': 'Some required fields are missing', + }), + password: Joi.string().min(6).required() + .messages({ + 'string.empty': 'Some required fields are missing', + 'any.min': 'password must be at least 6 characters long', + 'any.required': 'Some required fields are missing', + }), + image: Joi.string() + +}); + module.exports = { loginSchema, + createUserSchema, };