aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r--server/helpers/custom-validators/index.js19
-rw-r--r--server/helpers/custom-validators/index.ts6
-rw-r--r--server/helpers/custom-validators/misc.ts (renamed from server/helpers/custom-validators/misc.js)12
-rw-r--r--server/helpers/custom-validators/pods.ts (renamed from server/helpers/custom-validators/pods.js)20
-rw-r--r--server/helpers/custom-validators/remote/index.js11
-rw-r--r--server/helpers/custom-validators/remote/index.ts1
-rw-r--r--server/helpers/custom-validators/remote/videos.js118
-rw-r--r--server/helpers/custom-validators/remote/videos.ts138
-rw-r--r--server/helpers/custom-validators/users.ts (renamed from server/helpers/custom-validators/users.js)28
-rw-r--r--server/helpers/custom-validators/videos.ts (renamed from server/helpers/custom-validators/videos.js)95
10 files changed, 221 insertions, 227 deletions
diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js
deleted file mode 100644
index 9383e0304..000000000
--- a/server/helpers/custom-validators/index.js
+++ /dev/null
@@ -1,19 +0,0 @@
1'use strict'
2
3const miscValidators = require('./misc')
4const podsValidators = require('./pods')
5const remoteValidators = require('./remote')
6const usersValidators = require('./users')
7const videosValidators = require('./videos')
8
9const validators = {
10 misc: miscValidators,
11 pods: podsValidators,
12 remote: remoteValidators,
13 users: usersValidators,
14 videos: videosValidators
15}
16
17// ---------------------------------------------------------------------------
18
19module.exports = validators
diff --git a/server/helpers/custom-validators/index.ts b/server/helpers/custom-validators/index.ts
new file mode 100644
index 000000000..1dcab624a
--- /dev/null
+++ b/server/helpers/custom-validators/index.ts
@@ -0,0 +1,6 @@
1export * from './remote'
2export * from './misc'
3export * from './pods'
4export * from './pods'
5export * from './users'
6export * from './videos'
diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.ts
index 052726241..83f50a7fe 100644
--- a/server/helpers/custom-validators/misc.js
+++ b/server/helpers/custom-validators/misc.ts
@@ -1,10 +1,3 @@
1'use strict'
2
3const miscValidators = {
4 exists,
5 isArray
6}
7
8function exists (value) { 1function exists (value) {
9 return value !== undefined && value !== null 2 return value !== undefined && value !== null
10} 3}
@@ -15,4 +8,7 @@ function isArray (value) {
15 8
16// --------------------------------------------------------------------------- 9// ---------------------------------------------------------------------------
17 10
18module.exports = miscValidators 11export {
12 exists,
13 isArray
14}
diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.ts
index 8bb3733ff..e4c827feb 100644
--- a/server/helpers/custom-validators/pods.js
+++ b/server/helpers/custom-validators/pods.ts
@@ -1,20 +1,15 @@
1'use strict' 1import expressValidator = require('express-validator')
2// TODO: use .validator when express-validator typing will have validator field
3const validator = expressValidator['validator']
2 4
3const validator = require('express-validator').validator 5import { isArray } from './misc'
4
5const miscValidators = require('./misc')
6
7const podsValidators = {
8 isEachUniqueHostValid,
9 isHostValid
10}
11 6
12function isHostValid (host) { 7function isHostValid (host) {
13 return validator.isURL(host) && host.split('://').length === 1 8 return validator.isURL(host) && host.split('://').length === 1
14} 9}
15 10
16function isEachUniqueHostValid (hosts) { 11function isEachUniqueHostValid (hosts) {
17 return miscValidators.isArray(hosts) && 12 return isArray(hosts) &&
18 hosts.length !== 0 && 13 hosts.length !== 0 &&
19 hosts.every(function (host) { 14 hosts.every(function (host) {
20 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) 15 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
@@ -23,4 +18,7 @@ function isEachUniqueHostValid (hosts) {
23 18
24// --------------------------------------------------------------------------- 19// ---------------------------------------------------------------------------
25 20
26module.exports = podsValidators 21export {
22 isEachUniqueHostValid,
23 isHostValid
24}
diff --git a/server/helpers/custom-validators/remote/index.js b/server/helpers/custom-validators/remote/index.js
deleted file mode 100644
index 1939a95f4..000000000
--- a/server/helpers/custom-validators/remote/index.js
+++ /dev/null
@@ -1,11 +0,0 @@
1'use strict'
2
3const remoteVideosValidators = require('./videos')
4
5const validators = {
6 videos: remoteVideosValidators
7}
8
9// ---------------------------------------------------------------------------
10
11module.exports = validators
diff --git a/server/helpers/custom-validators/remote/index.ts b/server/helpers/custom-validators/remote/index.ts
new file mode 100644
index 000000000..d6f9a7e77
--- /dev/null
+++ b/server/helpers/custom-validators/remote/index.ts
@@ -0,0 +1 @@
export * from './videos';
diff --git a/server/helpers/custom-validators/remote/videos.js b/server/helpers/custom-validators/remote/videos.js
deleted file mode 100644
index 24715b4b3..000000000
--- a/server/helpers/custom-validators/remote/videos.js
+++ /dev/null
@@ -1,118 +0,0 @@
1'use strict'
2
3const has = require('lodash/has')
4const values = require('lodash/values')
5
6const constants = require('../../../initializers/constants')
7const videosValidators = require('../videos')
8const miscValidators = require('../misc')
9
10const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
11
12const remoteVideosValidators = {
13 isEachRemoteRequestVideosValid,
14 isEachRemoteRequestVideosQaduValid,
15 isEachRemoteRequestVideosEventsValid
16}
17
18function isEachRemoteRequestVideosValid (requests) {
19 return miscValidators.isArray(requests) &&
20 requests.every(function (request) {
21 const video = request.data
22
23 if (!video) return false
24
25 return (
26 isRequestTypeAddValid(request.type) &&
27 isCommonVideoAttributesValid(video) &&
28 videosValidators.isVideoAuthorValid(video.author) &&
29 videosValidators.isVideoThumbnailDataValid(video.thumbnailData)
30 ) ||
31 (
32 isRequestTypeUpdateValid(request.type) &&
33 isCommonVideoAttributesValid(video)
34 ) ||
35 (
36 isRequestTypeRemoveValid(request.type) &&
37 videosValidators.isVideoRemoteIdValid(video.remoteId)
38 ) ||
39 (
40 isRequestTypeReportAbuseValid(request.type) &&
41 videosValidators.isVideoRemoteIdValid(request.data.videoRemoteId) &&
42 videosValidators.isVideoAbuseReasonValid(request.data.reportReason) &&
43 videosValidators.isVideoAbuseReporterUsernameValid(request.data.reporterUsername)
44 )
45 })
46}
47
48function isEachRemoteRequestVideosQaduValid (requests) {
49 return miscValidators.isArray(requests) &&
50 requests.every(function (request) {
51 const video = request.data
52
53 if (!video) return false
54
55 return (
56 videosValidators.isVideoRemoteIdValid(video.remoteId) &&
57 (has(video, 'views') === false || videosValidators.isVideoViewsValid) &&
58 (has(video, 'likes') === false || videosValidators.isVideoLikesValid) &&
59 (has(video, 'dislikes') === false || videosValidators.isVideoDislikesValid)
60 )
61 })
62}
63
64function isEachRemoteRequestVideosEventsValid (requests) {
65 return miscValidators.isArray(requests) &&
66 requests.every(function (request) {
67 const eventData = request.data
68
69 if (!eventData) return false
70
71 return (
72 videosValidators.isVideoRemoteIdValid(eventData.remoteId) &&
73 values(constants.REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
74 videosValidators.isVideoEventCountValid(eventData.count)
75 )
76 })
77}
78
79// ---------------------------------------------------------------------------
80
81module.exports = remoteVideosValidators
82
83// ---------------------------------------------------------------------------
84
85function isCommonVideoAttributesValid (video) {
86 return videosValidators.isVideoDateValid(video.createdAt) &&
87 videosValidators.isVideoDateValid(video.updatedAt) &&
88 videosValidators.isVideoCategoryValid(video.category) &&
89 videosValidators.isVideoLicenceValid(video.licence) &&
90 videosValidators.isVideoLanguageValid(video.language) &&
91 videosValidators.isVideoNSFWValid(video.nsfw) &&
92 videosValidators.isVideoDescriptionValid(video.description) &&
93 videosValidators.isVideoDurationValid(video.duration) &&
94 videosValidators.isVideoInfoHashValid(video.infoHash) &&
95 videosValidators.isVideoNameValid(video.name) &&
96 videosValidators.isVideoTagsValid(video.tags) &&
97 videosValidators.isVideoRemoteIdValid(video.remoteId) &&
98 videosValidators.isVideoExtnameValid(video.extname) &&
99 videosValidators.isVideoViewsValid(video.views) &&
100 videosValidators.isVideoLikesValid(video.likes) &&
101 videosValidators.isVideoDislikesValid(video.dislikes)
102}
103
104function isRequestTypeAddValid (value) {
105 return value === ENDPOINT_ACTIONS.ADD
106}
107
108function isRequestTypeUpdateValid (value) {
109 return value === ENDPOINT_ACTIONS.UPDATE
110}
111
112function isRequestTypeRemoveValid (value) {
113 return value === ENDPOINT_ACTIONS.REMOVE
114}
115
116function isRequestTypeReportAbuseValid (value) {
117 return value === ENDPOINT_ACTIONS.REPORT_ABUSE
118}
diff --git a/server/helpers/custom-validators/remote/videos.ts b/server/helpers/custom-validators/remote/videos.ts
new file mode 100644
index 000000000..4b904d011
--- /dev/null
+++ b/server/helpers/custom-validators/remote/videos.ts
@@ -0,0 +1,138 @@
1import { has, values } from 'lodash'
2
3import {
4 REQUEST_ENDPOINTS,
5 REQUEST_ENDPOINT_ACTIONS,
6 REQUEST_VIDEO_EVENT_TYPES
7} from '../../../initializers'
8import { isArray } from '../misc'
9import {
10 isVideoAuthorValid,
11 isVideoThumbnailDataValid,
12 isVideoRemoteIdValid,
13 isVideoAbuseReasonValid,
14 isVideoAbuseReporterUsernameValid,
15 isVideoViewsValid,
16 isVideoLikesValid,
17 isVideoDislikesValid,
18 isVideoEventCountValid,
19 isVideoDateValid,
20 isVideoCategoryValid,
21 isVideoLicenceValid,
22 isVideoLanguageValid,
23 isVideoNSFWValid,
24 isVideoDescriptionValid,
25 isVideoDurationValid,
26 isVideoInfoHashValid,
27 isVideoNameValid,
28 isVideoTagsValid,
29 isVideoExtnameValid
30} from '../videos'
31
32const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
33
34function isEachRemoteRequestVideosValid (requests) {
35 return isArray(requests) &&
36 requests.every(function (request) {
37 const video = request.data
38
39 if (!video) return false
40
41 return (
42 isRequestTypeAddValid(request.type) &&
43 isCommonVideoAttributesValid(video) &&
44 isVideoAuthorValid(video.author) &&
45 isVideoThumbnailDataValid(video.thumbnailData)
46 ) ||
47 (
48 isRequestTypeUpdateValid(request.type) &&
49 isCommonVideoAttributesValid(video)
50 ) ||
51 (
52 isRequestTypeRemoveValid(request.type) &&
53 isVideoRemoteIdValid(video.remoteId)
54 ) ||
55 (
56 isRequestTypeReportAbuseValid(request.type) &&
57 isVideoRemoteIdValid(request.data.videoRemoteId) &&
58 isVideoAbuseReasonValid(request.data.reportReason) &&
59 isVideoAbuseReporterUsernameValid(request.data.reporterUsername)
60 )
61 })
62}
63
64function isEachRemoteRequestVideosQaduValid (requests) {
65 return isArray(requests) &&
66 requests.every(function (request) {
67 const video = request.data
68
69 if (!video) return false
70
71 return (
72 isVideoRemoteIdValid(video.remoteId) &&
73 (has(video, 'views') === false || isVideoViewsValid) &&
74 (has(video, 'likes') === false || isVideoLikesValid) &&
75 (has(video, 'dislikes') === false || isVideoDislikesValid)
76 )
77 })
78}
79
80function isEachRemoteRequestVideosEventsValid (requests) {
81 return isArray(requests) &&
82 requests.every(function (request) {
83 const eventData = request.data
84
85 if (!eventData) return false
86
87 return (
88 isVideoRemoteIdValid(eventData.remoteId) &&
89 values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
90 isVideoEventCountValid(eventData.count)
91 )
92 })
93}
94
95// ---------------------------------------------------------------------------
96
97export {
98 isEachRemoteRequestVideosValid,
99 isEachRemoteRequestVideosQaduValid,
100 isEachRemoteRequestVideosEventsValid
101}
102
103// ---------------------------------------------------------------------------
104
105function isCommonVideoAttributesValid (video) {
106 return isVideoDateValid(video.createdAt) &&
107 isVideoDateValid(video.updatedAt) &&
108 isVideoCategoryValid(video.category) &&
109 isVideoLicenceValid(video.licence) &&
110 isVideoLanguageValid(video.language) &&
111 isVideoNSFWValid(video.nsfw) &&
112 isVideoDescriptionValid(video.description) &&
113 isVideoDurationValid(video.duration) &&
114 isVideoInfoHashValid(video.infoHash) &&
115 isVideoNameValid(video.name) &&
116 isVideoTagsValid(video.tags) &&
117 isVideoRemoteIdValid(video.remoteId) &&
118 isVideoExtnameValid(video.extname) &&
119 isVideoViewsValid(video.views) &&
120 isVideoLikesValid(video.likes) &&
121 isVideoDislikesValid(video.dislikes)
122}
123
124function isRequestTypeAddValid (value) {
125 return value === ENDPOINT_ACTIONS.ADD
126}
127
128function isRequestTypeUpdateValid (value) {
129 return value === ENDPOINT_ACTIONS.UPDATE
130}
131
132function isRequestTypeRemoveValid (value) {
133 return value === ENDPOINT_ACTIONS.REMOVE
134}
135
136function isRequestTypeReportAbuseValid (value) {
137 return value === ENDPOINT_ACTIONS.REPORT_ABUSE
138}
diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.ts
index 2fc026e98..8fd2dac4f 100644
--- a/server/helpers/custom-validators/users.js
+++ b/server/helpers/custom-validators/users.ts
@@ -1,24 +1,17 @@
1'use strict' 1import { values } from 'lodash'
2import expressValidator = require('express-validator')
3// TODO: use .validator when express-validator typing will have validator field
4const validator = expressValidator['validator']
2 5
3const validator = require('express-validator').validator 6import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
4const values = require('lodash/values') 7const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
5
6const constants = require('../../initializers/constants')
7const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
8
9const usersValidators = {
10 isUserPasswordValid,
11 isUserRoleValid,
12 isUserUsernameValid,
13 isUserDisplayNSFWValid
14}
15 8
16function isUserPasswordValid (value) { 9function isUserPasswordValid (value) {
17 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) 10 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
18} 11}
19 12
20function isUserRoleValid (value) { 13function isUserRoleValid (value) {
21 return values(constants.USER_ROLES).indexOf(value) !== -1 14 return values(USER_ROLES).indexOf(value) !== -1
22} 15}
23 16
24function isUserUsernameValid (value) { 17function isUserUsernameValid (value) {
@@ -33,4 +26,9 @@ function isUserDisplayNSFWValid (value) {
33 26
34// --------------------------------------------------------------------------- 27// ---------------------------------------------------------------------------
35 28
36module.exports = usersValidators 29export {
30 isUserPasswordValid,
31 isUserRoleValid,
32 isUserUsernameValid,
33 isUserDisplayNSFWValid
34}
diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.ts
index 196731e04..2b2370be4 100644
--- a/server/helpers/custom-validators/videos.js
+++ b/server/helpers/custom-validators/videos.ts
@@ -1,43 +1,24 @@
1'use strict' 1import { values } from 'lodash'
2 2import expressValidator = require('express-validator')
3const validator = require('express-validator').validator 3// TODO: use .validator when express-validator typing will have validator field
4const values = require('lodash/values') 4const validator = expressValidator['validator']
5 5
6const constants = require('../../initializers/constants') 6import {
7const usersValidators = require('./users') 7 CONSTRAINTS_FIELDS,
8const miscValidators = require('./misc') 8 VIDEO_CATEGORIES,
9const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS 9 VIDEO_LICENCES,
10const VIDEO_ABUSES_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_ABUSES 10 VIDEO_LANGUAGES,
11const VIDEO_EVENTS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_EVENTS 11 VIDEO_RATE_TYPES
12 12} from '../../initializers'
13const videosValidators = { 13import { isUserUsernameValid } from './users'
14 isVideoAuthorValid, 14import { isArray } from './misc'
15 isVideoDateValid, 15
16 isVideoCategoryValid, 16const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
17 isVideoLicenceValid, 17const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
18 isVideoLanguageValid, 18const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
19 isVideoNSFWValid,
20 isVideoDescriptionValid,
21 isVideoDurationValid,
22 isVideoInfoHashValid,
23 isVideoNameValid,
24 isVideoTagsValid,
25 isVideoThumbnailValid,
26 isVideoThumbnailDataValid,
27 isVideoExtnameValid,
28 isVideoRemoteIdValid,
29 isVideoAbuseReasonValid,
30 isVideoAbuseReporterUsernameValid,
31 isVideoFile,
32 isVideoViewsValid,
33 isVideoLikesValid,
34 isVideoRatingTypeValid,
35 isVideoDislikesValid,
36 isVideoEventCountValid
37}
38 19
39function isVideoAuthorValid (value) { 20function isVideoAuthorValid (value) {
40 return usersValidators.isUserUsernameValid(value) 21 return isUserUsernameValid(value)
41} 22}
42 23
43function isVideoDateValid (value) { 24function isVideoDateValid (value) {
@@ -45,15 +26,15 @@ function isVideoDateValid (value) {
45} 26}
46 27
47function isVideoCategoryValid (value) { 28function isVideoCategoryValid (value) {
48 return constants.VIDEO_CATEGORIES[value] !== undefined 29 return VIDEO_CATEGORIES[value] !== undefined
49} 30}
50 31
51function isVideoLicenceValid (value) { 32function isVideoLicenceValid (value) {
52 return constants.VIDEO_LICENCES[value] !== undefined 33 return VIDEO_LICENCES[value] !== undefined
53} 34}
54 35
55function isVideoLanguageValid (value) { 36function isVideoLanguageValid (value) {
56 return value === null || constants.VIDEO_LANGUAGES[value] !== undefined 37 return value === null || VIDEO_LANGUAGES[value] !== undefined
57} 38}
58 39
59function isVideoNSFWValid (value) { 40function isVideoNSFWValid (value) {
@@ -81,7 +62,7 @@ function isVideoNameValid (value) {
81} 62}
82 63
83function isVideoTagsValid (tags) { 64function isVideoTagsValid (tags) {
84 return miscValidators.isArray(tags) && 65 return isArray(tags) &&
85 validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && 66 validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
86 tags.every(function (tag) { 67 tags.every(function (tag) {
87 return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) 68 return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
@@ -105,7 +86,7 @@ function isVideoAbuseReasonValid (value) {
105} 86}
106 87
107function isVideoAbuseReporterUsernameValid (value) { 88function isVideoAbuseReporterUsernameValid (value) {
108 return usersValidators.isUserUsernameValid(value) 89 return isUserUsernameValid(value)
109} 90}
110 91
111function isVideoViewsValid (value) { 92function isVideoViewsValid (value) {
@@ -125,7 +106,7 @@ function isVideoEventCountValid (value) {
125} 106}
126 107
127function isVideoRatingTypeValid (value) { 108function isVideoRatingTypeValid (value) {
128 return values(constants.VIDEO_RATE_TYPES).indexOf(value) !== -1 109 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
129} 110}
130 111
131function isVideoFile (value, files) { 112function isVideoFile (value, files) {
@@ -145,4 +126,28 @@ function isVideoFile (value, files) {
145 126
146// --------------------------------------------------------------------------- 127// ---------------------------------------------------------------------------
147 128
148module.exports = videosValidators 129export {
130 isVideoAuthorValid,
131 isVideoDateValid,
132 isVideoCategoryValid,
133 isVideoLicenceValid,
134 isVideoLanguageValid,
135 isVideoNSFWValid,
136 isVideoDescriptionValid,
137 isVideoDurationValid,
138 isVideoInfoHashValid,
139 isVideoNameValid,
140 isVideoTagsValid,
141 isVideoThumbnailValid,
142 isVideoThumbnailDataValid,
143 isVideoExtnameValid,
144 isVideoRemoteIdValid,
145 isVideoAbuseReasonValid,
146 isVideoAbuseReporterUsernameValid,
147 isVideoFile,
148 isVideoViewsValid,
149 isVideoLikesValid,
150 isVideoRatingTypeValid,
151 isVideoDislikesValid,
152 isVideoEventCountValid
153}