diff options
Diffstat (limited to 'server/helpers/custom-validators/video-playlists.ts')
-rw-r--r-- | server/helpers/custom-validators/video-playlists.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/video-playlists.ts b/server/helpers/custom-validators/video-playlists.ts new file mode 100644 index 000000000..0f5af4ec0 --- /dev/null +++ b/server/helpers/custom-validators/video-playlists.ts | |||
@@ -0,0 +1,44 @@ | |||
1 | import { exists } from './misc' | ||
2 | import * as validator from 'validator' | ||
3 | import { CONSTRAINTS_FIELDS, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers' | ||
4 | import * as express from 'express' | ||
5 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | ||
6 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | ||
7 | |||
8 | const PLAYLISTS_CONSTRAINT_FIELDS = CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS | ||
9 | |||
10 | function isVideoPlaylistNameValid (value: any) { | ||
11 | return exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.NAME) | ||
12 | } | ||
13 | |||
14 | function isVideoPlaylistDescriptionValid (value: any) { | ||
15 | return value === null || (exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.DESCRIPTION)) | ||
16 | } | ||
17 | |||
18 | function isVideoPlaylistPrivacyValid (value: number) { | ||
19 | return validator.isInt(value + '') && VIDEO_PLAYLIST_PRIVACIES[ value ] !== undefined | ||
20 | } | ||
21 | |||
22 | async function isVideoPlaylistExist (id: number | string, res: express.Response) { | ||
23 | const videoPlaylist = await VideoPlaylistModel.load(id, undefined) | ||
24 | |||
25 | if (!videoPlaylist) { | ||
26 | res.status(404) | ||
27 | .json({ error: 'Video playlist not found' }) | ||
28 | .end() | ||
29 | |||
30 | return false | ||
31 | } | ||
32 | |||
33 | res.locals.videoPlaylist = videoPlaylist | ||
34 | return true | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | export { | ||
40 | isVideoPlaylistExist, | ||
41 | isVideoPlaylistNameValid, | ||
42 | isVideoPlaylistDescriptionValid, | ||
43 | isVideoPlaylistPrivacyValid | ||
44 | } | ||