Skip to content

Commit

Permalink
remove repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
NickOvt committed Sep 16, 2024
1 parent ab7fbc0 commit b09ef4e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
38 changes: 3 additions & 35 deletions lib/api/mailboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const imapTools = require('../../imap-core/lib/imap-tools');
const tools = require('../tools');
const roles = require('../roles');
const util = require('util');
const { sessSchema, sessIPSchema, booleanSchema } = require('../schemas');
const { sessSchema, sessIPSchema, booleanSchema, mailboxPathValidator } = require('../schemas');
const { userId, mailboxId } = require('../schemas/request/general-schemas');
const { successRes } = require('../schemas/response/general-schemas');
const { GetMailboxesResult } = require('../schemas/response/mailboxes-schemas');
Expand Down Expand Up @@ -283,23 +283,7 @@ module.exports = (db, server, mailboxHandler) => {
path: Joi.string()
.regex(/\/{2,}|\/$/, { invert: true })
.max(MAX_MAILBOX_NAME_LENGTH * MAX_SUB_MAILBOXES + 127)
.custom((value, helpers) => {
const splittedPathArr = value.split('/');

if (splittedPathArr.length > MAX_SUB_MAILBOXES) {
// too many paths
return helpers.message(`The mailbox path cannot be more than ${MAX_SUB_MAILBOXES} levels deep`);
}

for (const pathPart of splittedPathArr) {
if (pathPart.length > MAX_MAILBOX_NAME_LENGTH) {
// part too long error
return helpers.message(`Any part of the mailbox path cannot be longer than ${MAX_MAILBOX_NAME_LENGTH} chars long`);
}
}

return value;
}, 'Mailbox path validation')
.custom(mailboxPathValidator, 'Mailbox path validation')
.required()
.description('Full path of the mailbox, folders are separated by slashes, ends with the mailbox name (unicode string)'),
hidden: booleanSchema.default(false).description('Is the folder hidden or not. Hidden folders can not be opened in IMAP.'),
Expand Down Expand Up @@ -559,23 +543,7 @@ module.exports = (db, server, mailboxHandler) => {
path: Joi.string()
.regex(/\/{2,}|\/$/, { invert: true })
.max(MAX_MAILBOX_NAME_LENGTH * MAX_SUB_MAILBOXES + 127)
.custom((value, helpers) => {
const splittedPathArr = value.split('/');

if (splittedPathArr.length > MAX_SUB_MAILBOXES) {
// too many paths
return helpers.message(`The mailbox path cannot be more than ${MAX_SUB_MAILBOXES} levels deep`);
}

for (const pathPart of splittedPathArr) {
if (pathPart.length > MAX_MAILBOX_NAME_LENGTH) {
// part too long error
return helpers.message(`Any part of the mailbox path cannot be longer than ${MAX_MAILBOX_NAME_LENGTH} chars long`);
}
}

return value;
}, 'Mailbox path validation')
.custom(mailboxPathValidator, 'Mailbox path validation')
.description('Full path of the mailbox, use this to rename an existing Mailbox'),
retention: Joi.number()
.empty('')
Expand Down
22 changes: 21 additions & 1 deletion lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const EJSON = require('mongodb-extended-json');
const Joi = require('joi');
const { MAX_SUB_MAILBOXES, MAX_MAILBOX_NAME_LENGTH } = require('./consts');

const sessSchema = Joi.string().max(255).label('Session identifier').description('Session identifier for the logs');
const sessIPSchema = Joi.string()
Expand Down Expand Up @@ -82,6 +83,24 @@ const metaDataValidator = () => (value, helpers) => {
return strValue;
};

const mailboxPathValidator = (value, helpers) => {
const splittedPathArr = value.split('/');

if (splittedPathArr.length > MAX_SUB_MAILBOXES) {
// too many paths
return helpers.message(`The mailbox path cannot be more than ${MAX_SUB_MAILBOXES} levels deep`);
}

for (const pathPart of splittedPathArr) {
if (pathPart.length > MAX_MAILBOX_NAME_LENGTH) {
// part too long error
return helpers.message(`Any part of the mailbox path cannot be longer than ${MAX_MAILBOX_NAME_LENGTH} chars long`);
}
}

return value;
};

const mongoCursorSchema = Joi.string().trim().empty('').custom(mongoCursorValidator({}), 'Cursor validation').max(1024);
const pageLimitSchema = Joi.number().default(20).min(1).max(250).label('Page size');
const pageNrSchema = Joi.number().default(1).label('Page number').description('Current page number. Informational only, page numbers start from 1');
Expand Down Expand Up @@ -111,5 +130,6 @@ module.exports = {
pageLimitSchema,
booleanSchema,
metaDataSchema,
usernameSchema
usernameSchema,
mailboxPathValidator
};

0 comments on commit b09ef4e

Please sign in to comment.