]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-playlists.ts
Add playlist rest tests
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-playlists.ts
CommitLineData
418d092a
C
1import { exists } from './misc'
2import * as validator from 'validator'
df0b219d 3import { CONSTRAINTS_FIELDS, VIDEO_PLAYLIST_PRIVACIES, VIDEO_PLAYLIST_TYPES } from '../../initializers'
418d092a
C
4import * as express from 'express'
5import { VideoPlaylistModel } from '../../models/video/video-playlist'
418d092a
C
6
7const PLAYLISTS_CONSTRAINT_FIELDS = CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS
8
9function isVideoPlaylistNameValid (value: any) {
10 return exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.NAME)
11}
12
13function isVideoPlaylistDescriptionValid (value: any) {
14 return value === null || (exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.DESCRIPTION))
15}
16
17function isVideoPlaylistPrivacyValid (value: number) {
18 return validator.isInt(value + '') && VIDEO_PLAYLIST_PRIVACIES[ value ] !== undefined
19}
20
df0b219d
C
21function isVideoPlaylistTimestampValid (value: any) {
22 return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
23}
24
25function isVideoPlaylistTypeValid (value: any) {
26 return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined
27}
28
418d092a 29async function isVideoPlaylistExist (id: number | string, res: express.Response) {
df0b219d 30 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)
418d092a
C
31
32 if (!videoPlaylist) {
33 res.status(404)
34 .json({ error: 'Video playlist not found' })
35 .end()
36
37 return false
38 }
39
40 res.locals.videoPlaylist = videoPlaylist
41 return true
42}
43
44// ---------------------------------------------------------------------------
45
46export {
47 isVideoPlaylistExist,
48 isVideoPlaylistNameValid,
49 isVideoPlaylistDescriptionValid,
df0b219d
C
50 isVideoPlaylistPrivacyValid,
51 isVideoPlaylistTimestampValid,
52 isVideoPlaylistTypeValid
418d092a 53}