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.js17
-rw-r--r--server/helpers/custom-validators/misc.js18
-rw-r--r--server/helpers/custom-validators/pods.js21
-rw-r--r--server/helpers/custom-validators/users.js31
-rw-r--r--server/helpers/custom-validators/videos.js107
5 files changed, 194 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js
new file mode 100644
index 000000000..96b5b20b9
--- /dev/null
+++ b/server/helpers/custom-validators/index.js
@@ -0,0 +1,17 @@
1'use strict'
2
3const miscValidators = require('./misc')
4const podsValidators = require('./pods')
5const usersValidators = require('./users')
6const videosValidators = require('./videos')
7
8const validators = {
9 misc: miscValidators,
10 pods: podsValidators,
11 users: usersValidators,
12 videos: videosValidators
13}
14
15// ---------------------------------------------------------------------------
16
17module.exports = validators
diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js
new file mode 100644
index 000000000..052726241
--- /dev/null
+++ b/server/helpers/custom-validators/misc.js
@@ -0,0 +1,18 @@
1'use strict'
2
3const miscValidators = {
4 exists,
5 isArray
6}
7
8function exists (value) {
9 return value !== undefined && value !== null
10}
11
12function isArray (value) {
13 return Array.isArray(value)
14}
15
16// ---------------------------------------------------------------------------
17
18module.exports = miscValidators
diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.js
new file mode 100644
index 000000000..40f8b5d0b
--- /dev/null
+++ b/server/helpers/custom-validators/pods.js
@@ -0,0 +1,21 @@
1'use strict'
2
3const validator = require('express-validator').validator
4
5const miscValidators = require('./misc')
6
7const podsValidators = {
8 isEachUniqueUrlValid
9}
10
11function isEachUniqueUrlValid (urls) {
12 return miscValidators.isArray(urls) &&
13 urls.length !== 0 &&
14 urls.every(function (url) {
15 return validator.isURL(url) && urls.indexOf(url) === urls.lastIndexOf(url)
16 })
17}
18
19// ---------------------------------------------------------------------------
20
21module.exports = podsValidators
diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js
new file mode 100644
index 000000000..88fa1592e
--- /dev/null
+++ b/server/helpers/custom-validators/users.js
@@ -0,0 +1,31 @@
1'use strict'
2
3const validator = require('express-validator').validator
4const values = require('lodash/values')
5
6const constants = require('../../initializers/constants')
7const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
8
9const usersValidators = {
10 isUserPasswordValid,
11 isUserRoleValid,
12 isUserUsernameValid
13}
14
15function isUserPasswordValid (value) {
16 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
17}
18
19function isUserRoleValid (value) {
20 return values(constants.USER_ROLES).indexOf(value) !== -1
21}
22
23function isUserUsernameValid (value) {
24 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
25 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
26 return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
27}
28
29// ---------------------------------------------------------------------------
30
31module.exports = usersValidators
diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js
new file mode 100644
index 000000000..a507ff686
--- /dev/null
+++ b/server/helpers/custom-validators/videos.js
@@ -0,0 +1,107 @@
1'use strict'
2
3const validator = require('express-validator').validator
4
5const constants = require('../../initializers/constants')
6const usersValidators = require('./users')
7const miscValidators = require('./misc')
8const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS
9
10const videosValidators = {
11 isEachRemoteVideosValid,
12 isVideoAuthorValid,
13 isVideoDateValid,
14 isVideoDescriptionValid,
15 isVideoDurationValid,
16 isVideoMagnetUriValid,
17 isVideoNameValid,
18 isVideoPodUrlValid,
19 isVideoTagsValid,
20 isVideoThumbnailValid,
21 isVideoThumbnail64Valid
22}
23
24function isEachRemoteVideosValid (requests) {
25 return miscValidators.isArray(requests) &&
26 requests.every(function (request) {
27 const video = request.data
28 return (
29 isRequestTypeAddValid(request.type) &&
30 isVideoAuthorValid(video.author) &&
31 isVideoDateValid(video.createdDate) &&
32 isVideoDescriptionValid(video.description) &&
33 isVideoDurationValid(video.duration) &&
34 isVideoMagnetUriValid(video.magnetUri) &&
35 isVideoNameValid(video.name) &&
36 isVideoPodUrlValid(video.podUrl) &&
37 isVideoTagsValid(video.tags) &&
38 isVideoThumbnail64Valid(video.thumbnailBase64)
39 ) ||
40 (
41 isRequestTypeRemoveValid(request.type) &&
42 isVideoNameValid(video.name) &&
43 isVideoMagnetUriValid(video.magnetUri)
44 )
45 })
46}
47
48function isVideoAuthorValid (value) {
49 return usersValidators.isUserUsernameValid(value)
50}
51
52function isVideoDateValid (value) {
53 return validator.isDate(value)
54}
55
56function isVideoDescriptionValid (value) {
57 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
58}
59
60function isVideoDurationValid (value) {
61 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
62}
63
64function isVideoMagnetUriValid (value) {
65 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI)
66}
67
68function isVideoNameValid (value) {
69 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
70}
71
72function isVideoPodUrlValid (value) {
73 // TODO: set options (TLD...)
74 return validator.isURL(value)
75}
76
77function isVideoTagsValid (tags) {
78 return miscValidators.isArray(tags) &&
79 validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
80 tags.every(function (tag) {
81 return validator.isAlphanumeric(tag) &&
82 validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
83 })
84}
85
86function isVideoThumbnailValid (value) {
87 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
88}
89
90function isVideoThumbnail64Valid (value) {
91 return validator.isBase64(value) &&
92 validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
93}
94
95// ---------------------------------------------------------------------------
96
97module.exports = videosValidators
98
99// ---------------------------------------------------------------------------
100
101function isRequestTypeAddValid (value) {
102 return value === 'add'
103}
104
105function isRequestTypeRemoveValid (value) {
106 return value === 'remove'
107}