diff options
Diffstat (limited to 'server/helpers/custom-validators.js')
-rw-r--r-- | server/helpers/custom-validators.js | 114 |
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 | |||
3 | const validator = require('express-validator').validator | ||
4 | |||
5 | const constants = require('../initializers/constants') | ||
6 | const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS | ||
7 | |||
8 | const 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 | |||
24 | function exists (value) { | ||
25 | return value !== undefined && value !== null | ||
26 | } | ||
27 | |||
28 | function 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 | |||
51 | function isArray (value) { | ||
52 | return Array.isArray(value) | ||
53 | } | ||
54 | |||
55 | function isRequestTypeAddValid (value) { | ||
56 | return value === 'add' | ||
57 | } | ||
58 | |||
59 | function isRequestTypeRemoveValid (value) { | ||
60 | return value === 'remove' | ||
61 | } | ||
62 | |||
63 | function isVideoAuthorValid (value) { | ||
64 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) | ||
65 | } | ||
66 | |||
67 | function isVideoDateValid (value) { | ||
68 | return validator.isDate(value) | ||
69 | } | ||
70 | |||
71 | function isVideoDescriptionValid (value) { | ||
72 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | ||
73 | } | ||
74 | |||
75 | function isVideoDurationValid (value) { | ||
76 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | ||
77 | } | ||
78 | |||
79 | function isVideoMagnetUriValid (value) { | ||
80 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) | ||
81 | } | ||
82 | |||
83 | function isVideoNameValid (value) { | ||
84 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | ||
85 | } | ||
86 | |||
87 | function isVideoPodUrlValid (value) { | ||
88 | // TODO: set options (TLD...) | ||
89 | return validator.isURL(value) | ||
90 | } | ||
91 | |||
92 | function 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 | |||
101 | function isVideoThumbnailValid (value) { | ||
102 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | ||
103 | } | ||
104 | |||
105 | function isVideoThumbnail64Valid (value) { | ||
106 | return validator.isBase64(value) && | ||
107 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | ||
108 | } | ||
109 | |||
110 | // --------------------------------------------------------------------------- | ||
111 | |||
112 | module.exports = customValidators | ||
113 | |||
114 | // --------------------------------------------------------------------------- | ||