diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-31 20:58:43 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-31 20:58:43 +0200 |
commit | e4c556196d7b31111f17596840d2e1d60caa7dcb (patch) | |
tree | 41be84f002600aa0153ac09cc5d79fdd90d126e3 /server/helpers | |
parent | e62f6ef741c8d14817e321c554796ad64ea7ae1b (diff) | |
download | PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.gz PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.zst PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.zip |
Server: reorganize express validators
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/index.js | 15 | ||||
-rw-r--r-- | server/helpers/custom-validators/misc.js | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/users.js | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.js (renamed from server/helpers/custom-validators.js) | 40 |
4 files changed, 67 insertions, 24 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 | |||
3 | const miscValidators = require('./misc') | ||
4 | const usersValidators = require('./users') | ||
5 | const videosValidators = require('./videos') | ||
6 | |||
7 | const validators = { | ||
8 | misc: miscValidators, | ||
9 | users: usersValidators, | ||
10 | videos: videosValidators | ||
11 | } | ||
12 | |||
13 | // --------------------------------------------------------------------------- | ||
14 | |||
15 | 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..782ae3dee --- /dev/null +++ b/server/helpers/custom-validators/misc.js | |||
@@ -0,0 +1,18 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const miscValidators = { | ||
4 | exists: exists, | ||
5 | isArray: 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/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 | |||
3 | const validator = require('express-validator').validator | ||
4 | |||
5 | const constants = require('../../initializers/constants') | ||
6 | const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS | ||
7 | |||
8 | const usersValidators = { | ||
9 | isUserUsernameValid: isUserUsernameValid | ||
10 | } | ||
11 | |||
12 | function isUserUsernameValid (value) { | ||
13 | return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME) | ||
14 | } | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | module.exports = usersValidators | ||
diff --git a/server/helpers/custom-validators.js b/server/helpers/custom-validators/videos.js index b666644c0..39a19cbd7 100644 --- a/server/helpers/custom-validators.js +++ b/server/helpers/custom-validators/videos.js | |||
@@ -2,13 +2,13 @@ | |||
2 | 2 | ||
3 | const validator = require('express-validator').validator | 3 | const validator = require('express-validator').validator |
4 | 4 | ||
5 | const constants = require('../initializers/constants') | 5 | const constants = require('../../initializers/constants') |
6 | const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS | 6 | const usersValidators = require('./users') |
7 | const miscValidators = require('./misc') | ||
8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS | ||
7 | 9 | ||
8 | const customValidators = { | 10 | const videosValidators = { |
9 | exists: exists, | ||
10 | isEachRemoteVideosValid: isEachRemoteVideosValid, | 11 | isEachRemoteVideosValid: isEachRemoteVideosValid, |
11 | isArray: isArray, | ||
12 | isVideoAuthorValid: isVideoAuthorValid, | 12 | isVideoAuthorValid: isVideoAuthorValid, |
13 | isVideoDateValid: isVideoDateValid, | 13 | isVideoDateValid: isVideoDateValid, |
14 | isVideoDescriptionValid: isVideoDescriptionValid, | 14 | isVideoDescriptionValid: isVideoDescriptionValid, |
@@ -21,10 +21,6 @@ const customValidators = { | |||
21 | isVideoThumbnail64Valid: isVideoThumbnail64Valid | 21 | isVideoThumbnail64Valid: isVideoThumbnail64Valid |
22 | } | 22 | } |
23 | 23 | ||
24 | function exists (value) { | ||
25 | return value !== undefined && value !== null | ||
26 | } | ||
27 | |||
28 | function isEachRemoteVideosValid (requests) { | 24 | function isEachRemoteVideosValid (requests) { |
29 | return requests.every(function (request) { | 25 | return requests.every(function (request) { |
30 | const video = request.data | 26 | const video = request.data |
@@ -48,20 +44,8 @@ function isEachRemoteVideosValid (requests) { | |||
48 | }) | 44 | }) |
49 | } | 45 | } |
50 | 46 | ||
51 | function isArray (value) { | ||
52 | return Array.isArray(value) | ||
53 | } | ||
54 | |||
55 | function isRequestTypeAddValid (value) { | ||
56 | return value === 'add' | ||
57 | } | ||
58 | |||
59 | function isRequestTypeRemoveValid (value) { | ||
60 | return value === 'remove' | ||
61 | } | ||
62 | |||
63 | function isVideoAuthorValid (value) { | 47 | function isVideoAuthorValid (value) { |
64 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) | 48 | return usersValidators.isUserUsernameValid(usersValidators) |
65 | } | 49 | } |
66 | 50 | ||
67 | function isVideoDateValid (value) { | 51 | function isVideoDateValid (value) { |
@@ -90,7 +74,7 @@ function isVideoPodUrlValid (value) { | |||
90 | } | 74 | } |
91 | 75 | ||
92 | function isVideoTagsValid (tags) { | 76 | function isVideoTagsValid (tags) { |
93 | return isArray(tags) && | 77 | return miscValidators.isArray(tags) && |
94 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | 78 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && |
95 | tags.every(function (tag) { | 79 | tags.every(function (tag) { |
96 | return validator.isAlphanumeric(tag) && | 80 | return validator.isAlphanumeric(tag) && |
@@ -109,6 +93,14 @@ function isVideoThumbnail64Valid (value) { | |||
109 | 93 | ||
110 | // --------------------------------------------------------------------------- | 94 | // --------------------------------------------------------------------------- |
111 | 95 | ||
112 | module.exports = customValidators | 96 | module.exports = videosValidators |
113 | 97 | ||
114 | // --------------------------------------------------------------------------- | 98 | // --------------------------------------------------------------------------- |
99 | |||
100 | function isRequestTypeAddValid (value) { | ||
101 | return value === 'add' | ||
102 | } | ||
103 | |||
104 | function isRequestTypeRemoveValid (value) { | ||
105 | return value === 'remove' | ||
106 | } | ||