Add not found errors

This commit is contained in:
Alexandre Bodin 2019-11-25 16:33:00 +01:00
parent e60e648a78
commit 0f79a20ef1
4 changed files with 64 additions and 31 deletions

View File

@ -1,5 +1,7 @@
'use strict'; 'use strict';
const _ = require('lodash');
const { const {
validateComponentInput, validateComponentInput,
validateUpdateComponentInput, validateUpdateComponentInput,
@ -81,6 +83,10 @@ module.exports = {
const { uid } = ctx.params; const { uid } = ctx.params;
const { body } = ctx.request; const { body } = ctx.request;
if (!_.has(strapi.components, uid)) {
return ctx.send({ error: 'component.notFound' }, 404);
}
try { try {
await validateUpdateComponentInput(body); await validateUpdateComponentInput(body);
} catch (error) { } catch (error) {
@ -112,6 +118,10 @@ module.exports = {
async deleteComponent(ctx) { async deleteComponent(ctx) {
const { uid } = ctx.params; const { uid } = ctx.params;
if (!_.has(strapi.components, uid)) {
return ctx.send({ error: 'component.notFound' }, 404);
}
try { try {
strapi.reload.isWatching = false; strapi.reload.isWatching = false;

View File

@ -76,6 +76,10 @@ module.exports = {
const { uid } = ctx.params; const { uid } = ctx.params;
const { body } = ctx.request; const { body } = ctx.request;
if (!_.has(strapi.contentTypes, uid)) {
return ctx.send({ error: 'contentType.notFound' }, 404);
}
try { try {
await validateUpdateContentTypeInput(body); await validateUpdateContentTypeInput(body);
} catch (error) { } catch (error) {
@ -102,6 +106,10 @@ module.exports = {
async deleteContentType(ctx) { async deleteContentType(ctx) {
const { uid } = ctx.params; const { uid } = ctx.params;
if (!_.has(strapi.contentTypes, uid)) {
return ctx.send({ error: 'contentType.notFound' }, 404);
}
try { try {
strapi.reload.isWatching = false; strapi.reload.isWatching = false;

View File

@ -34,7 +34,8 @@ const VALID_TYPES = [
const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, { const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {
modelType: modelTypes.COMPONENT, modelType: modelTypes.COMPONENT,
}).shape({ })
.shape({
icon: yup icon: yup
.string() .string()
.nullable() .nullable()
@ -45,7 +46,9 @@ const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {
.nullable() .nullable()
.test(isValidName) .test(isValidName)
.required('category.required'), .required('category.required'),
}); })
.required()
.noUnknown();
const nestedComponentSchema = yup.array().of( const nestedComponentSchema = yup.array().of(
componentSchema componentSchema
@ -63,19 +66,16 @@ const nestedComponentSchema = yup.array().of(
}, },
}) })
.required() .required()
.noUnknown()
); );
const createComponentSchema = () => { const validateComponentInput = data => {
return yup return yup
.object({ .object({
component: componentSchema.required().noUnknown(), component: componentSchema,
components: nestedComponentSchema, components: nestedComponentSchema,
}) })
.noUnknown(); .noUnknown()
};
const validateComponentInput = data => {
return createComponentSchema()
.validate(data, { .validate(data, {
strict: true, strict: true,
abortEarly: false, abortEarly: false,
@ -105,7 +105,12 @@ const validateUpdateComponentInput = data => {
}); });
} }
return createComponentSchema() return yup
.object({
component: componentSchema,
components: nestedComponentSchema,
})
.noUnknown()
.validate(data, { .validate(data, {
strict: true, strict: true,
abortEarly: false, abortEarly: false,
@ -116,6 +121,6 @@ const validateUpdateComponentInput = data => {
module.exports = { module.exports = {
validateComponentInput, validateComponentInput,
validateUpdateComponentInput, validateUpdateComponentInput,
componentSchema,
nestedComponentSchema, nestedComponentSchema,
}; };

View File

@ -44,17 +44,15 @@ const contentTypeSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {
modelType: modelTypes.CONTENT_TYPE, modelType: modelTypes.CONTENT_TYPE,
}); });
const createContentTypeSchema = () => { const createContentTypeSchema = yup
return yup
.object({ .object({
contentType: contentTypeSchema.required().noUnknown(), contentType: contentTypeSchema.required().noUnknown(),
components: nestedComponentSchema, components: nestedComponentSchema,
}) })
.noUnknown(); .noUnknown();
};
const validateContentTypeInput = data => { const validateContentTypeInput = data => {
return createContentTypeSchema() return createContentTypeSchema
.validate(data, { .validate(data, {
strict: true, strict: true,
abortEarly: false, 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, { .validate(data, {
strict: true, strict: true,
abortEarly: false, abortEarly: false,