aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/customValidators.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/customValidators.js')
-rw-r--r--server/helpers/customValidators.js101
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
3const validator = require('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
6 7
7const customValidators = { 8const 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,
13function 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) && 24function exists (value) {
24 validator.isByteLength(val.thumbnailBase64, { min: 0, max: 20000 }) && 25 return value !== undefined && value !== null
25 validator.isDate(val.createdDate) 26}
27
28function 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
29function eachIsRemoteVideosRemoveValid (values) { 42function 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
52function isVideoAuthorValid (value) {
53 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
54}
55
56function isVideoDateValid (value) {
57 return validator.isDate(value)
58}
59
60function isVideoDescriptionValid (value) {
61 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
62}
63
64function isVideoDurationValid (value) {
65 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
66}
67
68function isVideoMagnetUriValid (value) {
69 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI)
70}
71
72function isVideoNameValid (value) {
73 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
74}
75
76function isVideoPodUrlValid (value) {
77 return validator.isURL(value)
78}
79
80function 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
89function isVideoThumbnailValid (value) {
90 return validator.isBase64(value) &&
91 validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
92}
93
39// --------------------------------------------------------------------------- 94// ---------------------------------------------------------------------------
40 95
41module.exports = customValidators 96module.exports = customValidators
97
98// ---------------------------------------------------------------------------