Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 6d6453b

Browse files
✨ Consistent validations
1 parent 0a2c38e commit 6d6453b

File tree

5 files changed

+53
-45
lines changed

5 files changed

+53
-45
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "staart-manager",
3-
"version": "1.0.53",
3+
"version": "1.0.54",
44
"main": "index.js",
55
"repository": "git@github.com:AnandChowdhary/staart.git",
66
"author": "Anand Chowdhary <mail@anandchowdhary.com>",

src/controllers/auth.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,12 @@ export class AuthController {
205205

206206
@Post("impersonate/:id")
207207
@Middleware(authHandler)
208+
@Middleware(
209+
validator({ impersonateUserId: Joi.number().required() }, "params")
210+
)
208211
async getImpersonate(req: Request, res: Response) {
209212
const tokenUserId = res.locals.token.id;
210213
const impersonateUserId = req.params.id;
211-
joiValidate(
212-
{
213-
tokenUserId: Joi.number().required(),
214-
impersonateUserId: Joi.number().required()
215-
},
216-
{
217-
tokenUserId,
218-
impersonateUserId
219-
}
220-
);
221214
res.json(await impersonate(tokenUserId, impersonateUserId));
222215
}
223216

src/controllers/membership.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import {
1313
Delete,
1414
Controller,
1515
ClassMiddleware,
16-
ClassWrapper
16+
ClassWrapper,
17+
Middleware
1718
} from "@overnightjs/core";
18-
import { authHandler } from "../helpers/middleware";
19+
import { authHandler, validator } from "../helpers/middleware";
1920
import asyncHandler from "express-async-handler";
2021
import Joi from "@hapi/joi";
2122
import { joiValidate } from "../helpers/utils";
@@ -26,45 +27,27 @@ import i18n from "../i18n";
2627
@ClassMiddleware(authHandler)
2728
export class MembershipController {
2829
@Get(":id")
30+
@Middleware(validator({ id: Joi.number().required() }, "params"))
2931
async get(req: Request, res: Response) {
3032
const membershipId = req.params.id;
3133
const userId = res.locals.token.id;
32-
joiValidate(
33-
{
34-
membershipId: Joi.number().required(),
35-
userId: Joi.number().required()
36-
},
37-
{ membershipId, userId }
38-
);
3934
res.json(await getMembershipDetailsForUser(userId, membershipId));
4035
}
4136

4237
@Delete(":id")
38+
@Middleware(validator({ id: Joi.number().required() }, "params"))
4339
async delete(req: Request, res: Response) {
4440
const userId = res.locals.token.id;
4541
const membershipId = req.params.id;
46-
joiValidate(
47-
{
48-
membershipId: Joi.number().required(),
49-
userId: Joi.number().required()
50-
},
51-
{ membershipId, userId }
52-
);
5342
await deleteMembershipForUser(userId, membershipId, res.locals);
5443
res.json({ deleted: true });
5544
}
5645

5746
@Patch(":id")
47+
@Middleware(validator({ id: Joi.number().required() }, "params"))
5848
async patch(req: Request, res: Response) {
5949
const userId = res.locals.token.id;
6050
const membershipId = req.params.id;
61-
joiValidate(
62-
{
63-
membershipId: Joi.number().required(),
64-
userId: Joi.number().required()
65-
},
66-
{ membershipId, userId }
67-
);
6851
const data = req.body;
6952
delete req.body.id;
7053
await updateMembershipForUser(userId, membershipId, data, res.locals);

src/controllers/user.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import {
2323
Delete,
2424
Controller,
2525
ClassMiddleware,
26-
ClassWrapper
26+
ClassWrapper,
27+
Middleware
2728
} from "@overnightjs/core";
28-
import { authHandler } from "../helpers/middleware";
29+
import { authHandler, validator } from "../helpers/middleware";
2930
import {
3031
getAllEmailsForUser,
3132
addEmailToUserForUser,
@@ -54,6 +55,30 @@ export class UserController {
5455
}
5556

5657
@Patch(":id")
58+
@Middleware(
59+
validator(
60+
{
61+
name: Joi.string()
62+
.min(3)
63+
.regex(/^[a-zA-Z ]*$/),
64+
username: Joi.string(),
65+
nickname: Joi.string(),
66+
primaryEmail: Joi.number(),
67+
countryCode: Joi.string().length(2),
68+
password: Joi.string().min(6),
69+
gender: Joi.string().length(1),
70+
preferredLanguage: Joi.string()
71+
.min(2)
72+
.max(5),
73+
timezone: Joi.string(),
74+
notificationEmails: Joi.number(),
75+
prefersReducedMotion: Joi.boolean(),
76+
prefersColorSchemeDark: Joi.boolean(),
77+
profilePicture: Joi.string()
78+
},
79+
"body"
80+
)
81+
)
5782
async patch(req: Request, res: Response) {
5883
let id = req.params.id;
5984
if (id === "me") id = res.locals.token.id;
@@ -77,22 +102,29 @@ export class UserController {
77102
}
78103

79104
@Put(":id/password")
80-
async updatePassword(req: Request, res: Response) {
81-
let id = req.params.id;
82-
if (id === "me") id = res.locals.token.id;
83-
const oldPassword = req.body.oldPassword;
84-
const newPassword = req.body.newPassword;
85-
joiValidate(
105+
@Middleware(
106+
validator(
86107
{
87-
id: [Joi.string().required(), Joi.number().required()],
88108
oldPassword: Joi.string()
89109
.min(6)
90110
.required(),
91111
newPassword: Joi.string()
92112
.min(6)
93113
.required()
94114
},
95-
{ id, oldPassword, newPassword }
115+
"body"
116+
)
117+
)
118+
async updatePassword(req: Request, res: Response) {
119+
let id = req.params.id;
120+
if (id === "me") id = res.locals.token.id;
121+
const oldPassword = req.body.oldPassword;
122+
const newPassword = req.body.newPassword;
123+
joiValidate(
124+
{
125+
id: [Joi.string().required(), Joi.number().required()]
126+
},
127+
{ id }
96128
);
97129
await updatePasswordForUser(
98130
res.locals.token.id,

src/internal/staart-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.53
1+
1.0.54

0 commit comments

Comments
 (0)