Skip to content

Commit 79dba51

Browse files
authored
allow passing an array of specific schemas to skip (#5042)
1 parent 79af5cf commit 79dba51

File tree

6 files changed

+121
-183
lines changed

6 files changed

+121
-183
lines changed

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131
SchemaFailureConverter,
3232
SchemaFailureHandler,
3333
SchemaFailureInfo,
34+
SchemaType,
3435
} from '../endpointDefinitions'
3536
import {
3637
calculateProvidedBy,
@@ -68,7 +69,11 @@ import {
6869
isRejectedWithValue,
6970
SHOULD_AUTOBATCH,
7071
} from './rtkImports'
71-
import { parseWithSchema, NamedSchemaError } from '../standardSchema'
72+
import {
73+
parseWithSchema,
74+
NamedSchemaError,
75+
shouldSkip,
76+
} from '../standardSchema'
7277

7378
export type BuildThunksApiEndpointQuery<
7479
Definition extends QueryDefinition<any, any, any, any, any>,
@@ -346,7 +351,7 @@ export function buildThunks<
346351
selectors: AllSelectors
347352
onSchemaFailure: SchemaFailureHandler | undefined
348353
catchSchemaFailure: SchemaFailureConverter<BaseQuery> | undefined
349-
skipSchemaValidation: boolean | undefined
354+
skipSchemaValidation: boolean | SchemaType[] | undefined
350355
}) {
351356
type State = RootState<any, string, ReducerPath>
352357

@@ -569,7 +574,7 @@ export function buildThunks<
569574
const { extraOptions, argSchema, rawResponseSchema, responseSchema } =
570575
endpointDefinition
571576

572-
if (argSchema && !skipSchemaValidation) {
577+
if (argSchema && !shouldSkip(skipSchemaValidation, 'arg')) {
573578
finalQueryArg = await parseWithSchema(
574579
argSchema,
575580
finalQueryArg,
@@ -633,7 +638,10 @@ export function buildThunks<
633638

634639
let { data } = result
635640

636-
if (rawResponseSchema && !skipSchemaValidation) {
641+
if (
642+
rawResponseSchema &&
643+
!shouldSkip(skipSchemaValidation, 'rawResponse')
644+
) {
637645
data = await parseWithSchema(
638646
rawResponseSchema,
639647
result.data,
@@ -648,7 +656,7 @@ export function buildThunks<
648656
finalQueryArg,
649657
)
650658

651-
if (responseSchema && !skipSchemaValidation) {
659+
if (responseSchema && !shouldSkip(skipSchemaValidation, 'response')) {
652660
transformedResponse = await parseWithSchema(
653661
responseSchema,
654662
transformedResponse,
@@ -751,7 +759,11 @@ export function buildThunks<
751759
finalQueryReturnValue = await executeRequest(arg.originalArgs)
752760
}
753761

754-
if (metaSchema && !skipSchemaValidation && finalQueryReturnValue.meta) {
762+
if (
763+
metaSchema &&
764+
!shouldSkip(skipSchemaValidation, 'meta') &&
765+
finalQueryReturnValue.meta
766+
) {
755767
finalQueryReturnValue.meta = await parseWithSchema(
756768
metaSchema,
757769
finalQueryReturnValue.meta,
@@ -781,7 +793,10 @@ export function buildThunks<
781793
let { value, meta } = caughtError
782794

783795
try {
784-
if (rawErrorResponseSchema && !skipSchemaValidation) {
796+
if (
797+
rawErrorResponseSchema &&
798+
!shouldSkip(skipSchemaValidation, 'rawErrorResponse')
799+
) {
785800
value = await parseWithSchema(
786801
rawErrorResponseSchema,
787802
value,
@@ -790,15 +805,18 @@ export function buildThunks<
790805
)
791806
}
792807

793-
if (metaSchema && !skipSchemaValidation) {
808+
if (metaSchema && !shouldSkip(skipSchemaValidation, 'meta')) {
794809
meta = await parseWithSchema(metaSchema, meta, 'metaSchema', meta)
795810
}
796811
let transformedErrorResponse = await transformErrorResponse(
797812
value,
798813
meta,
799814
arg.originalArgs,
800815
)
801-
if (errorResponseSchema && !skipSchemaValidation) {
816+
if (
817+
errorResponseSchema &&
818+
!shouldSkip(skipSchemaValidation, 'errorResponse')
819+
) {
802820
transformedErrorResponse = await parseWithSchema(
803821
errorResponseSchema,
804822
transformedErrorResponse,

packages/toolkit/src/query/createApi.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
EndpointDefinitions,
99
SchemaFailureConverter,
1010
SchemaFailureHandler,
11+
SchemaType,
1112
} from './endpointDefinitions'
1213
import {
1314
DefinitionType,
@@ -280,6 +281,8 @@ export interface CreateApiOptions<
280281
*
281282
* If set to `true`, will skip schema validation for all endpoints, unless overridden by the endpoint.
282283
*
284+
* Can be overridden for specific schemas by passing an array of schema types to skip.
285+
*
283286
* @example
284287
* ```ts
285288
* // codeblock-meta no-transpile
@@ -288,7 +291,7 @@ export interface CreateApiOptions<
288291
*
289292
* const api = createApi({
290293
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
291-
* skipSchemaValidation: process.env.NODE_ENV === "test", // skip schema validation in tests, since we'll be mocking the response
294+
* skipSchemaValidation: process.env.NODE_ENV === "test" ? ["response"] : false, // skip schema validation for response in tests, since we'll be mocking the response
292295
* endpoints: (build) => ({
293296
* getPost: build.query<Post, { id: number }>({
294297
* query: ({ id }) => `/post/${id}`,
@@ -298,7 +301,7 @@ export interface CreateApiOptions<
298301
* })
299302
* ```
300303
*/
301-
skipSchemaValidation?: boolean
304+
skipSchemaValidation?: boolean | SchemaType[]
302305
}
303306

304307
export type CreateApi<Modules extends ModuleName> = {

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ type BaseEndpointTypes<
249249
RawResultType: RawResultType
250250
}
251251

252+
export type SchemaType =
253+
| 'arg'
254+
| 'rawResponse'
255+
| 'response'
256+
| 'rawErrorResponse'
257+
| 'errorResponse'
258+
| 'meta'
259+
252260
interface CommonEndpointDefinition<
253261
QueryArg,
254262
BaseQuery extends BaseQueryFn,
@@ -427,6 +435,8 @@ interface CommonEndpointDefinition<
427435
* If set to `true`, will skip schema validation for this endpoint.
428436
* Overrides the global setting.
429437
*
438+
* Can be overridden for specific schemas by passing an array of schema types to skip.
439+
*
430440
* @example
431441
* ```ts
432442
* // codeblock-meta no-transpile
@@ -439,13 +449,13 @@ interface CommonEndpointDefinition<
439449
* getPost: build.query<Post, { id: number }>({
440450
* query: ({ id }) => `/post/${id}`,
441451
* responseSchema: v.object({ id: v.number(), name: v.string() }),
442-
* skipSchemaValidation: process.env.NODE_ENV === "test", // skip schema validation in tests, since we'll be mocking the response
452+
* skipSchemaValidation: process.env.NODE_ENV === "test" ? ["response"] : false, // skip schema validation for response in tests, since we'll be mocking the response
443453
* }),
444454
* })
445455
* })
446456
* ```
447457
*/
448-
skipSchemaValidation?: boolean
458+
skipSchemaValidation?: boolean | SchemaType[]
449459
}
450460

451461
export type BaseEndpointDefinition<

packages/toolkit/src/query/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type {
4949
SchemaFailureHandler,
5050
SchemaFailureConverter,
5151
SchemaFailureInfo,
52+
SchemaType,
5253
} from './endpointDefinitions'
5354
export { fetchBaseQuery } from './fetchBaseQuery'
5455
export type {

packages/toolkit/src/query/standardSchema.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import type { StandardSchemaV1 } from '@standard-schema/spec'
22
import { SchemaError } from '@standard-schema/utils'
3+
import type { SchemaType } from './endpointDefinitions'
34

45
export class NamedSchemaError extends SchemaError {
56
constructor(
67
issues: readonly StandardSchemaV1.Issue[],
78
public readonly value: any,
8-
public readonly schemaName: string,
9+
public readonly schemaName: `${SchemaType}Schema`,
910
public readonly _bqMeta: any,
1011
) {
1112
super(issues)
1213
}
1314
}
1415

16+
export const shouldSkip = (
17+
skipSchemaValidation: boolean | SchemaType[] | undefined,
18+
schemaName: SchemaType,
19+
) =>
20+
Array.isArray(skipSchemaValidation)
21+
? skipSchemaValidation.includes(schemaName)
22+
: !!skipSchemaValidation
23+
1524
export async function parseWithSchema<Schema extends StandardSchemaV1>(
1625
schema: Schema,
1726
data: unknown,
18-
schemaName: string,
27+
schemaName: `${SchemaType}Schema`,
1928
bqMeta: any,
2029
): Promise<StandardSchemaV1.InferOutput<Schema>> {
2130
const result = await schema['~standard'].validate(data)

0 commit comments

Comments
 (0)