chore: create util to check if folder is the same as another one or a child one

This commit is contained in:
Marc-Roig 2023-05-22 10:06:37 +02:00
parent a7b8ad0fd5
commit 087ff3765b
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249
3 changed files with 12 additions and 10 deletions

View File

@ -0,0 +1,8 @@
'use strict';
const isFolderOrChild = (folderOrChild, folder) =>
folderOrChild.path === folder.path || folderOrChild.path.startsWith(`${folder.path}/`);
module.exports = {
isFolderOrChild,
};

View File

@ -4,6 +4,7 @@ const { intersection, map, isEmpty } = require('lodash/fp');
const { yup, validateYupSchema } = require('@strapi/utils');
const { FOLDER_MODEL_UID } = require('../../../constants');
const { folderExists } = require('./utils');
const { isFolderOrChild } = require('../../utils/folders');
const validateDeleteManyFoldersFilesSchema = yup
.object()
@ -75,11 +76,7 @@ const validateMoveFoldersNotInsideThemselvesSchema = yup
});
const unmovableFoldersNames = folders
.filter(
(folder) =>
destinationFolder.path === folder.path ||
destinationFolder.path.startsWith(`${folder.path}/`)
)
.filter((folder) => isFolderOrChild(destinationFolder, folder))
.map((f) => f.name);
if (unmovableFoldersNames.length > 0) {
return this.createError({

View File

@ -5,6 +5,7 @@ const { yup, validateYupSchema } = require('@strapi/utils');
const { getService } = require('../../../utils');
const { FOLDER_MODEL_UID } = require('../../../constants');
const { folderExists } = require('./utils');
const { isFolderOrChild } = require('../../utils/folders');
const NO_SLASH_REGEX = /^[^/]+$/;
const NO_SPACES_AROUND = /^(?! ).+(?<! )$/;
@ -78,11 +79,7 @@ const validateUpdateFolderSchema = (id) =>
if (!destinationFolder || !currentFolder) return true;
const isUnMovable =
destinationFolder.path === currentFolder.path ||
destinationFolder.path.startsWith(`${currentFolder.path}/`);
return !isUnMovable;
return !isFolderOrChild(destinationFolder, currentFolder);
}
),
})