Просмотр исходного кода

move schema validator to route and static methods

ryoji-s 3 лет назад
Родитель
Сommit
d6698dddd3

+ 2 - 3
apps/app/src/components/Bookmarks/BookmarkFolderItem.tsx

@@ -159,9 +159,8 @@ export const BookmarkFolderItem: FC<BookmarkFolderItemProps> = (props: BookmarkF
       }
 
       // Maximum folder hierarchy of 2 levels
-      const dropSourceFolderHasChildren = item.bookmarkFolder.children.length !== 0;
-      const dropDestFolderHasParent = bookmarkFolder.parent != null;
-      if (dropSourceFolderHasChildren || dropDestFolderHasParent) {
+      // Check source folder has no chidren or dest folder has no parent
+      if (item.bookmarkFolder.children.length !== 0 || bookmarkFolder.parent != null) {
         return false;
       }
 

+ 12 - 11
apps/app/src/server/models/bookmark-folder.ts

@@ -41,17 +41,6 @@ const bookmarkFolderSchema = new Schema<BookmarkFolderDocument, BookmarkFolderMo
     type: Schema.Types.ObjectId,
     ref: 'BookmarkFolder',
     required: false,
-    // Maximum folder hierarchy of 2 levels
-    validate: {
-      async validator(parent: string | null) {
-        if (!parent) return true;
-        const parentFolder = await this.model('BookmarkFolder').findById(parent);
-        if (!parentFolder) return true;
-        const grandparent = parentFolder.parent;
-        return !grandparent;
-      },
-      message: 'Parent folder must be at most 1 level deep',
-    },
   },
   bookmarks: {
     type: [{
@@ -167,6 +156,18 @@ bookmarkFolderSchema.statics.updateBookmarkFolder = async function(bookmarkFolde
   const parentFolder = parentId ? await this.findById(parentId) : null;
   updateFields.parent = parentFolder?._id ?? null;
 
+  // Maximum folder hierarchy of 2 levels
+  // Check source folder has no chidren or dest folder has no parent
+  if (parentId != null) {
+    if (parentFolder?.parent != null) {
+      throw new Error('Update bookmark folder failed');
+    }
+    const bookmarkFolder = await this.findById(bookmarkFolderId).populate('children');
+    if (bookmarkFolder?.children?.length !== 0) {
+      throw new Error('Update bookmark folder failed');
+    }
+  }
+
   const bookmarkFolder = await this.findByIdAndUpdate(bookmarkFolderId, { $set: updateFields }, { new: true });
   if (bookmarkFolder == null) {
     throw new Error('Update bookmark folder failed');

+ 7 - 2
apps/app/src/server/routes/apiv3/bookmark-folder.ts

@@ -1,7 +1,6 @@
 import { ErrorV3 } from '@growi/core';
 import { body } from 'express-validator';
 
-import { BookmarkFolderItems } from '~/interfaces/bookmark-info';
 import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 import { InvalidParentBookmarkFolderError } from '~/server/models/errors';
 import loggerFactory from '~/utils/logger';
@@ -17,7 +16,13 @@ const router = express.Router();
 const validator = {
   bookmarkFolder: [
     body('name').isString().withMessage('name must be a string'),
-    body('parent').isMongoId().optional({ nullable: true }),
+    body('parent').isMongoId().optional({ nullable: true })
+      .custom(async(parent: string) => {
+        const parentFolder = await BookmarkFolder.findById(parent);
+        if (parentFolder == null || parentFolder.parent != null) {
+          throw new Error('Maximum folder hierarchy of 2 levels');
+        }
+      }),
   ],
   bookmarkPage: [
     body('pageId').isMongoId().withMessage('Page ID must be a valid mongo ID'),