diff --git a/packages/strapi-plugin-content-type-builder/controllers/Components.js b/packages/strapi-plugin-content-type-builder/controllers/Components.js index 4372735fff..dcd2cfe23a 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/Components.js +++ b/packages/strapi-plugin-content-type-builder/controllers/Components.js @@ -57,7 +57,7 @@ module.exports = { return ctx.send({ error }, 400); } - const uid = componentService.createComponentUID(body); + const uid = componentService.createComponentUID(body.component); if (_.has(strapi.components, uid)) { return ctx.send({ error: 'component.alreadyExists' }, 400); @@ -67,7 +67,7 @@ module.exports = { const newComponent = await componentService.createComponent({ uid, - infos: body, + infos: body.component, }); strapi.reload(); @@ -91,12 +91,12 @@ module.exports = { } try { - await validateUpdateComponentInput(body); + await validateUpdateComponentInput(body.component); } catch (error) { return ctx.send({ error }, 400); } - const newUID = componentService.editComponentUID(body); + const newUID = componentService.editComponentUID(body.component); if (newUID !== uid && _.has(strapi.components, newUID)) { return ctx.send({ error: 'new.component.alreadyExists' }, 400); } @@ -105,7 +105,7 @@ module.exports = { const updatedComponent = await componentService.updateComponent({ uid, - infos: body, + infos: body.component, }); await componentService.updateComponentInModels( diff --git a/packages/strapi-plugin-content-type-builder/controllers/validation/component.js b/packages/strapi-plugin-content-type-builder/controllers/validation/component.js index 9acb068dc4..8aeb5d3e96 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/validation/component.js +++ b/packages/strapi-plugin-content-type-builder/controllers/validation/component.js @@ -32,19 +32,28 @@ const VALID_TYPES = [ 'component', ]; +const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, { + modelType: modelTypes.COMPONENT, +}).shape({ + icon: yup + .string() + .nullable() + .test(isValidName) + .required(), + category: yup + .string() + .nullable() + .test(isValidName) + .required(), +}); + const createComponentSchema = () => { - return createSchema(VALID_TYPES, VALID_RELATIONS, { - modelType: modelTypes.COMPONENT, - }).shape({ - icon: yup - .string() - .nullable() - .test(isValidName), - category: yup - .string() - .nullable() - .test(isValidName), - }); + return yup + .object({ + component: componentSchema.required().noUnknown(), + components: yup.array().of(componentSchema), + }) + .noUnknown(); }; const validateComponentInput = data => { @@ -58,10 +67,22 @@ const validateComponentInput = data => { const validateUpdateComponentInput = data => { // convert zero length string on default attributes to undefined - if (_.has(data, 'attributes')) { - Object.keys(data.attributes).forEach(attribute => { - if (data.attributes[attribute].default === '') { - data.attributes[attribute].default = undefined; + if (_.has(data, ['component', 'attributes'])) { + Object.keys(data.component.attributes).forEach(attribute => { + if (data.component.attributes[attribute].default === '') { + data.component.attributes[attribute].default = undefined; + } + }); + } + + if (_.has(data, 'components') && Array.isArray(data.components)) { + data.components.forEach(data => { + if (_.has(data, 'attributes') && _.has(data, 'uid')) { + Object.keys(data.attributes).forEach(attribute => { + if (data.attributes[attribute].default === '') { + data.attributes[attribute].default = undefined; + } + }); } }); }