diff options
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/index.js | 17 | ||||
-rw-r--r-- | server/helpers/custom-validators/misc.js | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/pods.js | 21 | ||||
-rw-r--r-- | server/helpers/custom-validators/users.js | 31 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.js | 107 |
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 | |||
3 | const miscValidators = require('./misc') | ||
4 | const podsValidators = require('./pods') | ||
5 | const usersValidators = require('./users') | ||
6 | const videosValidators = require('./videos') | ||
7 | |||
8 | const validators = { | ||
9 | misc: miscValidators, | ||
10 | pods: podsValidators, | ||
11 | users: usersValidators, | ||
12 | videos: videosValidators | ||
13 | } | ||
14 | |||
15 | // --------------------------------------------------------------------------- | ||
16 | |||
17 | module.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 | |||
3 | const miscValidators = { | ||
4 | exists, | ||
5 | isArray | ||
6 | } | ||
7 | |||
8 | function exists (value) { | ||
9 | return value !== undefined && value !== null | ||
10 | } | ||
11 | |||
12 | function isArray (value) { | ||
13 | return Array.isArray(value) | ||
14 | } | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | module.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 | |||
3 | const validator = require('express-validator').validator | ||
4 | |||
5 | const miscValidators = require('./misc') | ||
6 | |||
7 | const podsValidators = { | ||
8 | isEachUniqueUrlValid | ||
9 | } | ||
10 | |||
11 | function 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 | |||
21 | module.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 | |||
3 | const validator = require('express-validator').validator | ||
4 | const values = require('lodash/values') | ||
5 | |||
6 | const constants = require('../../initializers/constants') | ||
7 | const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS | ||
8 | |||
9 | const usersValidators = { | ||
10 | isUserPasswordValid, | ||
11 | isUserRoleValid, | ||
12 | isUserUsernameValid | ||
13 | } | ||
14 | |||
15 | function isUserPasswordValid (value) { | ||
16 | return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) | ||
17 | } | ||
18 | |||
19 | function isUserRoleValid (value) { | ||
20 | return values(constants.USER_ROLES).indexOf(value) !== -1 | ||
21 | } | ||
22 | |||
23 | function 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 | |||
31 | module.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 | |||
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 | isVideoMagnetUriValid, | ||
17 | isVideoNameValid, | ||
18 | isVideoPodUrlValid, | ||
19 | isVideoTagsValid, | ||
20 | isVideoThumbnailValid, | ||
21 | isVideoThumbnail64Valid | ||
22 | } | ||
23 | |||
24 | function 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 | |||
48 | function isVideoAuthorValid (value) { | ||
49 | return usersValidators.isUserUsernameValid(value) | ||
50 | } | ||
51 | |||
52 | function isVideoDateValid (value) { | ||
53 | return validator.isDate(value) | ||
54 | } | ||
55 | |||
56 | function isVideoDescriptionValid (value) { | ||
57 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
58 | } | ||
59 | |||
60 | function isVideoDurationValid (value) { | ||
61 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
62 | } | ||
63 | |||
64 | function isVideoMagnetUriValid (value) { | ||
65 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) | ||
66 | } | ||
67 | |||
68 | function isVideoNameValid (value) { | ||
69 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
70 | } | ||
71 | |||
72 | function isVideoPodUrlValid (value) { | ||
73 | // TODO: set options (TLD...) | ||
74 | return validator.isURL(value) | ||
75 | } | ||
76 | |||
77 | function 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 | |||
86 | function isVideoThumbnailValid (value) { | ||
87 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
88 | } | ||
89 | |||
90 | function isVideoThumbnail64Valid (value) { | ||
91 | return validator.isBase64(value) && | ||
92 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | module.exports = videosValidators | ||
98 | |||
99 | // --------------------------------------------------------------------------- | ||
100 | |||
101 | function isRequestTypeAddValid (value) { | ||
102 | return value === 'add' | ||
103 | } | ||
104 | |||
105 | function isRequestTypeRemoveValid (value) { | ||
106 | return value === 'remove' | ||
107 | } | ||