Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow standard schemas to validate endpoint values #4864

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resultSchema -> responseSchema
  • Loading branch information
EskiMojo14 committed Mar 25, 2025
commit 150a286d60b672d988406ef9d1a84efa02c01809
10 changes: 5 additions & 5 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
@@ -566,7 +566,7 @@ export function buildThunks<
finalQueryArg: unknown,
): Promise<QueryReturnValue> {
let result: QueryReturnValue
const { extraOptions, argSchema, rawResultSchema, resultSchema } =
const { extraOptions, argSchema, rawResponseSchema, responseSchema } =
endpointDefinition

if (argSchema) {
@@ -628,8 +628,8 @@ export function buildThunks<

let { data } = result

if (rawResultSchema) {
data = await parseWithSchema(rawResultSchema, result.data)
if (rawResponseSchema) {
data = await parseWithSchema(rawResponseSchema, result.data)
}

let transformedResponse = await transformResponse(
@@ -638,9 +638,9 @@ export function buildThunks<
finalQueryArg,
)

if (resultSchema) {
if (responseSchema) {
transformedResponse = await parseWithSchema(
resultSchema,
responseSchema,
transformedResponse,
)
}
6 changes: 3 additions & 3 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ type EndpointDefinitionWithQuery<
): unknown

/** A schema for the result *before* it's passed to `transformResponse` */
rawResultSchema?: StandardSchemaV1<BaseQueryResult<BaseQuery>>
rawResponseSchema?: StandardSchemaV1<BaseQueryResult<BaseQuery>>

/** A schema for the error object returned by the `query` or `queryFn`, *before* it's passed to `transformErrorResponse` */
rawErrorSchema?: StandardSchemaV1<BaseQueryError<BaseQuery>>
@@ -169,7 +169,7 @@ type EndpointDefinitionWithQueryFn<
query?: never
transformResponse?: never
transformErrorResponse?: never
rawResultSchema?: never
rawResponseSchema?: never
rawErrorSchema?: never
}

@@ -193,7 +193,7 @@ export type BaseEndpointDefinition<
argSchema?: StandardSchemaV1<QueryArg>

/** A schema for the result (including `transformResponse` if provided) */
resultSchema?: StandardSchemaV1<ResultType>
responseSchema?: StandardSchemaV1<ResultType>

/** A schema for the error object returned by the `query` or `queryFn` (including `transformErrorResponse` if provided) */
errorSchema?: StandardSchemaV1<BaseQueryError<BaseQuery>>
13 changes: 6 additions & 7 deletions packages/toolkit/src/query/tests/createApi.test-d.ts
Original file line number Diff line number Diff line change
@@ -398,7 +398,7 @@ describe('type tests', () => {
query: build.query<Post, { id: number }>({
query: ({ id }) => `/post/${id}`,
argSchema,
resultSchema: postSchema,
responseSchema: postSchema,
errorSchema,
metaSchema,
}),
@@ -407,7 +407,7 @@ describe('type tests', () => {
// @ts-expect-error wrong schema
argSchema: v.object({ id: v.string() }),
// @ts-expect-error wrong schema
resultSchema: v.object({ id: v.string() }),
responseSchema: v.object({ id: v.string() }),
// @ts-expect-error wrong schema
errorSchema: v.object({ status: v.string() }),
// @ts-expect-error wrong schema
@@ -420,7 +420,7 @@ describe('type tests', () => {
id: v.pipe(v.string(), v.transform(Number), v.number()),
}),
// @ts-expect-error can't expect different input
resultSchema: v.object({
responseSchema: v.object({
...postSchema.entries,
id: v.pipe(v.string(), v.transform(Number)),
}) satisfies v.GenericSchema<any, Post>,
@@ -445,7 +445,7 @@ describe('type tests', () => {
id: v.pipe(v.number(), v.transform(String)),
}),
// @ts-expect-error can't provide different output
resultSchema: v.object({
responseSchema: v.object({
...postSchema.entries,
id: v.pipe(v.number(), v.transform(String)),
}) satisfies v.GenericSchema<Post, any>,
@@ -471,9 +471,8 @@ describe('type tests', () => {
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
endpoints: (build) => ({
query: build.query({
query: ({ id }) => `/post/${id}`,
argSchema,
resultSchema: postSchema,
query: ({ id }: { id: number }) => `/post/${id}`,
responseSchema: postSchema,
}),
}),
})
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
@@ -1233,7 +1233,7 @@ describe('endpoint schemas', () => {
endpoints: (build) => ({
query: build.query<{ success: boolean }, void>({
query: () => '/success',
rawResultSchema: v.object({ value: v.literal('success!') }),
rawResponseSchema: v.object({ value: v.literal('success!') }),
}),
}),
})
@@ -1250,7 +1250,7 @@ describe('endpoint schemas', () => {
query: build.query<{ success: boolean }, void>({
query: () => '/success',
transformResponse: () => ({ success: false }),
resultSchema: v.object({ success: v.literal(true) }),
responseSchema: v.object({ success: v.literal(true) }),
}),
}),
})