]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/search.ts
6bb335127a4b0ceeea11728f827709f4f659aef7
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / search.ts
1 import * as express from 'express'
2 import { query } from 'express-validator'
3 import { isSearchTargetValid } from '@server/helpers/custom-validators/search'
4 import { isHostValid } from '@server/helpers/custom-validators/servers'
5 import { isDateValid } from '../../helpers/custom-validators/misc'
6 import { logger } from '../../helpers/logger'
7 import { areValidationErrors } from './shared'
8
9 const videosSearchValidator = [
10 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
11
12 query('host')
13 .optional()
14 .custom(isHostValid).withMessage('Should have a valid host'),
15
16 query('startDate')
17 .optional()
18 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
19 query('endDate')
20 .optional()
21 .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
22
23 query('originallyPublishedStartDate')
24 .optional()
25 .custom(isDateValid).withMessage('Should have a published start date that conforms to ISO 8601'),
26 query('originallyPublishedEndDate')
27 .optional()
28 .custom(isDateValid).withMessage('Should have a published end date that conforms to ISO 8601'),
29
30 query('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
31 query('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
32
33 query('searchTarget').optional().custom(isSearchTargetValid).withMessage('Should have a valid search target'),
34
35 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videos search query', { parameters: req.query })
37
38 if (areValidationErrors(req, res)) return
39
40 return next()
41 }
42 ]
43
44 const videoChannelsListSearchValidator = [
45 query('search').not().isEmpty().withMessage('Should have a valid search'),
46 query('searchTarget').optional().custom(isSearchTargetValid).withMessage('Should have a valid search target'),
47
48 (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking video channels search query', { parameters: req.query })
50
51 if (areValidationErrors(req, res)) return
52
53 return next()
54 }
55 ]
56
57 const videoPlaylistsListSearchValidator = [
58 query('search').not().isEmpty().withMessage('Should have a valid search'),
59 query('searchTarget').optional().custom(isSearchTargetValid).withMessage('Should have a valid search target'),
60
61 (req: express.Request, res: express.Response, next: express.NextFunction) => {
62 logger.debug('Checking video playlists search query', { parameters: req.query })
63
64 if (areValidationErrors(req, res)) return
65
66 return next()
67 }
68 ]
69
70 // ---------------------------------------------------------------------------
71
72 export {
73 videosSearchValidator,
74 videoChannelsListSearchValidator,
75 videoPlaylistsListSearchValidator
76 }