const Joi = require('joi');
const { ValidationError } = require('../utils/errors');

const schemas = {
  '/users': Joi.object({
    email: Joi.string().email().required(),
    password: Joi.string().min(8).required(),
    name: Joi.string().required()
  })
};

const validateRequest = (req, res, next) => {
  const schema = schemas[req.path];
  if (schema) {
    const { error } = schema.validate(req.body);
    if (error) {
      throw new ValidationError(error.details[0].message);
    }
  }
  next();
};