diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 17:30:46 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:43:01 +0100 |
commit | a2431b7dcbc72c05101dcdbe631ff84a823aeb51 (patch) | |
tree | 09278a822905622a70ff976a75e09d99bc45639a /server/helpers/custom-validators/video-channels.ts | |
parent | fcaf1e0aa84213a1b1f1b1a44a3276eae35ebe70 (diff) | |
download | PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.gz PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.zst PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.zip |
Refractor validators
Diffstat (limited to 'server/helpers/custom-validators/video-channels.ts')
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 38 |
1 files changed, 2 insertions, 36 deletions
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 267d987fc..3de9f041b 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -1,21 +1,13 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
3 | import 'express-validator' | 2 | import 'express-validator' |
4 | import 'multer' | 3 | import 'multer' |
5 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
6 | |||
7 | import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' | 5 | import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' |
8 | import { VideoChannelInstance } from '../../models' | 6 | import { VideoChannelInstance } from '../../models' |
9 | import { logger } from '../logger' | ||
10 | import { isActivityPubUrlValid } from './index' | ||
11 | import { exists } from './misc' | 7 | import { exists } from './misc' |
12 | 8 | ||
13 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 9 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
14 | 10 | ||
15 | function isVideoChannelUrlValid (value: string) { | ||
16 | return isActivityPubUrlValid(value) | ||
17 | } | ||
18 | |||
19 | function isVideoChannelDescriptionValid (value: string) { | 11 | function isVideoChannelDescriptionValid (value: string) { |
20 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | 12 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) |
21 | } | 13 | } |
@@ -24,31 +16,7 @@ function isVideoChannelNameValid (value: string) { | |||
24 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | 16 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) |
25 | } | 17 | } |
26 | 18 | ||
27 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { | 19 | async function isVideoChannelExist (id: string, res: express.Response) { |
28 | let promise: Bluebird<VideoChannelInstance> | ||
29 | if (validator.isInt(id)) { | ||
30 | promise = db.VideoChannel.loadAndPopulateAccount(+id) | ||
31 | } else { // UUID | ||
32 | promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id) | ||
33 | } | ||
34 | |||
35 | promise.then(videoChannel => { | ||
36 | if (!videoChannel) { | ||
37 | return res.status(404) | ||
38 | .json({ error: 'Video channel not found' }) | ||
39 | .end() | ||
40 | } | ||
41 | |||
42 | res.locals.videoChannel = videoChannel | ||
43 | callback() | ||
44 | }) | ||
45 | .catch(err => { | ||
46 | logger.error('Error in video channel request validator.', err) | ||
47 | return res.sendStatus(500) | ||
48 | }) | ||
49 | } | ||
50 | |||
51 | async function isVideoChannelExistsPromise (id: string, res: express.Response) { | ||
52 | let videoChannel: VideoChannelInstance | 20 | let videoChannel: VideoChannelInstance |
53 | if (validator.isInt(id)) { | 21 | if (validator.isInt(id)) { |
54 | videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) | 22 | videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) |
@@ -72,8 +40,6 @@ async function isVideoChannelExistsPromise (id: string, res: express.Response) { | |||
72 | 40 | ||
73 | export { | 41 | export { |
74 | isVideoChannelDescriptionValid, | 42 | isVideoChannelDescriptionValid, |
75 | checkVideoChannelExists, | ||
76 | isVideoChannelNameValid, | 43 | isVideoChannelNameValid, |
77 | isVideoChannelExistsPromise, | 44 | isVideoChannelExist |
78 | isVideoChannelUrlValid | ||
79 | } | 45 | } |