]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const validator = require('express-validator').validator | |
4 | ||
5 | const constants = require('../../initializers/constants') | |
6 | const usersValidators = require('./users') | |
7 | const miscValidators = require('./misc') | |
8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS | |
9 | ||
10 | const videosValidators = { | |
11 | isEachRemoteVideosValid, | |
12 | isVideoAuthorValid, | |
13 | isVideoDateValid, | |
14 | isVideoDescriptionValid, | |
15 | isVideoDurationValid, | |
16 | isVideoInfoHashValid, | |
17 | isVideoNameValid, | |
18 | isVideoTagsValid, | |
19 | isVideoThumbnailValid, | |
20 | isVideoThumbnailDataValid | |
21 | } | |
22 | ||
23 | function isEachRemoteVideosValid (requests) { | |
24 | return miscValidators.isArray(requests) && | |
25 | requests.every(function (request) { | |
26 | const video = request.data | |
27 | return ( | |
28 | isRequestTypeAddValid(request.type) && | |
29 | isVideoAuthorValid(video.author) && | |
30 | isVideoDateValid(video.createdAt) && | |
31 | isVideoDateValid(video.updatedAt) && | |
32 | isVideoDescriptionValid(video.description) && | |
33 | isVideoDurationValid(video.duration) && | |
34 | isVideoInfoHashValid(video.infoHash) && | |
35 | isVideoNameValid(video.name) && | |
36 | isVideoTagsValid(video.tags) && | |
37 | isVideoThumbnailDataValid(video.thumbnailData) && | |
38 | isVideoRemoteIdValid(video.remoteId) && | |
39 | isVideoExtnameValid(video.extname) | |
40 | ) || | |
41 | ( | |
42 | isRequestTypeUpdateValid(request.type) && | |
43 | isVideoDateValid(video.createdAt) && | |
44 | isVideoDateValid(video.updatedAt) && | |
45 | isVideoDescriptionValid(video.description) && | |
46 | isVideoDurationValid(video.duration) && | |
47 | isVideoInfoHashValid(video.infoHash) && | |
48 | isVideoNameValid(video.name) && | |
49 | isVideoTagsValid(video.tags) && | |
50 | isVideoRemoteIdValid(video.remoteId) && | |
51 | isVideoExtnameValid(video.extname) | |
52 | ) || | |
53 | ( | |
54 | isRequestTypeRemoveValid(request.type) && | |
55 | isVideoNameValid(video.name) && | |
56 | isVideoRemoteIdValid(video.remoteId) | |
57 | ) | |
58 | }) | |
59 | } | |
60 | ||
61 | function isVideoAuthorValid (value) { | |
62 | return usersValidators.isUserUsernameValid(value) | |
63 | } | |
64 | ||
65 | function isVideoDateValid (value) { | |
66 | return validator.isDate(value) | |
67 | } | |
68 | ||
69 | function isVideoDescriptionValid (value) { | |
70 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | |
71 | } | |
72 | ||
73 | function isVideoDurationValid (value) { | |
74 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | |
75 | } | |
76 | ||
77 | function isVideoExtnameValid (value) { | |
78 | return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 | |
79 | } | |
80 | ||
81 | function isVideoInfoHashValid (value) { | |
82 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | |
83 | } | |
84 | ||
85 | function isVideoNameValid (value) { | |
86 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | |
87 | } | |
88 | ||
89 | function isVideoTagsValid (tags) { | |
90 | return miscValidators.isArray(tags) && | |
91 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | |
92 | tags.every(function (tag) { | |
93 | return validator.isAlphanumeric(tag) && | |
94 | validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | |
95 | }) | |
96 | } | |
97 | ||
98 | function isVideoThumbnailValid (value) { | |
99 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | |
100 | } | |
101 | ||
102 | function isVideoThumbnailDataValid (value) { | |
103 | return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) | |
104 | } | |
105 | ||
106 | function isVideoRemoteIdValid (value) { | |
107 | return validator.isUUID(value, 4) | |
108 | } | |
109 | ||
110 | // --------------------------------------------------------------------------- | |
111 | ||
112 | module.exports = videosValidators | |
113 | ||
114 | // --------------------------------------------------------------------------- | |
115 | ||
116 | function isRequestTypeAddValid (value) { | |
117 | return value === 'add' | |
118 | } | |
119 | ||
120 | function isRequestTypeUpdateValid (value) { | |
121 | return value === 'update' | |
122 | } | |
123 | ||
124 | function isRequestTypeRemoveValid (value) { | |
125 | return value === 'remove' | |
126 | } |