user.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const Joi = require('joi');
  2. const validate = require('./validate');
  3. const schemaCreateNewUser = Joi.object({
  4. number: Joi.string().min(8).max(14).required(),
  5. country: Joi.string().min(1).max(40).required(),
  6. });
  7. const schemaUpdateCredentials = Joi.object({
  8. name: Joi.string().min(1).max(30).trim().required(),
  9. lastName: Joi.string().min(1).max(30).trim().required(),
  10. originalName: Joi.string().min(1).max(30).trim().required(),
  11. originalLastName: Joi.string().min(1).max(30).trim().required(),
  12. });
  13. const schemaUpdateUser = Joi.object({
  14. nightMode: Joi.boolean().optional(),
  15. sort: Joi.boolean().optional(),
  16. }).min(1);
  17. const schemaLogIn = Joi.object({
  18. number: Joi.string().min(8).max(14).required(),
  19. code: Joi.string().min(3).max(12).required(),
  20. });
  21. module.exports.registration = (req, _res, next) => {
  22. return validate(schemaCreateNewUser, req.body, next);
  23. };
  24. module.exports.updateCredentials = (req, _res, next) => {
  25. return validate(schemaUpdateCredentials, req.body, next);
  26. };
  27. module.exports.updateUser = (req, _res, next) => {
  28. return validate(schemaUpdateUser, req.body, next);
  29. };
  30. module.exports.logIn = (req, _res, next) => {
  31. return validate(schemaLogIn, req.body, next);
  32. };
  33. module.exports.validateUploadFile = (req, res, next) => {
  34. if (!req.file)
  35. return res.status(400).json({
  36. status: 'error',
  37. code: 400,
  38. data: 'Bad request',
  39. message: 'File not found',
  40. });
  41. next();
  42. };