aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators.js')
-rw-r--r--server/helpers/custom-validators.js114
1 files changed, 114 insertions, 0 deletions
diff --git a/server/helpers/custom-validators.js b/server/helpers/custom-validators.js
new file mode 100644
index 000000000..b666644c0
--- /dev/null
+++ b/server/helpers/custom-validators.js
@@ -0,0 +1,114 @@
1'use strict'
2
3const validator = require('express-validator').validator
4
5const constants = require('../initializers/constants')
6const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
7
8const customValidators = {
9 exists: exists,
10 isEachRemoteVideosValid: isEachRemoteVideosValid,
11 isArray: isArray,
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
24function exists (value) {
25 return value !== undefined && value !== null
26}
27
28function isEachRemoteVideosValid (requests) {
29 return requests.every(function (request) {
30 const video = request.data
31 return (
32 isRequestTypeAddValid(request.type) &&
33 isVideoAuthorValid(video.author) &&
34 isVideoDateValid(video.createdDate) &&
35 isVideoDescriptionValid(video.description) &&
36 isVideoDurationValid(video.duration) &&
37 isVideoMagnetUriValid(video.magnetUri) &&
38 isVideoNameValid(video.name) &&
39 isVideoPodUrlValid(video.podUrl) &&
40 isVideoTagsValid(video.tags) &&
41 isVideoThumbnail64Valid(video.thumbnailBase64)
42 ) ||
43 (
44 isRequestTypeRemoveValid(request.type) &&
45 isVideoNameValid(video.name) &&
46 isVideoMagnetUriValid(video.magnetUri)
47 )
48 })
49}
50
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) {
64 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
65}
66
67function isVideoDateValid (value) {
68 return validator.isDate(value)
69}
70
71function isVideoDescriptionValid (value) {
72 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
73}
74
75function isVideoDurationValid (value) {
76 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
77}
78
79function isVideoMagnetUriValid (value) {
80 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI)
81}
82
83function isVideoNameValid (value) {
84 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
85}
86
87function isVideoPodUrlValid (value) {
88 // TODO: set options (TLD...)
89 return validator.isURL(value)
90}
91
92function isVideoTagsValid (tags) {
93 return isArray(tags) &&
94 validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
95 tags.every(function (tag) {
96 return validator.isAlphanumeric(tag) &&
97 validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
98 })
99}
100
101function isVideoThumbnailValid (value) {
102 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
103}
104
105function isVideoThumbnail64Valid (value) {
106 return validator.isBase64(value) &&
107 validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
108}
109
110// ---------------------------------------------------------------------------
111
112module.exports = customValidators
113
114// ---------------------------------------------------------------------------