Skip to content

Commit

Permalink
added advanced route with params
Browse files Browse the repository at this point in the history
separated query validation based on route pathname

cleaned up the files

added validation

cleaned up validator

added oneof in declarations
  • Loading branch information
AmasiaNalbandian committed Dec 16, 2021
1 parent c535234 commit 3925230
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
71 changes: 68 additions & 3 deletions src/api/search/src/bin/validation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { check, validationResult } = require('express-validator');
const { oneOf, check, validationResult } = require('express-validator');

const queryValidationRules = [
// text must be between 1 and 256 and not empty
Expand Down Expand Up @@ -31,8 +31,73 @@ const queryValidationRules = [
.bail(),
];

const validateQuery = (rules) => {
/**
* Advanced search is more flexible, only needs at least ONE field, but can run without any too.
* Date formats must be YYYY-MM-DD
*/
const advancedQueryValidationRules = [
oneOf([
check('postText')
.exists({ checkFalsy: true })
.withMessage('post should not be empty')
.bail()
.isLength({ max: 256, min: 1 })
.withMessage('post should be between 1 to 256 characters')
.bail(),
check('authorText')
.exists({ checkFalsy: true })
.withMessage('author should exist')
.bail()
.isLength({ max: 100, min: 2 })
.withMessage('invalid author value')
.bail(),

check('dateEnd')
.exists({ checkFalsy: true })
.withMessage('dateEnd should exist')
.bail()
.isLength({ max: 100, min: 2 })
.withMessage('invalid author value')
.bail()
.isISO8601()
.withMessage('invalid date format')
.bail(),

check('dateStart')
.exists({ checkFalsy: true })
.withMessage('dateEnd should exist')
.bail()
.isLength({ max: 100, min: 2 })
.withMessage('invalid author value')
.bail()
.isISO8601()
.withMessage('invalid date format')
.bail(),

check('perPage')
.optional()
.isInt({ min: 1, max: 10 })
.withMessage('perPage should be empty or a number between 1 to 10')
.bail(),

check('page')
.optional()
.isInt({ min: 0, max: 999 })
.withMessage('page should be empty or a number between 0 to 999')
.bail(),
]),
];

/**
* Validates query by passing rules. The rules are different based on the pathname
* of the request. If the pathname is '/' it is the basic route.
* Otherwise, if '/advanced/' it is the advanced search
*/
const validateQuery = () => {
return async (req, res, next) => {
const rules =
req._parsedUrl.pathname === '/' ? queryValidationRules : advancedQueryValidationRules;

await Promise.all(rules.map((rule) => rule.run(req)));

const result = validationResult(req);
Expand All @@ -45,4 +110,4 @@ const validateQuery = (rules) => {
};
};

module.exports.validateQuery = validateQuery(queryValidationRules);
module.exports.validateQuery = validateQuery();
14 changes: 13 additions & 1 deletion src/api/search/src/routes/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Router, createError } = require('@senecacdot/satellite');
const { validateQuery } = require('../bin/validation');
const search = require('../search');
const { search, advancedSearch } = require('../search');

const router = Router();

Expand All @@ -13,4 +13,16 @@ router.get('/', validateQuery, async (req, res, next) => {
}
});

// route for advanced
router.get('/advanced', validateQuery, async (req, res, next) => {
try {
const { postText, authorText, titleText, dateStart, dateEnd, page, perPage } = req.query;
res.send(
await advancedSearch(postText, authorText, titleText, dateStart, dateEnd, page, perPage)
);
} catch (error) {
next(createError(503, error));
}
});

module.exports = router;

0 comments on commit 3925230

Please sign in to comment.