Skip to content

Commit d9f5be4

Browse files
authored
Merge pull request #1465 from CyberL1/fix/name-must-not-be-empty
2 parents 23f6754 + e2df7c8 commit d9f5be4

6 files changed

Lines changed: 35 additions & 9 deletions

File tree

assets/openapi.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,6 +3997,9 @@
39973997
},
39983998
"username": {
39993999
"type": "string"
4000+
},
4001+
"banner": {
4002+
"type": "string"
40004003
}
40014004
}
40024005
},

assets/schemas.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4234,6 +4234,9 @@
42344234
},
42354235
"username": {
42364236
"type": "string"
4237+
},
4238+
"banner": {
4239+
"type": "string"
42374240
}
42384241
},
42394242
"additionalProperties": false,

src/api/routes/applications/#application_id/bot/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { route } from "@spacebar/api";
20-
import { Application, DiscordApiErrors, User, createAppBotUser, generateToken, handleFile } from "@spacebar/util";
20+
import { Application, DiscordApiErrors, FieldErrors, User, createAppBotUser, generateToken, handleFile } from "@spacebar/util";
2121
import { Request, Response, Router } from "express";
2222
import { HTTPError } from "lambert-server";
2323
import { verifyToken } from "node-2fa";
@@ -100,6 +100,15 @@ router.patch(
100100
const body = req.body as BotModifySchema;
101101
if (!body.avatar?.trim()) delete body.avatar;
102102

103+
if (body.username?.trim() == "") {
104+
throw FieldErrors({
105+
username: {
106+
code: "BASE_TYPE_REQUIRED",
107+
message: req.t("common:field.BASE_TYPE_REQUIRED"),
108+
},
109+
});
110+
}
111+
103112
const app = await Application.findOneOrFail({
104113
where: { id: req.params.application_id },
105114
relations: { bot: true, owner: true },

src/api/routes/applications/#application_id/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { route } from "@spacebar/api";
20-
import { Application, DiscordApiErrors, Guild, handleFile, User } from "@spacebar/util";
20+
import { Application, DiscordApiErrors, FieldErrors, Guild, handleFile, User } from "@spacebar/util";
2121
import { Request, Response, Router } from "express";
2222
import { HTTPError } from "lambert-server";
2323
import { verifyToken } from "node-2fa";
@@ -73,6 +73,15 @@ router.patch(
7373

7474
if (app.owner.totp_secret && (!req.body.code || verifyToken(app.owner.totp_secret, req.body.code))) throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008);
7575

76+
if (body.name?.trim() == "") {
77+
throw FieldErrors({
78+
name: {
79+
code: "BASE_TYPE_REQUIRED",
80+
message: req.t("common:field.BASE_TYPE_REQUIRED"),
81+
},
82+
});
83+
}
84+
7685
if (body.icon) {
7786
body.icon = await handleFile(`/app-icons/${app.id}`, body.icon as string);
7887
}

src/schemas/uncategorised/BotModifySchema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
export interface BotModifySchema {
2020
avatar?: string;
2121
username?: string;
22+
banner?: string;
2223
}

src/util/util/FieldError.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ export type ErrorContent = { code: string; message: string };
2929
export type ObjectErrorContent = { _errors: ErrorContent[] };
3030

3131
export function FieldErrors(fields: Record<string, { code?: string; message: string }>, errors?: ErrorObject[]) {
32-
return new FieldError(
33-
50035,
34-
"Invalid Form Body",
35-
Object.values(fields).map(({ message, code }) => ({
32+
const errorsObject = {} as Record<string, ObjectErrorContent>;
33+
34+
Object.entries(fields).map(([key, { message, code }]) => {
35+
errorsObject[key] = {
3636
_errors: [
3737
{
3838
message,
3939
code: code || "BASE_TYPE_INVALID",
4040
},
4141
],
42-
})),
43-
errors,
44-
);
42+
};
43+
});
44+
45+
return new FieldError(50035, "Invalid Form Body", errorsObject, errors);
4546
}
4647

4748
// TODO: implement Image data type: Data URI scheme that supports JPG, GIF, and PNG formats. An example Data URI format is: data:image/jpeg;base64,BASE64_ENCODED_JPEG_IMAGE_DATA

0 commit comments

Comments
 (0)