Skip to content

Commit

Permalink
add possibility to ignore validation for some response status in axio…
Browse files Browse the repository at this point in the history
…s and fetch adapters
  • Loading branch information
JeromeBu committed Oct 3, 2024
1 parent 6c2c606 commit 1691d0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
22 changes: 12 additions & 10 deletions src/axios/createAxiosSharedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ export const createAxiosHandlerCreator =
},
});

const responseBody = options?.skipResponseValidation
? data
: validateSchemaWithExplicitError({
adapterName: "axios",
checkedSchema: "responses",
responseStatus: status as any,
params: data,
route,
withIssuesInMessage: true,
});
const responseBody =
options?.skipResponseValidation ||
options?.skipResponseValidationForStatuses?.includes(status)
? data
: validateSchemaWithExplicitError({
adapterName: "axios",
checkedSchema: "responses",
responseStatus: status as any,
params: data,
route,
withIssuesInMessage: true,
});

return { ...rest, status, body: responseBody };
};
Expand Down
22 changes: 12 additions & 10 deletions src/fetch/createFetchSharedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ export const createFetchHandlerCreator =

const processedBody = await responseTypeToResponseBody(res, route.responseType);

const responseBody = options.skipResponseValidation
? processedBody
: validateSchemaWithExplicitError({
adapterName: "fetch",
checkedSchema: "responses",
responseStatus: res.status as any,
params: processedBody,
route,
withIssuesInMessage: true,
});
const responseBody =
options?.skipResponseValidation ||
options?.skipResponseValidationForStatuses?.includes(res.status)
? processedBody
: validateSchemaWithExplicitError({
adapterName: "fetch",
checkedSchema: "responses",
responseStatus: res.status as any,
params: processedBody,
route,
withIssuesInMessage: true,
});

return {
body: responseBody,
Expand Down
2 changes: 2 additions & 0 deletions src/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type ValidationOptions = {
skipInputValidation?: boolean;
/* if true, will not validate response body */
skipResponseValidation?: boolean;
/* list of response status codes for which validation will be skipped */
skipResponseValidationForStatuses?: number[];
};

type ExtractFromExisting<T, U extends T> = Extract<T, U>;
Expand Down
30 changes: 30 additions & 0 deletions test/createAxiosAndFetchSharedClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe("createAxiosSharedCaller", () => {
].join("\n"),
);
},
{ timeout: 3_000 },
);

it.each([
Expand All @@ -188,6 +189,35 @@ describe("createAxiosSharedCaller", () => {
},
);

const skippedStatuses = [200];

it.each([
{
name: "axios",
httpClient: createAxiosSharedClient(routes, axios, {
skipResponseValidationForStatuses: skippedStatuses,
}),
},
{
name: "fetch",
httpClient: createFetchSharedClient(routes, fetch, {
skipResponseValidationForStatuses: skippedStatuses,
}),
},
])(
"can skip the response validation for some selected statuses, for $name",
async ({ httpClient }) => {
const postId = "1";
const body = { title: "My great post", body: "Some content", userId: 1 };
const response = await httpClient.updatePostWithIncorrectReturnCode({
urlParams: { postId: "1" },
body,
});
expect(response.status).toBe(200);
expect(response.body).toEqual({ ...body, id: +postId });
},
);

describe("explicit error when calling without respecting the contract", () => {
const todoSchema = z.object({
userId: z.number(),
Expand Down

0 comments on commit 1691d0a

Please sign in to comment.