Skip to content

Commit

Permalink
feat: prevent runtime problems
Browse files Browse the repository at this point in the history
  • Loading branch information
3imed-jaberi committed Apr 16, 2023
1 parent cf7ac14 commit ee10822
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/body-parser.utils.ts
Expand Up @@ -43,10 +43,11 @@ export function getIsEnabledBodyAs(enableTypes: BodyType[]) {
export function getMimeTypes(
extendTypes: NonNullable<BodyParserOptions['extendTypes']>,
) {
for (const extendedType of Object.keys(extendTypes) as BodyType[]) {
for (const extendedType in extendTypes) {
if (
!supportedBodyTypes.includes(extendedType) ||
!Array.isArray(extendTypes[extendedType])
!supportedBodyTypes.includes(extendedType as BodyType) ||
!Array.isArray(extendTypes[extendedType as BodyType]) ||
!extendTypes[extendedType as BodyType]?.filter(Boolean).length
) {
throw new UnsupportedBodyTypeError(extendedType);
}
Expand Down
13 changes: 12 additions & 1 deletion test/middleware.test.ts
Expand Up @@ -383,7 +383,7 @@ describe("test/body-parser.test.ts", () => {
}
});

it("should throw when pass supported type but with string value instead of array", () => {
it("should throw when pass supported types with string value instead of array", () => {
try {
createApp({
extendTypes: {
Expand All @@ -394,6 +394,17 @@ describe("test/body-parser.test.ts", () => {
expect(error instanceof UnsupportedBodyTypeError).toBe(true);
}
});
it("should throw when pass supported types with array contain falsy values", () => {
try {
createApp({
extendTypes: {
json: ["", 0, false, null, undefined],
} as any,
});
} catch (error) {
expect(error instanceof UnsupportedBodyTypeError).toBe(true);
}
});
});

describe("enableTypes", () => {
Expand Down

0 comments on commit ee10822

Please sign in to comment.