sepia-search-motore-di-rice.../server/middlewares/validators/search.ts

105 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-05-29 09:13:22 +02:00
import { check } from 'express-validator'
2020-02-14 16:14:45 +01:00
import * as express from 'express'
import { isDateValid, toArray } from '../../helpers/custom-validators/misc'
import { isNSFWQueryValid, isNumberArray, isStringArray } from '../../helpers/custom-validators/search-videos'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
const commonFiltersValidators = [
2020-05-29 09:13:22 +02:00
check('blockedAccounts')
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid blockedAccounts array'),
2020-05-29 09:13:22 +02:00
check('blockedHosts')
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid hosts array'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
2020-05-29 16:16:55 +02:00
logger.debug({ query: req.query, body: req.body }, 'Checking commons filters query')
if (areValidationErrors(req, res)) return
return next()
}
]
2020-02-14 16:14:45 +01:00
const commonVideosFiltersValidator = [
2020-05-29 09:13:22 +02:00
check('categoryOneOf')
2020-02-14 16:14:45 +01:00
.optional()
.customSanitizer(toArray)
.custom(isNumberArray).withMessage('Should have a valid one of category array'),
2020-05-29 09:13:22 +02:00
check('licenceOneOf')
2020-02-14 16:14:45 +01:00
.optional()
.customSanitizer(toArray)
.custom(isNumberArray).withMessage('Should have a valid one of licence array'),
2020-05-29 09:13:22 +02:00
check('languageOneOf')
2020-02-14 16:14:45 +01:00
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid one of language array'),
2020-05-29 09:13:22 +02:00
check('tagsOneOf')
2020-02-14 16:14:45 +01:00
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid one of tags array'),
2020-05-29 09:13:22 +02:00
check('tagsAllOf')
2020-02-14 16:14:45 +01:00
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid all of tags array'),
2020-05-29 09:13:22 +02:00
check('nsfw')
2020-02-14 16:14:45 +01:00
.optional()
.custom(isNSFWQueryValid).withMessage('Should have a valid NSFW attribute'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
return next()
}
]
const videosSearchValidator = [
2020-05-29 09:13:22 +02:00
check('search').optional().not().isEmpty().withMessage('Should have a valid search'),
2020-02-14 16:14:45 +01:00
2020-05-29 09:13:22 +02:00
check('startDate').optional().custom(isDateValid).withMessage('Should have a valid start date'),
check('endDate').optional().custom(isDateValid).withMessage('Should have a valid end date'),
2020-02-14 16:14:45 +01:00
2020-05-29 09:13:22 +02:00
check('originallyPublishedStartDate').optional().custom(isDateValid).withMessage('Should have a valid published start date'),
check('originallyPublishedEndDate').optional().custom(isDateValid).withMessage('Should have a valid published end date'),
2020-02-14 16:14:45 +01:00
2020-05-29 09:13:22 +02:00
check('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
check('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
2020-02-14 16:14:45 +01:00
2020-09-23 11:17:43 +02:00
check('boostLanguages')
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid boostLanguages array'),
2020-02-14 16:14:45 +01:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
2020-05-29 16:16:55 +02:00
logger.debug({ query: req.query, body: req.body }, 'Checking videos search query')
2020-02-14 16:14:45 +01:00
if (areValidationErrors(req, res)) return
return next()
}
]
2020-02-19 15:39:35 +01:00
const videoChannelsSearchValidator = [
2020-05-29 09:13:22 +02:00
check('search').not().isEmpty().withMessage('Should have a valid search'),
2020-02-19 15:39:35 +01:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
2020-05-29 16:16:55 +02:00
logger.debug({ query: req.query, body: req.body }, 'Checking video channels search query')
2020-02-19 15:39:35 +01:00
if (areValidationErrors(req, res)) return
return next()
}
]
2020-02-14 16:14:45 +01:00
// ---------------------------------------------------------------------------
export {
2020-02-19 15:39:35 +01:00
videoChannelsSearchValidator,
commonFiltersValidators,
2020-02-14 16:14:45 +01:00
commonVideosFiltersValidator,
videosSearchValidator
}