Skip to content

Commit

Permalink
Support RegExp in ignoredPaths/ignoredActionPaths
Browse files Browse the repository at this point in the history
Fix: #2553
  • Loading branch information
indutny authored and markerikson committed Jan 28, 2023
1 parent 85ec86b commit b27aa9b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
13 changes: 7 additions & 6 deletions docs/api/serializabilityMiddleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ interface SerializableStateInvariantMiddlewareOptions {
ignoredActions?: string[]

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

/**
* An array of dot-separated path strings to ignore when checking
* for serializability, Defaults to []
* An array of dot-separated path strings or regular expressions to ignore
* when checking for serializability, Defaults to []
*/
ignoredPaths?: string[]
ignoredPaths?: (string | RegExp)[]
/**
* Execution time warning threshold. If the middleware takes longer
* than `warnAfter` ms, a warning will be displayed in the console.
Expand Down
Empty file modified packages/rtk-codemods/bin/cli.js
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions packages/toolkit/etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export function findNonSerializableValue(
path?: string,
isSerializable?: (value: unknown) => boolean,
getEntries?: (value: unknown) => [string, any][],
ignoredPaths?: readonly string[]
ignoredPaths?: readonly (string | RegExp)[]
): NonSerializableValue | false

export { freeze }
Expand Down Expand Up @@ -762,9 +762,9 @@ export { Selector }
// @public
export interface SerializableStateInvariantMiddlewareOptions {
getEntries?: (value: any) => [string, any][]
ignoredActionPaths?: string[]
ignoredActionPaths?: (string | RegExp)[]
ignoredActions?: string[]
ignoredPaths?: string[]
ignoredPaths?: (string | RegExp)[]
ignoreState?: boolean
isSerializable?: (value: any) => boolean
warnAfter?: number
Expand Down
27 changes: 18 additions & 9 deletions packages/toolkit/src/serializableStateInvariantMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function findNonSerializableValue(
path: string = '',
isSerializable: (value: unknown) => boolean = isPlain,
getEntries?: (value: unknown) => [string, any][],
ignoredPaths: readonly string[] = []
ignoredPaths: readonly (string | RegExp)[] = []
): NonSerializableValue | false {
let foundNestedSerializable: NonSerializableValue | false

Expand All @@ -58,8 +58,16 @@ export function findNonSerializableValue(
for (const [key, nestedValue] of entries) {
const nestedPath = path ? path + '.' + key : key

if (hasIgnoredPaths && ignoredPaths.indexOf(nestedPath) >= 0) {
continue
if (hasIgnoredPaths) {
const hasMatches = ignoredPaths.some(ignored => {
if (ignored instanceof RegExp) {
return ignored.test(nestedPath)
}
return nestedPath === ignored
})
if (hasMatches) {
continue
}
}

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

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

/**
* An array of dot-separated path strings to ignore when checking
* for serializability, Defaults to []
* An array of dot-separated path strings or regular expressions to ignore
* when checking for serializability, Defaults to []
*/
ignoredPaths?: string[]
ignoredPaths?: (string | RegExp)[]
/**
* Execution time warning threshold. If the middleware takes longer
* than `warnAfter` ms, a warning will be displayed in the console.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Take a look at the reducer(s) handling this action type: TEST_ACTION.

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

exports[`serializableStateInvariantMiddleware ignored action paths can specify regexp 1`] = `""`;

exports[`serializableStateInvariantMiddleware ignored action paths default value can be overridden 1`] = `
"A non-serializable value was detected in an action, in the path: \`meta.arg\`. Value: Map {}
Take a look at the logic that dispatched this action: Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,22 @@ describe('serializableStateInvariantMiddleware', () => {

expect(getLog().log).toMatchInlineSnapshot(`""`)
})

it('can specify regexp', () => {
configureStore({
reducer,
middleware: [
createSerializableStateInvariantMiddleware({
ignoredActionPaths: [/^payload\..*$/],
}),
],
}).dispatch({
type: 'test',
payload: { arg: nonSerializableValue },
})

expect(getLog().log).toMatchInlineSnapshot(`""`)
})
})

it('allows ignoring actions entirely', () => {
Expand Down Expand Up @@ -439,6 +455,10 @@ describe('serializableStateInvariantMiddleware', () => {
d: badValue,
},
e: { f: badValue },
g: {
h: badValue,
i: badValue,
},
}
}
default:
Expand All @@ -455,6 +475,8 @@ describe('serializableStateInvariantMiddleware', () => {
'testSlice.b.c',
// Test for ignoring an object and its children
'testSlice.e',
// Test for ignoring based on RegExp
/^testSlice\.g\..*$/,
],
})

Expand Down

0 comments on commit b27aa9b

Please sign in to comment.