mirror of
https://github.com/strapi/strapi.git
synced 2025-08-04 14:56:22 +00:00
Init nested component in component
This commit is contained in:
parent
f2b3fec654
commit
f61afa45f4
@ -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(
|
||||
|
@ -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, {
|
||||
|
Loading…
x
Reference in New Issue
Block a user