diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-06-06 14:15:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-06-06 14:15:03 +0200 |
commit | be587647f98a4b83ca06a61fe55c7ac5d60927c6 (patch) | |
tree | 8a28a1cb9c9a06d80803ecd3fa00aa6767bb3f8b /server/helpers/customValidators.js | |
parent | 8483b2216454afdb88f6aa53cad5eecd8c394bc0 (diff) | |
download | PeerTube-be587647f98a4b83ca06a61fe55c7ac5d60927c6.tar.gz PeerTube-be587647f98a4b83ca06a61fe55c7ac5d60927c6.tar.zst PeerTube-be587647f98a4b83ca06a61fe55c7ac5d60927c6.zip |
Add tags support to server
Diffstat (limited to 'server/helpers/customValidators.js')
-rw-r--r-- | server/helpers/customValidators.js | 101 |
1 files changed, 79 insertions, 22 deletions
diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js index 5a0e70ffc..9c3ff38ef 100644 --- a/server/helpers/customValidators.js +++ b/server/helpers/customValidators.js | |||
@@ -1,34 +1,47 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const validator = require('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 | 7 | ||
7 | const customValidators = { | 8 | const customValidators = { |
8 | eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, | 9 | exists: exists, |
9 | eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, | 10 | isEachAddRemoteVideosValid: isEachAddRemoteVideosValid, |
10 | isArray: isArray | 11 | isEachRemoveRemoteVideosValid: isEachRemoveRemoteVideosValid, |
11 | } | 12 | isArray: isArray, |
12 | 13 | isVideoAuthorValid: isVideoAuthorValid, | |
13 | function eachIsRemoteVideosAddValid (values) { | 14 | isVideoDateValid: isVideoDateValid, |
14 | return values.every(function (val) { | 15 | isVideoDescriptionValid: isVideoDescriptionValid, |
15 | return validator.isLength(val.name, 1, 50) && | 16 | isVideoDurationValid: isVideoDurationValid, |
16 | validator.isLength(val.description, 1, 50) && | 17 | isVideoMagnetUriValid: isVideoMagnetUriValid, |
17 | validator.isLength(val.magnetUri, 10) && | 18 | isVideoNameValid: isVideoNameValid, |
18 | validator.isURL(val.podUrl) && | 19 | isVideoPodUrlValid: isVideoPodUrlValid, |
19 | !isNaN(val.duration) && | 20 | isVideoTagsValid: isVideoTagsValid, |
20 | val.duration >= 0 && | 21 | isVideoThumbnailValid: isVideoThumbnailValid |
21 | val.duration < constants.MAXIMUM_VIDEO_DURATION && | 22 | } |
22 | validator.isLength(val.author, 1, constants.MAXIMUM_AUTHOR_LENGTH) && | 23 | |
23 | validator.isBase64(val.thumbnailBase64) && | 24 | function exists (value) { |
24 | validator.isByteLength(val.thumbnailBase64, { min: 0, max: 20000 }) && | 25 | return value !== undefined && value !== null |
25 | validator.isDate(val.createdDate) | 26 | } |
27 | |||
28 | function isEachAddRemoteVideosValid (videos) { | ||
29 | return videos.every(function (video) { | ||
30 | return 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 | isVideoThumbnailValid(video.thumbnailBase64) | ||
26 | }) | 39 | }) |
27 | } | 40 | } |
28 | 41 | ||
29 | function eachIsRemoteVideosRemoveValid (values) { | 42 | function isEachRemoveRemoteVideosValid (videos) { |
30 | return values.every(function (val) { | 43 | return videos.every(function (video) { |
31 | return validator.isLength(val.magnetUri, 10) | 44 | return isVideoMagnetUriValid(video.magnetUri) |
32 | }) | 45 | }) |
33 | } | 46 | } |
34 | 47 | ||
@@ -36,6 +49,50 @@ function isArray (value) { | |||
36 | return Array.isArray(value) | 49 | return Array.isArray(value) |
37 | } | 50 | } |
38 | 51 | ||
52 | function isVideoAuthorValid (value) { | ||
53 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) | ||
54 | } | ||
55 | |||
56 | function isVideoDateValid (value) { | ||
57 | return validator.isDate(value) | ||
58 | } | ||
59 | |||
60 | function isVideoDescriptionValid (value) { | ||
61 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
62 | } | ||
63 | |||
64 | function isVideoDurationValid (value) { | ||
65 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
66 | } | ||
67 | |||
68 | function isVideoMagnetUriValid (value) { | ||
69 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) | ||
70 | } | ||
71 | |||
72 | function isVideoNameValid (value) { | ||
73 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
74 | } | ||
75 | |||
76 | function isVideoPodUrlValid (value) { | ||
77 | return validator.isURL(value) | ||
78 | } | ||
79 | |||
80 | function isVideoTagsValid (tags) { | ||
81 | return isArray(tags) && | ||
82 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | ||
83 | tags.every(function (tag) { | ||
84 | return validator.isAlphanumeric(tag) && | ||
85 | validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | function isVideoThumbnailValid (value) { | ||
90 | return validator.isBase64(value) && | ||
91 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
92 | } | ||
93 | |||
39 | // --------------------------------------------------------------------------- | 94 | // --------------------------------------------------------------------------- |
40 | 95 | ||
41 | module.exports = customValidators | 96 | module.exports = customValidators |
97 | |||
98 | // --------------------------------------------------------------------------- | ||