]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/search.ts
Fix redundancy with videos already duplicated with another instance
[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'
0b18f4aa 5import { isNumberArray, isStringArray, isNSFWQueryValid } from '../../helpers/custom-validators/search'
d525fc39 6import { isBooleanValid, isDateValid, toArray } from '../../helpers/custom-validators/misc'
57c36b27 7
f37dc0dd 8const videosSearchValidator = [
d4112450 9 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
57c36b27 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) => {
f37dc0dd
C
18 logger.debug('Checking videos search query', { parameters: req.query })
19
20 if (areValidationErrors(req, res)) return
21
22 return next()
23 }
24]
25
26const videoChannelsSearchValidator = [
27 query('search').not().isEmpty().withMessage('Should have a valid search'),
28
29 (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking video channels search query', { parameters: req.query })
d525fc39
C
31
32 if (areValidationErrors(req, res)) return
33
34 return next()
35 }
36]
37
38const commonVideosFiltersValidator = [
39 query('categoryOneOf')
40 .optional()
41 .customSanitizer(toArray)
42 .custom(isNumberArray).withMessage('Should have a valid one of category array'),
43 query('licenceOneOf')
44 .optional()
45 .customSanitizer(toArray)
46 .custom(isNumberArray).withMessage('Should have a valid one of licence array'),
47 query('languageOneOf')
48 .optional()
49 .customSanitizer(toArray)
50 .custom(isStringArray).withMessage('Should have a valid one of language array'),
51 query('tagsOneOf')
52 .optional()
53 .customSanitizer(toArray)
54 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
55 query('tagsAllOf')
56 .optional()
57 .customSanitizer(toArray)
58 .custom(isStringArray).withMessage('Should have a valid all of tags array'),
59 query('nsfw')
60 .optional()
0b18f4aa 61 .custom(isNSFWQueryValid).withMessage('Should have a valid NSFW attribute'),
d525fc39 62
57c36b27 63 (req: express.Request, res: express.Response, next: express.NextFunction) => {
d525fc39 64 logger.debug('Checking commons video filters query', { parameters: req.query })
57c36b27
C
65
66 if (areValidationErrors(req, res)) return
67
68 return next()
69 }
70]
71
72// ---------------------------------------------------------------------------
73
74export {
d525fc39 75 commonVideosFiltersValidator,
f37dc0dd
C
76 videoChannelsSearchValidator,
77 videosSearchValidator
57c36b27 78}