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

Blacklist nested attributes #134

Closed
39otrebla opened this issue Jul 14, 2016 · 8 comments
Closed

Blacklist nested attributes #134

39otrebla opened this issue Jul 14, 2016 · 8 comments

Comments

@39otrebla
Copy link

39otrebla commented Jul 14, 2016

Assuming my store objects looks like this
{ data: {search: {data: [....]}, history:{search: [...], product: [...]}, favourites: {search: [...], product: [...]}}, navigation: {....}, user: {....}}

How can I blacklist data.search, while keeping data.history and data.favourites ?
I've tried with['data.search'], but no luck.

Thank you.

@rt2zz
Copy link
Owner

rt2zz commented Jul 21, 2016

This is not available out of the box but it should be trivial to write a transform that does that. It would look something like

let blacklistTransform = createTransform(
  (inboundState, key) => {
      if (key !== 'data') return inboundState
      else return {
        ...inboundState,
        data: undefined,
       }
  }
)

@rt2zz
Copy link
Owner

rt2zz commented Sep 18, 2016

while I have not personally used it, this project aims to solve this problem: https://github.com/edy/redux-persist-transform-filter

@rt2zz rt2zz closed this as completed Sep 18, 2016
@brunolemos
Copy link

brunolemos commented Dec 2, 2016

@rt2zz lodash accept nested paths, so you could use pick for whitelist and omit for blacklist.

@ankianan
Copy link

ankianan commented May 7, 2017

There is one more way. Store a getter function in place of nested key which needs to be blacklisted.

let nestedKey = "blacklisted";
let getNestedKey = ()=>nestedKey;
...
{
case REHYDRATE : return {...state,
nestedKey:getNestedKey
};
}

In storage getter function will always be null.

When passing as props call the getter function to get the actual value.

@gyosifov
Copy link

gyosifov commented Oct 8, 2018

As @brunolemos and @rt2zz have suggested the code looks something like this:

import omit from 'lodash/omit'

let blacklistTransform = createTransform(
    (inboundState, key) => {
        if (key === 'data') {
            return omit(inboundState, ['search']);
        } else {
            return inboundState;
        }
    }
)
...
const persistConfig = {
    key: 'root',
    storage,
    transforms: [blacklistTransform],
}

@Venryx
Copy link

Venryx commented Jan 25, 2019

Here's a slightly more modular version of @gyosifov's solution above:

import omit from 'lodash/omit';

const blacklistPaths = ['prop1', 'prop2', 'prop3.nestedProp'];
const persistConfig = {
	[...]
	blacklist: blacklistPaths.filter(a => !a.includes('.')),
	transforms: [
		// nested blacklist-paths require a custom transform to be applied
		createTransform((inboundState, key) => {
			const blacklistPaths_forKey = blacklistPaths.filter(path => path.startsWith(`${key}.`)).map(path => path.substr(key.length + 1));
			return omit(inboundState, ...blacklistPaths_forKey);
		}, null),
	],
};

@nandorojo
Copy link

Has anyone come up with a better solution so far?

@rahulsuresh-git
Copy link

@Venryx Works well, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants