]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/search.ts
Add ability to search by uuids/actor names
[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 { areUUIDsValid, isDateValid, toCompleteUUIDs } 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')
31 .optional()
32 .isInt().withMessage('Should have a valid min duration'),
33 query('durationMax')
34 .optional()
35 .isInt().withMessage('Should have a valid max duration'),
36
37 query('uuids')
38 .optional()
39 .toArray()
40 .customSanitizer(toCompleteUUIDs)
41 .custom(areUUIDsValid).withMessage('Should have valid uuids'),
42
43 query('searchTarget').optional().custom(isSearchTargetValid).withMessage('Should have a valid search target'),
44
45 (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking videos search query', { parameters: req.query })
47
48 if (areValidationErrors(req, res)) return
49
50 return next()
51 }
52 ]
53
54 const videoChannelsListSearchValidator = [
55 query('search')
56 .optional()
57 .not().isEmpty().withMessage('Should have a valid search'),
58
59 query('host')
60 .optional()
61 .custom(isHostValid).withMessage('Should have a valid host'),
62
63 query('searchTarget')
64 .optional()
65 .custom(isSearchTargetValid).withMessage('Should have a valid search target'),
66
67 query('names')
68 .optional()
69 .toArray(),
70
71 (req: express.Request, res: express.Response, next: express.NextFunction) => {
72 logger.debug('Checking video channels search query', { parameters: req.query })
73
74 if (areValidationErrors(req, res)) return
75
76 return next()
77 }
78 ]
79
80 const videoPlaylistsListSearchValidator = [
81 query('search')
82 .optional()
83 .not().isEmpty().withMessage('Should have a valid search'),
84
85 query('host')
86 .optional()
87 .custom(isHostValid).withMessage('Should have a valid host'),
88
89 query('searchTarget')
90 .optional()
91 .custom(isSearchTargetValid).withMessage('Should have a valid search target'),
92
93 query('uuids')
94 .optional()
95 .toArray()
96 .customSanitizer(toCompleteUUIDs)
97 .custom(areUUIDsValid).withMessage('Should have valid uuids'),
98
99 (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking video playlists search query', { parameters: req.query })
101
102 if (areValidationErrors(req, res)) return
103
104 return next()
105 }
106 ]
107
108 // ---------------------------------------------------------------------------
109
110 export {
111 videosSearchValidator,
112 videoChannelsListSearchValidator,
113 videoPlaylistsListSearchValidator
114 }