validate compo

This commit is contained in:
Alexandre Bodin 2021-07-08 22:50:24 +02:00
parent 6d78f94a54
commit d48eaa0d1f
4 changed files with 34 additions and 32 deletions

View File

@ -33,7 +33,7 @@
"redux": "^4.0.1", "redux": "^4.0.1",
"redux-immutable": "^4.0.0", "redux-immutable": "^4.0.0",
"reselect": "^4.0.0", "reselect": "^4.0.0",
"yup": "^0.27.0" "yup": "^0.32.9"
}, },
"author": { "author": {
"name": "Strapi team", "name": "Strapi team",

View File

@ -405,26 +405,27 @@ const createComponents = async (uid, data) => {
continue; continue;
} }
if (attribute.type === 'dynamiczone') { // if (attribute.type === 'dynamiczone') {
const dynamiczoneValues = data[attributeName];
if (!Array.isArray(dynamiczoneValues)) { // const dynamiczoneValues = data[attributeName];
throw new Error('Expected an array to create repeatable component');
}
const components = await Promise.all( // if (!Array.isArray(dynamiczoneValues)) {
dynamiczoneValues.map(value => { // throw new Error('Expected an array to create repeatable component');
return strapi.query(value.__component).create({ data: value }); // }
})
);
componentBody[attributeName] = components.map(({ id }, idx) => { // const components = await Promise.all(
// TODO: add & support pivot data in DB // dynamiczoneValues.map(value => {
return id; // return strapi.query(value.__component).create({ data: value });
}); // })
// );
continue; // componentBody[attributeName] = components.map(({ id }, idx) => {
} // // TODO: add & support pivot data in DB
// return id;
// });
// continue;
// }
} }
return componentBody; return componentBody;
@ -491,24 +492,24 @@ const updateComponents = async (uid, entityToUpdate, data) => {
continue; continue;
} }
if (attribute.type === 'dynamiczone') { // if (attribute.type === 'dynamiczone') {
const dynamiczoneValues = data[attributeName]; // const dynamiczoneValues = data[attributeName];
if (!Array.isArray(dynamiczoneValues)) { // if (!Array.isArray(dynamiczoneValues)) {
throw new Error('Expected an array to create repeatable component'); // throw new Error('Expected an array to create repeatable component');
} // }
const components = await Promise.all( // const components = await Promise.all(
dynamiczoneValues.map(value => updateOrCreateComponent(value.__component, value)) // dynamiczoneValues.map(value => updateOrCreateComponent(value.__component, value))
); // );
componentBody[attributeName] = components.map(({ id }, idx) => { // componentBody[attributeName] = components.map(({ id }, idx) => {
// TODO: add & support pivot data in DB // // TODO: add & support pivot data in DB
return id; // return id;
}); // });
continue; // continue;
} // }
} }
return componentBody; return componentBody;

View File

@ -134,6 +134,7 @@ const createScalarAttributeValidator = createOrUpdate => (attr, { isDraft }) =>
const createAttributeValidator = createOrUpdate => (attr, data, { isDraft }) => { const createAttributeValidator = createOrUpdate => (attr, data, { isDraft }) => {
let validator; let validator;
if (isMediaAttribute(attr)) { if (isMediaAttribute(attr)) {
validator = yup.mixed(); validator = yup.mixed();
} else if (isScalarAttribute(attr)) { } else if (isScalarAttribute(attr)) {

View File

@ -94,7 +94,7 @@ const isPrivateAttribute = (model = {}, attributeName) => {
}; };
const isScalarAttribute = attribute => { const isScalarAttribute = attribute => {
return ['component', 'relation', 'dynamiczone'].includes(attribute.type); return !['component', 'relation', 'dynamiczone'].includes(attribute.type);
}; };
const isMediaAttribute = attr => { const isMediaAttribute = attr => {