From 6ceacdf3ffaa7a436643c692fb6dc11ea3f069f4 Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Sat, 24 Apr 2021 05:47:26 +0000 Subject: [PATCH] ref #356 resign, transfer --- controllers/forms/resign.js | 26 ++++++++------------------ controllers/forms/transfer.js | 20 ++++++-------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/controllers/forms/resign.js b/controllers/forms/resign.js index 67ddc628..7616bd81 100644 --- a/controllers/forms/resign.js +++ b/controllers/forms/resign.js @@ -16,25 +16,15 @@ module.exports = { controller: async (req, res, next) => { - const errors = []; - - if (!req.body.confirm) { - errors.push('Missing confirmation'); - } - if (!req.body.board || req.body.board.length === 0) { - errors.push('You did not select a board'); - } else if (alphaNumericRegex.test(req.body.board) !== true) { - errors.push('URI must contain a-z 0-9 only'); - } else { - try { + const errors = checkSchema([ + { result: existsBody(req.body.confirm), expected: true, error: 'Missing confirmation' }, + { result: lengthBody(req.body.board, 1), expected: true, error: 'You did not select a board' }, + { result: alphaNumericRegex.test(req.body.board), expected: true, error: 'URI must contain a-z 0-9 only' }, + { result: async () => { res.locals.board = await Boards.findOne(req.body.board); - } catch (err) { - return next(err); - } - if (!res.locals.board) { - errors.push(`Board /${req.body.board}/ does not exist`); - } - } + return res.locals.board != null; + }, expected: true, error: `Board /${req.body.board}/ does not exist` }, + ]); if (errors.length > 0) { return dynamicResponse(req, res, 400, 'message', { diff --git a/controllers/forms/transfer.js b/controllers/forms/transfer.js index 195348f7..b1376674 100644 --- a/controllers/forms/transfer.js +++ b/controllers/forms/transfer.js @@ -15,20 +15,12 @@ module.exports = { controller: async (req, res, next) => { - const errors = []; - - if (!req.body.username || req.body.username.length === 0) { - errors.push('Missing transfer username'); - } - if (req.body.username && req.body.username.length > 50) { - errors.push('Transfer username must be 50 characters or less'); - } - if (req.body.username === res.locals.board.owner) { - errors.push('New owner username must not be same as old owner'); - } - if (alphaNumericRegex.test(req.body.username) !== true) { - errors.push('Username must contain a-z 0-9 only'); - } + const errors = checkSchema([ + { result: existsBody(req.body.username), expected: true, error: 'Missing new owner username' }, + { result: lengthBody(req.body.username, 1, 50), expected: true, error: 'New owner username must be 50 characters or less' }, + { result: (req.body.username === res.locals.board.owner), expected: false, error: 'New owner must be different from current owner' }, + { result: alphaNumericRegex.test(req.body.username), expected: true, error: 'New owner username must contain a-z 0-9 only' }, + ]); if (errors.length > 0) { return dynamicResponse(req, res, 400, 'message', {