aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-channels.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/video-channels.ts')
-rw-r--r--server/helpers/custom-validators/video-channels.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
new file mode 100644
index 000000000..b6be557e6
--- /dev/null
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -0,0 +1,57 @@
1import * as Promise from 'bluebird'
2import * as validator from 'validator'
3import * as express from 'express'
4import 'express-validator'
5import 'multer'
6
7import { database as db, CONSTRAINTS_FIELDS } from '../../initializers'
8import { VideoChannelInstance } from '../../models'
9import { logger } from '../logger'
10import { exists } from './misc'
11
12const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
13
14function isVideoChannelDescriptionValid (value: string) {
15 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
16}
17
18function isVideoChannelNameValid (value: string) {
19 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
20}
21
22function isVideoChannelUUIDValid (value: string) {
23 return exists(value) && validator.isUUID('' + value, 4)
24}
25
26function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
27 let promise: Promise<VideoChannelInstance>
28 if (validator.isInt(id)) {
29 promise = db.VideoChannel.loadAndPopulateAuthor(+id)
30 } else { // UUID
31 promise = db.VideoChannel.loadByUUIDAndPopulateAuthor(id)
32 }
33
34 promise.then(videoChannel => {
35 if (!videoChannel) {
36 return res.status(404)
37 .json({ error: 'Video channel not found' })
38 .end()
39 }
40
41 res.locals.videoChannel = videoChannel
42 callback()
43 })
44 .catch(err => {
45 logger.error('Error in video channel request validator.', err)
46 return res.sendStatus(500)
47 })
48}
49
50// ---------------------------------------------------------------------------
51
52export {
53 isVideoChannelDescriptionValid,
54 isVideoChannelNameValid,
55 isVideoChannelUUIDValid,
56 checkVideoChannelExists
57}