Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

45 řádky
1.1 KiB

  1. const Joi = require('joi');
  2. const constants = require('../constants');
  3. const { NAME_MIN, NAME_MAX } = constants;
  4. const schema = Joi.object().keys({
  5. name: Joi.string()
  6. .min(NAME_MIN)
  7. .max(NAME_MAX)
  8. .required(),
  9. username: Joi.string().email({ minDomainAtoms: 2 }),
  10. });
  11. async function validateRegisterPayload(req, res, next) {
  12. let payloadValidation;
  13. try {
  14. payloadValidation = await Joi.validate(req.body, schema, { abortEarly: false });
  15. } catch (validateRegisterError) {
  16. payloadValidation = validateRegisterError;
  17. }
  18. const { details } = payloadValidation;
  19. let errors;
  20. if (details) {
  21. errors = {};
  22. details.forEach(errorDetail => {
  23. const {
  24. message,
  25. path: [key],
  26. type,
  27. } = errorDetail;
  28. const errorType = type.split('.')[1];
  29. errors[key] = constants[`${key.toUpperCase()}_${errorType.toUpperCase()}_ERROR`] || message;
  30. });
  31. }
  32. if (errors) {
  33. req.session.messages = { errors };
  34. return res.status(400).redirect('/profile');
  35. }
  36. return next();
  37. }
  38. module.exports = validateRegisterPayload;