user.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. silentMode: Joi.boolean().optional(),
  16. sort: Joi.boolean().optional(),
  17. }).min(1);
  18. const schemaLogIn = Joi.object({
  19. number: Joi.string().min(8).max(14).required(),
  20. code: Joi.string().min(3).max(12).required(),
  21. });
  22. module.exports.registration = (req, _res, next) => {
  23. return validate(schemaCreateNewUser, req.body, next);
  24. };
  25. module.exports.updateCredentials = (req, _res, next) => {
  26. return validate(schemaUpdateCredentials, req.body, next);
  27. };
  28. module.exports.updateUser = (req, _res, next) => {
  29. return validate(schemaUpdateUser, req.body, next);
  30. };
  31. module.exports.logIn = (req, _res, next) => {
  32. return validate(schemaLogIn, req.body, next);
  33. };
  34. module.exports.validateUploadFile = (req, res, next) => {
  35. if (!req.file)
  36. return res.status(400).json({
  37. status: 'error',
  38. code: 400,
  39. data: 'Bad request',
  40. message: 'File not found',
  41. });
  42. next();
  43. };