From be587647f98a4b83ca06a61fe55c7ac5d60927c6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 6 Jun 2016 14:15:03 +0200 Subject: Add tags support to server --- server/helpers/customValidators.js | 101 +++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 22 deletions(-) (limited to 'server/helpers') 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 @@ 'use strict' -const validator = require('validator') +const validator = require('express-validator').validator const constants = require('../initializers/constants') +const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS const customValidators = { - eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, - eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, - isArray: isArray -} - -function eachIsRemoteVideosAddValid (values) { - return values.every(function (val) { - return validator.isLength(val.name, 1, 50) && - validator.isLength(val.description, 1, 50) && - validator.isLength(val.magnetUri, 10) && - validator.isURL(val.podUrl) && - !isNaN(val.duration) && - val.duration >= 0 && - val.duration < constants.MAXIMUM_VIDEO_DURATION && - validator.isLength(val.author, 1, constants.MAXIMUM_AUTHOR_LENGTH) && - validator.isBase64(val.thumbnailBase64) && - validator.isByteLength(val.thumbnailBase64, { min: 0, max: 20000 }) && - validator.isDate(val.createdDate) + exists: exists, + isEachAddRemoteVideosValid: isEachAddRemoteVideosValid, + isEachRemoveRemoteVideosValid: isEachRemoveRemoteVideosValid, + isArray: isArray, + isVideoAuthorValid: isVideoAuthorValid, + isVideoDateValid: isVideoDateValid, + isVideoDescriptionValid: isVideoDescriptionValid, + isVideoDurationValid: isVideoDurationValid, + isVideoMagnetUriValid: isVideoMagnetUriValid, + isVideoNameValid: isVideoNameValid, + isVideoPodUrlValid: isVideoPodUrlValid, + isVideoTagsValid: isVideoTagsValid, + isVideoThumbnailValid: isVideoThumbnailValid +} + +function exists (value) { + return value !== undefined && value !== null +} + +function isEachAddRemoteVideosValid (videos) { + return videos.every(function (video) { + return isVideoAuthorValid(video.author) && + isVideoDateValid(video.createdDate) && + isVideoDescriptionValid(video.description) && + isVideoDurationValid(video.duration) && + isVideoMagnetUriValid(video.magnetUri) && + isVideoNameValid(video.name) && + isVideoPodUrlValid(video.podUrl) && + isVideoTagsValid(video.tags) && + isVideoThumbnailValid(video.thumbnailBase64) }) } -function eachIsRemoteVideosRemoveValid (values) { - return values.every(function (val) { - return validator.isLength(val.magnetUri, 10) +function isEachRemoveRemoteVideosValid (videos) { + return videos.every(function (video) { + return isVideoMagnetUriValid(video.magnetUri) }) } @@ -36,6 +49,50 @@ function isArray (value) { return Array.isArray(value) } +function isVideoAuthorValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) +} + +function isVideoDateValid (value) { + return validator.isDate(value) +} + +function isVideoDescriptionValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) +} + +function isVideoDurationValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) +} + +function isVideoMagnetUriValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) +} + +function isVideoNameValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) +} + +function isVideoPodUrlValid (value) { + return validator.isURL(value) +} + +function isVideoTagsValid (tags) { + return isArray(tags) && + validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && + tags.every(function (tag) { + return validator.isAlphanumeric(tag) && + validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) + }) +} + +function isVideoThumbnailValid (value) { + return validator.isBase64(value) && + validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) +} + // --------------------------------------------------------------------------- module.exports = customValidators + +// --------------------------------------------------------------------------- -- cgit v1.2.3