Skip to content

Commit e4cd61c

Browse files
committed
Support RegExp in ignoredPaths/ignoredActionPaths
Fix: reduxjs#2553
1 parent 4383544 commit e4cd61c

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

docs/api/serializabilityMiddleware.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ interface SerializableStateInvariantMiddlewareOptions {
3939
ignoredActions?: string[]
4040

4141
/**
42-
* An array of dot-separated path strings to ignore when checking
43-
* for serializability, Defaults to ['meta.arg', 'meta.baseQueryMeta']
42+
* An array of dot-separated path strings or regular expressions to ignore
43+
* when checking for serializability, Defaults to
44+
* ['meta.arg', 'meta.baseQueryMeta']
4445
*/
45-
ignoredActionPaths?: string[]
46+
ignoredActionPaths?: (string | RegExp)[]
4647

4748
/**
48-
* An array of dot-separated path strings to ignore when checking
49-
* for serializability, Defaults to []
49+
* An array of dot-separated path strings or regular expressions to ignore
50+
* when checking for serializability, Defaults to []
5051
*/
51-
ignoredPaths?: string[]
52+
ignoredPaths?: (string | RegExp)[]
5253
/**
5354
* Execution time warning threshold. If the middleware takes longer
5455
* than `warnAfter` ms, a warning will be displayed in the console.

packages/toolkit/etc/redux-toolkit.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ export function findNonSerializableValue(
511511
path?: string,
512512
isSerializable?: (value: unknown) => boolean,
513513
getEntries?: (value: unknown) => [string, any][],
514-
ignoredPaths?: readonly string[]
514+
ignoredPaths?: readonly (string | RegExp)[]
515515
): NonSerializableValue | false
516516

517517
export { freeze }
@@ -762,9 +762,9 @@ export { Selector }
762762
// @public
763763
export interface SerializableStateInvariantMiddlewareOptions {
764764
getEntries?: (value: any) => [string, any][]
765-
ignoredActionPaths?: string[]
765+
ignoredActionPaths?: (string | RegExp)[]
766766
ignoredActions?: string[]
767-
ignoredPaths?: string[]
767+
ignoredPaths?: (string | RegExp)[]
768768
ignoreState?: boolean
769769
isSerializable?: (value: any) => boolean
770770
warnAfter?: number

packages/toolkit/src/serializableStateInvariantMiddleware.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function findNonSerializableValue(
3636
path: string = '',
3737
isSerializable: (value: unknown) => boolean = isPlain,
3838
getEntries?: (value: unknown) => [string, any][],
39-
ignoredPaths: readonly string[] = []
39+
ignoredPaths: readonly (string | RegExp)[] = []
4040
): NonSerializableValue | false {
4141
let foundNestedSerializable: NonSerializableValue | false
4242

@@ -58,7 +58,13 @@ export function findNonSerializableValue(
5858
for (const [key, nestedValue] of entries) {
5959
const nestedPath = path ? path + '.' + key : key
6060

61-
if (hasIgnoredPaths && ignoredPaths.indexOf(nestedPath) >= 0) {
61+
if (hasIgnoredPaths) {
62+
const hasMatches = ignoredPaths.some(ignored => {
63+
if (ignored instanceof RegExp) {
64+
return ignored.test(nestedPath);
65+
}
66+
return nestedPath === ignored;
67+
});
6268
continue
6369
}
6470

@@ -113,16 +119,17 @@ export interface SerializableStateInvariantMiddlewareOptions {
113119
ignoredActions?: string[]
114120

115121
/**
116-
* An array of dot-separated path strings to ignore when checking
117-
* for serializability, Defaults to ['meta.arg', 'meta.baseQueryMeta']
122+
* An array of dot-separated path strings or regular expressions to ignore
123+
* when checking for serializability, Defaults to
124+
* ['meta.arg', 'meta.baseQueryMeta']
118125
*/
119-
ignoredActionPaths?: string[]
126+
ignoredActionPaths?: (string | RegExp)[]
120127

121128
/**
122-
* An array of dot-separated path strings to ignore when checking
123-
* for serializability, Defaults to []
129+
* An array of dot-separated path strings or regular expressions to ignore
130+
* when checking for serializability, Defaults to []
124131
*/
125-
ignoredPaths?: string[]
132+
ignoredPaths?: (string | RegExp)[]
126133
/**
127134
* Execution time warning threshold. If the middleware takes longer
128135
* than `warnAfter` ms, a warning will be displayed in the console.

packages/toolkit/src/tests/serializableStateInvariantMiddleware.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ describe('serializableStateInvariantMiddleware', () => {
389389

390390
expect(getLog().log).toMatchInlineSnapshot(`""`)
391391
})
392+
393+
it('can specify regexp', () => {
394+
configureStore({
395+
reducer,
396+
middleware: [
397+
createSerializableStateInvariantMiddleware({
398+
ignoredActionPaths: [/^payload\..*$/],
399+
}),
400+
],
401+
}).dispatch({
402+
type: 'test',
403+
payload: { arg: nonSerializableValue },
404+
})
405+
406+
expect(getLog().log).toMatchInlineSnapshot(`""`)
407+
})
392408
})
393409

394410
it('allows ignoring actions entirely', () => {
@@ -439,6 +455,10 @@ describe('serializableStateInvariantMiddleware', () => {
439455
d: badValue,
440456
},
441457
e: { f: badValue },
458+
g: {
459+
h: badValue,
460+
i: badValue,
461+
},
442462
}
443463
}
444464
default:
@@ -455,6 +475,8 @@ describe('serializableStateInvariantMiddleware', () => {
455475
'testSlice.b.c',
456476
// Test for ignoring an object and its children
457477
'testSlice.e',
478+
// Test for ignoring based on RegExp
479+
/^testSlice\.g\..*$/,
458480
],
459481
})
460482

0 commit comments

Comments
 (0)