Skip to content

Commit b27aa9b

Browse files
indutnymarkerikson
authored andcommitted
Support RegExp in ignoredPaths/ignoredActionPaths
Fix: #2553
1 parent 85ec86b commit b27aa9b

File tree

6 files changed

+52
-18
lines changed

6 files changed

+52
-18
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/rtk-codemods/bin/cli.js

100644100755
File mode changed.

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: 18 additions & 9 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,8 +58,16 @@ 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) {
62-
continue
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+
})
68+
if (hasMatches) {
69+
continue
70+
}
6371
}
6472

6573
if (!isSerializable(nestedValue)) {
@@ -113,16 +121,17 @@ export interface SerializableStateInvariantMiddlewareOptions {
113121
ignoredActions?: string[]
114122

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

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Take a look at the reducer(s) handling this action type: TEST_ACTION.
3131

3232
exports[`serializableStateInvariantMiddleware ignored action paths can specify (multiple) different values 1`] = `""`;
3333

34+
exports[`serializableStateInvariantMiddleware ignored action paths can specify regexp 1`] = `""`;
35+
3436
exports[`serializableStateInvariantMiddleware ignored action paths default value can be overridden 1`] = `
3537
"A non-serializable value was detected in an action, in the path: \`meta.arg\`. Value: Map {}
3638
Take a look at the logic that dispatched this action: Object {

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)