# All files / nodejs-homework-API/routes/api/validation validationUser.js

41.18% Statements 7/17
0% Branches 0/4
0% Functions 0/4
41.18% Lines 7/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    421x

    1x

    1x

    1x

    1x

    1x

    1x

     const Joi = require("joi");

    const schemaCreateNewUser = Joi.object({
      email: Joi.string().email().min(3).max(30).required(),
      password: Joi.string().required(),
    }).min(2);

    const schemaLogIn = Joi.object({
      email: Joi.string().email().min(3).max(30).required(),
      password: Joi.string().required(),
    }).min(2);

    const validate = (schema, obj, next) => {
      const { error } = schema.validate(obj);
      if (error) {
        const [{ message }] = error.details;
        return next({
          status: 400,
          message: `Filed: ${message.replace(/"/g, "")}`,
        });
      }
      next();
    };

    module.exports.registration = (req, _res, next) => {
      return validate(schemaCreateNewUser, req.body, next);
    };
    module.exports.logIn = (req, _res, next) => {
      return validate(schemaLogIn, req.body, next);
    };
    module.exports.validateUploadAvatar = (req, res, next) => {
      if (!req.file) {
        return res.status(400).json({
          status: "error",
          code: 400,
          data: "Bad request",
          message: "Field of avatar with file not found",
        });
      }
      next();
    };