Skip to content

Commit

Permalink
refactoring validations
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmarinho committed Mar 29, 2020
1 parent db5972b commit 4c6c64d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 71 deletions.
59 changes: 59 additions & 0 deletions backend/src/controllers/middlewares/validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { celebrate, Segments, Joi } = require("celebrate");

module.exports = {
sessions: celebrate({
[Segments.BODY]: Joi.object().keys({
id: Joi.string().required()
})
}),

createOngs: celebrate({
[Segments.BODY]: Joi.object().keys({
name: Joi.string().required(),
email: Joi.string()
.required()
.email(),
whatsapp: Joi.string()
.required()
.min(10)
.max(11),
city: Joi.string().required(),
uf: Joi.string()
.required()
.length(2)
})
}),

profile: celebrate({
[Segments.HEADERS]: Joi.object({
authorization: Joi.string().required()
}).unknown()
}),

createIncident: celebrate({
[Segments.HEADERS]: Joi.object({
authorization: Joi.string().required()
}).unknown(),
[Segments.BODY]: Joi.object().keys({
title: Joi.string()
.required()
.min(3),
description: Joi.string()
.required()
.min(3),
value: Joi.number().required()
})
}),

indexIncidents: celebrate({
[Segments.QUERY]: Joi.object().keys({
page: Joi.number()
})
}),

deleteIncident: celebrate({
[Segments.PARAMS]: Joi.object().keys({
id: Joi.number().required()
})
})
};
Binary file modified backend/src/database/db.sqlite
Binary file not shown.
78 changes: 7 additions & 71 deletions backend/src/routes.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,24 @@
const express = require("express");
const { celebrate, Segments, Joi } = require("celebrate");
const OngController = require("./controllers/OngController");
const IncidentController = require("./controllers/IncidentController");
const ProfileController = require("./controllers/ProfileController");
const SessionController = require("./controllers/SessionController");
const Validation = require("./controllers/middlewares/validations");

const routes = express.Router();

routes.post(
"/sessions",
celebrate({
[Segments.BODY]: Joi.object().keys({
id: Joi.string().required()
})
}),
SessionController.create
);
routes.post("/sessions", Validation.sessions, SessionController.create);

routes.post(
"/ongs",
celebrate({
[Segments.BODY]: Joi.object().keys({
name: Joi.string().required(),
email: Joi.string()
.required()
.email(),
whatsapp: Joi.string()
.required()
.min(10)
.max(11),
city: Joi.string().required(),
uf: Joi.string()
.required()
.length(2)
})
}),
OngController.create
);
routes.post("/ongs", Validation.createOngs, OngController.create);
routes.get("/ongs", OngController.index);

routes.get(
"/profile",
celebrate({
[Segments.HEADERS]: Joi.object({
authorization: Joi.string().required()
}).unknown()
}),
ProfileController.index
);
routes.get("/profile", Validation.profile, ProfileController.index);

routes.post(
"/incidents",
celebrate({
[Segments.HEADERS]: Joi.object({
authorization: Joi.string().required()
}).unknown(),
[Segments.BODY]: Joi.object().keys({
title: Joi.string()
.required()
.min(3),
description: Joi.string()
.required()
.min(3),
value: Joi.number().required()
})
}),
IncidentController.create
);
routes.get(
"/incidents",
celebrate({
[Segments.QUERY]: Joi.object().keys({
page: Joi.number()
})
}),
IncidentController.index
);
routes.post("/incidents", Validation.createIncident, IncidentController.create);
routes.get("/incidents", Validation.indexIncidents, IncidentController.index);
routes.delete(
"/incidents/:id",
celebrate({
[Segments.PARAMS]: Joi.object().keys({
id: Joi.number().required()
})
}),
Validation.deleteIncident,
IncidentController.delete
);

Expand Down

0 comments on commit 4c6c64d

Please sign in to comment.