aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-31 20:58:43 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-31 20:58:43 +0200
commite4c556196d7b31111f17596840d2e1d60caa7dcb (patch)
tree41be84f002600aa0153ac09cc5d79fdd90d126e3 /server/helpers
parente62f6ef741c8d14817e321c554796ad64ea7ae1b (diff)
downloadPeerTube-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.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.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
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.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
3const validator = require('express-validator').validator 3const validator = require('express-validator').validator
4 4
5const constants = require('../initializers/constants') 5const constants = require('../../initializers/constants')
6const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS 6const usersValidators = require('./users')
7const miscValidators = require('./misc')
8const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS
7 9
8const customValidators = { 10const 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
24function exists (value) {
25 return value !== undefined && value !== null
26}
27
28function isEachRemoteVideosValid (requests) { 24function 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
51function isArray (value) {
52 return Array.isArray(value)
53}
54
55function isRequestTypeAddValid (value) {
56 return value === 'add'
57}
58
59function isRequestTypeRemoveValid (value) {
60 return value === 'remove'
61}
62
63function isVideoAuthorValid (value) { 47function isVideoAuthorValid (value) {
64 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) 48 return usersValidators.isUserUsernameValid(usersValidators)
65} 49}
66 50
67function isVideoDateValid (value) { 51function isVideoDateValid (value) {
@@ -90,7 +74,7 @@ function isVideoPodUrlValid (value) {
90} 74}
91 75
92function isVideoTagsValid (tags) { 76function 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
112module.exports = customValidators 96module.exports = videosValidators
113 97
114// --------------------------------------------------------------------------- 98// ---------------------------------------------------------------------------
99
100function isRequestTypeAddValid (value) {
101 return value === 'add'
102}
103
104function isRequestTypeRemoveValid (value) {
105 return value === 'remove'
106}