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';
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;

View File

@ -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;

View File

@ -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,
};

View File

@ -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,