]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/search.ts
Add videos list filters
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / search.ts
CommitLineData
57c36b27
C
1import * as express from 'express'
2import { areValidationErrors } from './utils'
3import { logger } from '../../helpers/logger'
4import { query } from 'express-validator/check'
d525fc39
C
5import { isNumberArray, isStringArray } from '../../helpers/custom-validators/search'
6import { isBooleanValid, isDateValid, toArray } from '../../helpers/custom-validators/misc'
57c36b27
C
7
8const searchValidator = [
9 query('search').not().isEmpty().withMessage('Should have a valid search'),
10
d525fc39
C
11 query('startDate').optional().custom(isDateValid).withMessage('Should have a valid start date'),
12 query('endDate').optional().custom(isDateValid).withMessage('Should have a valid end date'),
13
14 query('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
15 query('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
16
17 (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 logger.debug('Checking search query', { parameters: req.query })
19
20 if (areValidationErrors(req, res)) return
21
22 return next()
23 }
24]
25
26const commonVideosFiltersValidator = [
27 query('categoryOneOf')
28 .optional()
29 .customSanitizer(toArray)
30 .custom(isNumberArray).withMessage('Should have a valid one of category array'),
31 query('licenceOneOf')
32 .optional()
33 .customSanitizer(toArray)
34 .custom(isNumberArray).withMessage('Should have a valid one of licence array'),
35 query('languageOneOf')
36 .optional()
37 .customSanitizer(toArray)
38 .custom(isStringArray).withMessage('Should have a valid one of language array'),
39 query('tagsOneOf')
40 .optional()
41 .customSanitizer(toArray)
42 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
43 query('tagsAllOf')
44 .optional()
45 .customSanitizer(toArray)
46 .custom(isStringArray).withMessage('Should have a valid all of tags array'),
47 query('nsfw')
48 .optional()
49 .toBoolean()
50 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
51
57c36b27 52 (req: express.Request, res: express.Response, next: express.NextFunction) => {
d525fc39 53 logger.debug('Checking commons video filters query', { parameters: req.query })
57c36b27
C
54
55 if (areValidationErrors(req, res)) return
56
57 return next()
58 }
59]
60
61// ---------------------------------------------------------------------------
62
63export {
d525fc39 64 commonVideosFiltersValidator,
57c36b27
C
65 searchValidator
66}