mirror of
https://github.com/strapi/strapi.git
synced 2025-09-21 14:31:16 +00:00
add fields remover for selected fields on cloned data
Signed-off-by: Francois AUBEUT <contact@mexar.fr>
This commit is contained in:
parent
4791f2f485
commit
a76f29ca35
@ -11,3 +11,4 @@ export { default as getTrad } from './getTrad';
|
|||||||
export { default as ItemTypes } from './ItemTypes';
|
export { default as ItemTypes } from './ItemTypes';
|
||||||
export { default as removeKeyInObject } from './removeKeyInObject';
|
export { default as removeKeyInObject } from './removeKeyInObject';
|
||||||
export { default as removePasswordFieldsFromData } from './removePasswordFieldsFromData';
|
export { default as removePasswordFieldsFromData } from './removePasswordFieldsFromData';
|
||||||
|
export { default as removeFieldsFromClonedData } from './removeFieldsFromClonedData';
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
import { get } from 'lodash';
|
||||||
|
import { getType, getOtherInfos } from './getAttributeInfos';
|
||||||
|
|
||||||
|
const defaultFields = [
|
||||||
|
'created_at',
|
||||||
|
'createdAt',
|
||||||
|
'created_by',
|
||||||
|
'createdBy',
|
||||||
|
'updated_at',
|
||||||
|
'updatedAt',
|
||||||
|
'updated_by',
|
||||||
|
'updatedBy',
|
||||||
|
'id',
|
||||||
|
'_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
const removeFieldsFromClonedData = (
|
||||||
|
data,
|
||||||
|
contentTypeSchema,
|
||||||
|
componentSchema,
|
||||||
|
fields = defaultFields
|
||||||
|
) => {
|
||||||
|
const recursiveCleanData = (data, schema) => {
|
||||||
|
return Object.keys(data).reduce((acc, current) => {
|
||||||
|
const attrType = getType(schema.schema, current);
|
||||||
|
const value = get(data, current);
|
||||||
|
const component = getOtherInfos(schema.schema, [current, 'component']);
|
||||||
|
const isRepeatable = getOtherInfos(schema.schema, [current, 'repeatable']);
|
||||||
|
|
||||||
|
if (fields.indexOf(current) !== -1) {
|
||||||
|
delete acc[current];
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attrType === 'dynamiczone') {
|
||||||
|
acc[current] = value.map(componentValue => {
|
||||||
|
const subCleanedData = recursiveCleanData(
|
||||||
|
componentValue,
|
||||||
|
componentSchema[componentValue.__component]
|
||||||
|
);
|
||||||
|
|
||||||
|
return subCleanedData;
|
||||||
|
});
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attrType === 'component') {
|
||||||
|
if (isRepeatable) {
|
||||||
|
/* eslint-disable indent */
|
||||||
|
acc[current] = value
|
||||||
|
? value.map(compoData => {
|
||||||
|
const subCleanedData = recursiveCleanData(compoData, componentSchema[component]);
|
||||||
|
|
||||||
|
return subCleanedData;
|
||||||
|
})
|
||||||
|
: value;
|
||||||
|
/* eslint-enable indent */
|
||||||
|
} else {
|
||||||
|
acc[current] = value ? recursiveCleanData(value, componentSchema[component]) : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
acc[current] = value;
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
|
||||||
|
return recursiveCleanData(data, contentTypeSchema);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default removeFieldsFromClonedData;
|
@ -0,0 +1,39 @@
|
|||||||
|
import { testData } from '../../testUtils';
|
||||||
|
import removeFieldsFromClonedData from '../removeFieldsFromClonedData';
|
||||||
|
|
||||||
|
describe('CONTENT MANAGER | containers | EditViewDataManager | utils', () => {
|
||||||
|
describe('removeFieldsFromClonedData', () => {
|
||||||
|
it('should return an empty object', () => {
|
||||||
|
const { components, contentType } = testData;
|
||||||
|
|
||||||
|
expect(removeFieldsFromClonedData({}, contentType, components)).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the initial data if there is no field with the specified key', () => {
|
||||||
|
const { components, contentType } = testData;
|
||||||
|
|
||||||
|
expect(
|
||||||
|
removeFieldsFromClonedData({ name: 'test' }, contentType, components, ['_id'])
|
||||||
|
).toEqual({
|
||||||
|
name: 'test',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove the specified field for a simple data structure', () => {
|
||||||
|
const { components, contentType } = testData;
|
||||||
|
const data = { _id: 'test', name: 'test' };
|
||||||
|
const expected = { name: 'test' };
|
||||||
|
|
||||||
|
expect(removeFieldsFromClonedData(data, contentType, components, ['_id'])).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove all password fields', () => {
|
||||||
|
const { components, contentType, modifiedData, expectedNoFieldsModifiedData } = testData;
|
||||||
|
const fields = ['id', 'created_at', 'updated_at'];
|
||||||
|
|
||||||
|
expect(removeFieldsFromClonedData(modifiedData, contentType, components, fields)).toEqual(
|
||||||
|
expectedNoFieldsModifiedData
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user