aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/videos.ts')
-rw-r--r--server/helpers/custom-validators/videos.ts110
1 files changed, 70 insertions, 40 deletions
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 6389998e1..c5ef4cb5f 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -9,105 +9,105 @@ import {
9 VIDEO_RATE_TYPES 9 VIDEO_RATE_TYPES
10} from '../../initializers' 10} from '../../initializers'
11import { isUserUsernameValid } from './users' 11import { isUserUsernameValid } from './users'
12import { isArray } from './misc' 12import { isArray, exists } from './misc'
13 13
14const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 14const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
15const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 15const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
16const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS 16const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
17 17
18function isVideoAuthorValid (value) { 18function isVideoAuthorValid (value: string) {
19 return isUserUsernameValid(value) 19 return isUserUsernameValid(value)
20} 20}
21 21
22function isVideoDateValid (value) { 22function isVideoDateValid (value: string) {
23 return validator.isDate(value) 23 return exists(value) && validator.isISO8601(value)
24} 24}
25 25
26function isVideoCategoryValid (value) { 26function isVideoCategoryValid (value: number) {
27 return VIDEO_CATEGORIES[value] !== undefined 27 return VIDEO_CATEGORIES[value] !== undefined
28} 28}
29 29
30function isVideoLicenceValid (value) { 30function isVideoLicenceValid (value: number) {
31 return VIDEO_LICENCES[value] !== undefined 31 return VIDEO_LICENCES[value] !== undefined
32} 32}
33 33
34function isVideoLanguageValid (value) { 34function isVideoLanguageValid (value: number) {
35 return value === null || VIDEO_LANGUAGES[value] !== undefined 35 return value === null || VIDEO_LANGUAGES[value] !== undefined
36} 36}
37 37
38function isVideoNSFWValid (value) { 38function isVideoNSFWValid (value: any) {
39 return validator.isBoolean(value) 39 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
40} 40}
41 41
42function isVideoDescriptionValid (value) { 42function isVideoDescriptionValid (value: string) {
43 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) 43 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
44} 44}
45 45
46function isVideoDurationValid (value) { 46function isVideoDurationValid (value: string) {
47 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) 47 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
48} 48}
49 49
50function isVideoExtnameValid (value) { 50function isVideoExtnameValid (value: string) {
51 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 51 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
52} 52}
53 53
54function isVideoInfoHashValid (value) { 54function isVideoInfoHashValid (value: string) {
55 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) 55 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
56} 56}
57 57
58function isVideoNameValid (value) { 58function isVideoNameValid (value: string) {
59 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) 59 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
60} 60}
61 61
62function isVideoTagsValid (tags) { 62function isVideoTagsValid (tags: string[]) {
63 return isArray(tags) && 63 return isArray(tags) &&
64 validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && 64 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
65 tags.every(function (tag) { 65 tags.every(function (tag) {
66 return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) 66 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
67 }) 67 })
68} 68}
69 69
70function isVideoThumbnailValid (value) { 70function isVideoThumbnailValid (value: string) {
71 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) 71 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
72} 72}
73 73
74function isVideoThumbnailDataValid (value) { 74function isVideoThumbnailDataValid (value: string) {
75 return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) 75 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
76} 76}
77 77
78function isVideoRemoteIdValid (value) { 78function isVideoRemoteIdValid (value: string) {
79 return validator.isUUID(value, 4) 79 return exists(value) && validator.isUUID(value, 4)
80} 80}
81 81
82function isVideoAbuseReasonValid (value) { 82function isVideoAbuseReasonValid (value: string) {
83 return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) 83 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
84} 84}
85 85
86function isVideoAbuseReporterUsernameValid (value) { 86function isVideoAbuseReporterUsernameValid (value: string) {
87 return isUserUsernameValid(value) 87 return isUserUsernameValid(value)
88} 88}
89 89
90function isVideoViewsValid (value) { 90function isVideoViewsValid (value: string) {
91 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) 91 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
92} 92}
93 93
94function isVideoLikesValid (value) { 94function isVideoLikesValid (value: string) {
95 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) 95 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
96} 96}
97 97
98function isVideoDislikesValid (value) { 98function isVideoDislikesValid (value: string) {
99 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) 99 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
100} 100}
101 101
102function isVideoEventCountValid (value) { 102function isVideoEventCountValid (value: string) {
103 return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) 103 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
104} 104}
105 105
106function isVideoRatingTypeValid (value) { 106function isVideoRatingTypeValid (value: string) {
107 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1 107 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
108} 108}
109 109
110function isVideoFile (value, files) { 110function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
111 // Should have files 111 // Should have files
112 if (!files) return false 112 if (!files) return false
113 113
@@ -149,3 +149,33 @@ export {
149 isVideoDislikesValid, 149 isVideoDislikesValid,
150 isVideoEventCountValid 150 isVideoEventCountValid
151} 151}
152
153declare global {
154 namespace ExpressValidator {
155 export interface Validator {
156 isVideoAuthorValid,
157 isVideoDateValid,
158 isVideoCategoryValid,
159 isVideoLicenceValid,
160 isVideoLanguageValid,
161 isVideoNSFWValid,
162 isVideoDescriptionValid,
163 isVideoDurationValid,
164 isVideoInfoHashValid,
165 isVideoNameValid,
166 isVideoTagsValid,
167 isVideoThumbnailValid,
168 isVideoThumbnailDataValid,
169 isVideoExtnameValid,
170 isVideoRemoteIdValid,
171 isVideoAbuseReasonValid,
172 isVideoAbuseReporterUsernameValid,
173 isVideoFile,
174 isVideoViewsValid,
175 isVideoLikesValid,
176 isVideoRatingTypeValid,
177 isVideoDislikesValid,
178 isVideoEventCountValid
179 }
180 }
181}