Skip to content

Commit

Permalink
fix: Prevent name collisions on express json validator
Browse files Browse the repository at this point in the history
  • Loading branch information
laurence79 committed Oct 9, 2024
1 parent 908e48f commit a04a6e2
Show file tree
Hide file tree
Showing 8 changed files with 71,936 additions and 48,656 deletions.
118,735 changes: 70,996 additions & 47,739 deletions example/generated/GitHubV3RestApiExpress.generated.ts

Large diffs are not rendered by default.

1,730 changes: 866 additions & 864 deletions example/generated/GitHubV3RestApiExpressDi.generated.ts

Large diffs are not rendered by default.

63 changes: 41 additions & 22 deletions example/generated/SwaggerPetstoreExpress.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from 'express-serve-static-core';
import { Router } from 'express';
import { ParsedQs } from 'qs';
import { Validator, ValidationError } from 'express-json-validator-middleware';
import * as ExpressJonValidator from 'express-json-validator-middleware';

export type Category = { readonly id?: number; readonly name?: string };

Expand Down Expand Up @@ -488,7 +488,10 @@ export type Handlers = {
createUser: CreateUserController;
};

const { validate } = new Validator({ strict: false, coerceTypes: true });
const { validate } = new ExpressJonValidator.Validator({
strict: false,
coerceTypes: true
});

export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {
const router = Router();
Expand Down Expand Up @@ -556,7 +559,11 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {
}

try {
await handler(req as AddPetRequest, res as AddPetResponse, next);
await handler(
(req as unknown) as AddPetRequest,
res as AddPetResponse,
next
);
} catch (e: unknown) {
next(e);
}
Expand Down Expand Up @@ -626,7 +633,11 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {
}

try {
await handler(req as UpdatePetRequest, res as UpdatePetResponse, next);
await handler(
(req as unknown) as UpdatePetRequest,
res as UpdatePetResponse,
next
);
} catch (e: unknown) {
next(e);
}
Expand Down Expand Up @@ -674,7 +685,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as FindPetsByStatusRequest,
(req as unknown) as FindPetsByStatusRequest,
res as FindPetsByStatusResponse,
next
);
Expand Down Expand Up @@ -716,7 +727,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as FindPetsByTagsRequest,
(req as unknown) as FindPetsByTagsRequest,
res as FindPetsByTagsResponse,
next
);
Expand Down Expand Up @@ -752,7 +763,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as GetPetByIdRequest,
(req as unknown) as GetPetByIdRequest,
res as GetPetByIdResponse,
next
);
Expand Down Expand Up @@ -788,7 +799,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as UpdatePetWithFormRequest,
(req as unknown) as UpdatePetWithFormRequest,
res as UpdatePetWithFormResponse,
next
);
Expand Down Expand Up @@ -823,7 +834,11 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {
}

try {
await handler(req as DeletePetRequest, res as DeletePetResponse, next);
await handler(
(req as unknown) as DeletePetRequest,
res as DeletePetResponse,
next
);
} catch (e: unknown) {
next(e);
}
Expand Down Expand Up @@ -867,7 +882,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as PlaceOrderRequest,
(req as unknown) as PlaceOrderRequest,
res as PlaceOrderResponse,
next
);
Expand Down Expand Up @@ -905,7 +920,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as GetOrderByIdRequest,
(req as unknown) as GetOrderByIdRequest,
res as GetOrderByIdResponse,
next
);
Expand Down Expand Up @@ -942,7 +957,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as DeleteOrderRequest,
(req as unknown) as DeleteOrderRequest,
res as DeleteOrderResponse,
next
);
Expand All @@ -964,7 +979,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as GetInventoryRequest,
(req as unknown) as GetInventoryRequest,
res as GetInventoryResponse,
next
);
Expand Down Expand Up @@ -1014,7 +1029,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as CreateUsersWithArrayInputRequest,
(req as unknown) as CreateUsersWithArrayInputRequest,
res as CreateUsersWithArrayInputResponse,
next
);
Expand Down Expand Up @@ -1064,7 +1079,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as CreateUsersWithListInputRequest,
(req as unknown) as CreateUsersWithListInputRequest,
res as CreateUsersWithListInputResponse,
next
);
Expand Down Expand Up @@ -1100,7 +1115,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as GetUserByNameRequest,
(req as unknown) as GetUserByNameRequest,
res as GetUserByNameResponse,
next
);
Expand Down Expand Up @@ -1160,7 +1175,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as UpdateUserRequest,
(req as unknown) as UpdateUserRequest,
res as UpdateUserResponse,
next
);
Expand Down Expand Up @@ -1195,7 +1210,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as DeleteUserRequest,
(req as unknown) as DeleteUserRequest,
res as DeleteUserResponse,
next
);
Expand Down Expand Up @@ -1230,7 +1245,11 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {
}

try {
await handler(req as LoginUserRequest, res as LoginUserResponse, next);
await handler(
(req as unknown) as LoginUserRequest,
res as LoginUserResponse,
next
);
} catch (e: unknown) {
next(e);
}
Expand All @@ -1249,7 +1268,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as LogoutUserRequest,
(req as unknown) as LogoutUserRequest,
res as LogoutUserResponse,
next
);
Expand Down Expand Up @@ -1298,7 +1317,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {

try {
await handler(
req as CreateUserRequest,
(req as unknown) as CreateUserRequest,
res as CreateUserResponse,
next
);
Expand All @@ -1316,7 +1335,7 @@ export const addHandlers = (app: Express, handlers: Partial<Handlers>) => {
response,
next
) => {
if (error instanceof ValidationError) {
if (error instanceof ExpressJonValidator.ValidationError) {
response.status(400).send({
type: 'REQUEST_VALIDATION_FAILED',
fields: error.validationErrors
Expand Down
Loading

0 comments on commit a04a6e2

Please sign in to comment.