generated from TritonSE/onboarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.ts
56 lines (52 loc) · 1.87 KB
/
task.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { body } from "express-validator";
// more info about validators:
// https://express-validator.github.io/docs/guides/validation-chain
// https://github.com/validatorjs/validator.js#validators
const makeIDValidator = () =>
body("_id")
.exists()
.withMessage("_id is required")
.bail()
.isMongoId()
.withMessage("_id must be a MongoDB object ID");
const makeTitleValidator = () =>
body("title")
// title must exist, if not this message will be displayed
.exists()
.withMessage("title is required")
// bail prevents the remainder of the validation chain for this field from being executed if
// there was an error
.bail()
.isString()
.withMessage("title must be a string")
.bail()
.notEmpty()
.withMessage("title cannot be empty");
const makeDescriptionValidator = () =>
body("description")
// order matters for the validation chain - by marking this field as optional, the rest of
// the chain will only be evaluated if it exists
.optional()
.isString()
.withMessage("description must be a string");
const makeIsCheckedValidator = () =>
body("isChecked").optional().isBoolean().withMessage("isChecked must be a boolean");
const makeDateCreatedValidator = () =>
body("dateCreated").isISO8601().withMessage("dateCreated must be a valid date-time string");
// assignee is for Part 2.1
const makeAssigneeValidator = () =>
body("assignee").optional().isMongoId().withMessage("assignee must be a MongoDB object ID");
// establishes a set of rules that the body of the task creation route must follow
export const createTask = [
makeTitleValidator(),
makeDescriptionValidator(),
makeIsCheckedValidator(),
];
export const updateTask = [
makeIDValidator(),
makeTitleValidator(),
makeDescriptionValidator(),
makeIsCheckedValidator(),
makeDateCreatedValidator(),
makeAssigneeValidator(), // for Part 2.1
];