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