diff --git a/packages/strapi-plugin-content-type-builder/controllers/Components.js b/packages/strapi-plugin-content-type-builder/controllers/Components.js index ce59b57ca3..506cc9be1d 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/Components.js +++ b/packages/strapi-plugin-content-type-builder/controllers/Components.js @@ -1,5 +1,7 @@ 'use strict'; +const _ = require('lodash'); + const { validateComponentInput, validateUpdateComponentInput, @@ -81,6 +83,10 @@ module.exports = { const { uid } = ctx.params; const { body } = ctx.request; + if (!_.has(strapi.components, uid)) { + return ctx.send({ error: 'component.notFound' }, 404); + } + try { await validateUpdateComponentInput(body); } catch (error) { @@ -112,6 +118,10 @@ module.exports = { async deleteComponent(ctx) { const { uid } = ctx.params; + if (!_.has(strapi.components, uid)) { + return ctx.send({ error: 'component.notFound' }, 404); + } + try { strapi.reload.isWatching = false; diff --git a/packages/strapi-plugin-content-type-builder/controllers/ContentTypes.js b/packages/strapi-plugin-content-type-builder/controllers/ContentTypes.js index 5e31c17886..c9b130e2c3 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/ContentTypes.js +++ b/packages/strapi-plugin-content-type-builder/controllers/ContentTypes.js @@ -76,6 +76,10 @@ module.exports = { const { uid } = ctx.params; const { body } = ctx.request; + if (!_.has(strapi.contentTypes, uid)) { + return ctx.send({ error: 'contentType.notFound' }, 404); + } + try { await validateUpdateContentTypeInput(body); } catch (error) { @@ -102,6 +106,10 @@ module.exports = { async deleteContentType(ctx) { const { uid } = ctx.params; + if (!_.has(strapi.contentTypes, uid)) { + return ctx.send({ error: 'contentType.notFound' }, 404); + } + try { strapi.reload.isWatching = false; 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 0b0e93794b..4e14742e05 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/validation/component.js +++ b/packages/strapi-plugin-content-type-builder/controllers/validation/component.js @@ -34,18 +34,21 @@ const VALID_TYPES = [ const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, { modelType: modelTypes.COMPONENT, -}).shape({ - icon: yup - .string() - .nullable() - .test(isValidName) - .required('icon.required'), - category: yup - .string() - .nullable() - .test(isValidName) - .required('category.required'), -}); +}) + .shape({ + icon: yup + .string() + .nullable() + .test(isValidName) + .required('icon.required'), + category: yup + .string() + .nullable() + .test(isValidName) + .required('category.required'), + }) + .required() + .noUnknown(); const nestedComponentSchema = yup.array().of( componentSchema @@ -63,19 +66,16 @@ const nestedComponentSchema = yup.array().of( }, }) .required() + .noUnknown() ); -const createComponentSchema = () => { +const validateComponentInput = data => { return yup .object({ - component: componentSchema.required().noUnknown(), + component: componentSchema, components: nestedComponentSchema, }) - .noUnknown(); -}; - -const validateComponentInput = data => { - return createComponentSchema() + .noUnknown() .validate(data, { strict: true, abortEarly: false, @@ -105,7 +105,12 @@ const validateUpdateComponentInput = data => { }); } - return createComponentSchema() + return yup + .object({ + component: componentSchema, + components: nestedComponentSchema, + }) + .noUnknown() .validate(data, { strict: true, abortEarly: false, @@ -116,6 +121,6 @@ const validateUpdateComponentInput = data => { module.exports = { validateComponentInput, validateUpdateComponentInput, - componentSchema, + nestedComponentSchema, }; diff --git a/packages/strapi-plugin-content-type-builder/controllers/validation/content-type.js b/packages/strapi-plugin-content-type-builder/controllers/validation/content-type.js index 709e16d95c..de33b40197 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/validation/content-type.js +++ b/packages/strapi-plugin-content-type-builder/controllers/validation/content-type.js @@ -44,17 +44,15 @@ const contentTypeSchema = createSchema(VALID_TYPES, VALID_RELATIONS, { modelType: modelTypes.CONTENT_TYPE, }); -const createContentTypeSchema = () => { - return yup - .object({ - contentType: contentTypeSchema.required().noUnknown(), - components: nestedComponentSchema, - }) - .noUnknown(); -}; +const createContentTypeSchema = yup + .object({ + contentType: contentTypeSchema.required().noUnknown(), + components: nestedComponentSchema, + }) + .noUnknown(); const validateContentTypeInput = data => { - return createContentTypeSchema() + return createContentTypeSchema .validate(data, { strict: true, abortEarly: false, @@ -72,7 +70,19 @@ const validateUpdateContentTypeInput = data => { }); } - return createContentTypeSchema() + 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; + } + }); + } + }); + } + + return createContentTypeSchema .validate(data, { strict: true, abortEarly: false,