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/custom-validators | |
parent | e62f6ef741c8d14817e321c554796ad64ea7ae1b (diff) | |
download | PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.gz PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.zst PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.zip |
Server: reorganize express validators
Diffstat (limited to 'server/helpers/custom-validators')
-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 | 106 |
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 | |||
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/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 | |||
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: 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 | |||
24 | function 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 | |||
47 | function isVideoAuthorValid (value) { | ||
48 | return usersValidators.isUserUsernameValid(usersValidators) | ||
49 | } | ||
50 | |||
51 | function isVideoDateValid (value) { | ||
52 | return validator.isDate(value) | ||
53 | } | ||
54 | |||
55 | function isVideoDescriptionValid (value) { | ||
56 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
57 | } | ||
58 | |||
59 | function isVideoDurationValid (value) { | ||
60 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
61 | } | ||
62 | |||
63 | function isVideoMagnetUriValid (value) { | ||
64 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) | ||
65 | } | ||
66 | |||
67 | function isVideoNameValid (value) { | ||
68 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
69 | } | ||
70 | |||
71 | function isVideoPodUrlValid (value) { | ||
72 | // TODO: set options (TLD...) | ||
73 | return validator.isURL(value) | ||
74 | } | ||
75 | |||
76 | function 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 | |||
85 | function isVideoThumbnailValid (value) { | ||
86 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
87 | } | ||
88 | |||
89 | function isVideoThumbnail64Valid (value) { | ||
90 | return validator.isBase64(value) && | ||
91 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
92 | } | ||
93 | |||
94 | // --------------------------------------------------------------------------- | ||
95 | |||
96 | module.exports = videosValidators | ||
97 | |||
98 | // --------------------------------------------------------------------------- | ||
99 | |||
100 | function isRequestTypeAddValid (value) { | ||
101 | return value === 'add' | ||
102 | } | ||
103 | |||
104 | function isRequestTypeRemoveValid (value) { | ||
105 | return value === 'remove' | ||
106 | } | ||