Init nested component in component

This commit is contained in:
Alexandre Bodin 2019-11-08 15:16:17 +01:00
parent f2b3fec654
commit f61afa45f4
2 changed files with 42 additions and 21 deletions

View File

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

View File

@ -32,19 +32,28 @@ const VALID_TYPES = [
'component',
];
const createComponentSchema = () => {
return createSchema(VALID_TYPES, VALID_RELATIONS, {
const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {
modelType: modelTypes.COMPONENT,
}).shape({
}).shape({
icon: yup
.string()
.nullable()
.test(isValidName),
.test(isValidName)
.required(),
category: yup
.string()
.nullable()
.test(isValidName),
});
.test(isValidName)
.required(),
});
const createComponentSchema = () => {
return yup
.object({
component: componentSchema.required().noUnknown(),
components: yup.array().of(componentSchema),
})
.noUnknown();
};
const validateComponentInput = data => {
@ -58,13 +67,25 @@ const validateComponentInput = data => {
const validateUpdateComponentInput = data => {
// convert zero length string on default attributes to undefined
if (_.has(data, 'attributes')) {
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;
}
});
}
});
}
return createComponentSchema()
.validate(data, {