Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
Add support to validate components recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
EdisonPeM committed Jun 17, 2021
1 parent b4f0fb0 commit d24dd27
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
54 changes: 54 additions & 0 deletions services/utils/contentChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,61 @@ async function getValidMedia(value, attribute, user) {
}
}

async function getValidSingleComponent(value, attributes, user) {
const mappedComponent = {};
for (const attr in attributes) {
const element = value[attr];
if (element) {
let mappedElement = element;
const { type, model, collection, plugin } = attributes[attr];
if (plugin && plugin === "upload") {
const multiple = collection && !model;
mappedElement = await getValidMedia(element, { multiple }, user);
} else if (model || collection) {
const targetModel = collection || model;
const relationType = collection && !model ? "manyWay" : "oneWay";
mappedElement = await getValidRelations(element, {
relationType,
targetModel,
});
} else if (type === "component") {
mappedElement = await getValidComponent(
element,
attributes[attr],
user
);
}

mappedComponent[attr] = mappedElement;
}
}

return mappedComponent;
}
async function getValidComponent(value, attribute, user) {
const { repeatable, component } = attribute;
const { attributes } = strapi.components[component];

if (repeatable) {
const componentValues = Array.isArray(value) ? value : [value];
return Promise.all(
componentValues.map((val) =>
getValidSingleComponent(val, attributes, user)
)
);
} else {
const componentValue = Array.isArray(value) ? value[0] : value;
return getValidSingleComponent(componentValue, attributes, user);
}
}

async function getValidDynamic(value, attribute, user) {
return value;
}

module.exports = {
getValidRelations,
getValidMedia,
getValidComponent,
getValidDynamic,
};
11 changes: 10 additions & 1 deletion services/utils/fieldUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const {
stringIsUrl,
urlIsMedia,
} = require("./formatsValidator");
const { getValidRelations, getValidMedia } = require("./contentChecker");
const {
getValidRelations,
getValidMedia,
getValidComponent,
getValidDynamic,
} = require("./contentChecker");

function getFormatFromField(field) {
switch (typeof field) {
Expand Down Expand Up @@ -59,6 +64,10 @@ function mapFieldsToTargetFields({ items, fields, attributes, user }) {
targetItem = await getValidRelations(targetItem, attribute);
} else if (attribute.type === "media") {
targetItem = await getValidMedia(targetItem, attribute, user);
} else if (attribute.type === "component") {
targetItem = await getValidComponent(targetItem, attribute, user);
} else if (attribute.type === "dynamiczone") {
targetItem = await getValidDynamic(targetItem, attribute, user);
}

mappedItem[targetField] = targetItem;
Expand Down

0 comments on commit d24dd27

Please sign in to comment.