1 import * as Promise from 'bluebird'
2 import * as validator from 'validator'
3 import * as express from 'express'
4 import 'express-validator'
7 import { database as db, CONSTRAINTS_FIELDS } from '../../initializers'
8 import { VideoChannelInstance } from '../../models'
9 import { logger } from '../logger'
10 import { exists } from './misc'
12 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
14 function isVideoChannelDescriptionValid (value: string) {
15 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
18 function isVideoChannelNameValid (value: string) {
19 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
22 function isVideoChannelUUIDValid (value: string) {
23 return exists(value) && validator.isUUID('' + value, 4)
26 function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
27 let promise: Promise<VideoChannelInstance>
28 if (validator.isInt(id)) {
29 promise = db.VideoChannel.loadAndPopulateAuthor(+id)
31 promise = db.VideoChannel.loadByUUIDAndPopulateAuthor(id)
34 promise.then(videoChannel => {
36 return res.status(404)
37 .json({ error: 'Video channel not found' })
41 res.locals.videoChannel = videoChannel
45 logger.error('Error in video channel request validator.', err)
46 return res.sendStatus(500)
50 // ---------------------------------------------------------------------------
53 isVideoChannelDescriptionValid,
54 isVideoChannelNameValid,
55 isVideoChannelUUIDValid,
56 checkVideoChannelExists