From 65fcc3119c334b75dd13bcfdebf186afdc580a8f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 15 May 2017 22:22:03 +0200 Subject: First typescript iteration --- server/helpers/custom-validators/index.js | 19 --- server/helpers/custom-validators/index.ts | 6 + server/helpers/custom-validators/misc.js | 18 --- server/helpers/custom-validators/misc.ts | 14 ++ server/helpers/custom-validators/pods.js | 26 ---- server/helpers/custom-validators/pods.ts | 24 ++++ server/helpers/custom-validators/remote/index.js | 11 -- server/helpers/custom-validators/remote/index.ts | 1 + server/helpers/custom-validators/remote/videos.js | 118 ----------------- server/helpers/custom-validators/remote/videos.ts | 138 +++++++++++++++++++ server/helpers/custom-validators/users.js | 36 ----- server/helpers/custom-validators/users.ts | 34 +++++ server/helpers/custom-validators/videos.js | 148 --------------------- server/helpers/custom-validators/videos.ts | 153 ++++++++++++++++++++++ 14 files changed, 370 insertions(+), 376 deletions(-) delete mode 100644 server/helpers/custom-validators/index.js create mode 100644 server/helpers/custom-validators/index.ts delete mode 100644 server/helpers/custom-validators/misc.js create mode 100644 server/helpers/custom-validators/misc.ts delete mode 100644 server/helpers/custom-validators/pods.js create mode 100644 server/helpers/custom-validators/pods.ts delete mode 100644 server/helpers/custom-validators/remote/index.js create mode 100644 server/helpers/custom-validators/remote/index.ts delete mode 100644 server/helpers/custom-validators/remote/videos.js create mode 100644 server/helpers/custom-validators/remote/videos.ts delete mode 100644 server/helpers/custom-validators/users.js create mode 100644 server/helpers/custom-validators/users.ts delete mode 100644 server/helpers/custom-validators/videos.js create mode 100644 server/helpers/custom-validators/videos.ts (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js deleted file mode 100644 index 9383e0304..000000000 --- a/server/helpers/custom-validators/index.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict' - -const miscValidators = require('./misc') -const podsValidators = require('./pods') -const remoteValidators = require('./remote') -const usersValidators = require('./users') -const videosValidators = require('./videos') - -const validators = { - misc: miscValidators, - pods: podsValidators, - remote: remoteValidators, - users: usersValidators, - videos: videosValidators -} - -// --------------------------------------------------------------------------- - -module.exports = validators diff --git a/server/helpers/custom-validators/index.ts b/server/helpers/custom-validators/index.ts new file mode 100644 index 000000000..1dcab624a --- /dev/null +++ b/server/helpers/custom-validators/index.ts @@ -0,0 +1,6 @@ +export * from './remote' +export * from './misc' +export * from './pods' +export * from './pods' +export * from './users' +export * from './videos' diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js deleted file mode 100644 index 052726241..000000000 --- a/server/helpers/custom-validators/misc.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict' - -const miscValidators = { - exists, - isArray -} - -function exists (value) { - return value !== undefined && value !== null -} - -function isArray (value) { - return Array.isArray(value) -} - -// --------------------------------------------------------------------------- - -module.exports = miscValidators diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts new file mode 100644 index 000000000..83f50a7fe --- /dev/null +++ b/server/helpers/custom-validators/misc.ts @@ -0,0 +1,14 @@ +function exists (value) { + return value !== undefined && value !== null +} + +function isArray (value) { + return Array.isArray(value) +} + +// --------------------------------------------------------------------------- + +export { + exists, + isArray +} diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.js deleted file mode 100644 index 8bb3733ff..000000000 --- a/server/helpers/custom-validators/pods.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' - -const validator = require('express-validator').validator - -const miscValidators = require('./misc') - -const podsValidators = { - isEachUniqueHostValid, - isHostValid -} - -function isHostValid (host) { - return validator.isURL(host) && host.split('://').length === 1 -} - -function isEachUniqueHostValid (hosts) { - return miscValidators.isArray(hosts) && - hosts.length !== 0 && - hosts.every(function (host) { - return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) - }) -} - -// --------------------------------------------------------------------------- - -module.exports = podsValidators diff --git a/server/helpers/custom-validators/pods.ts b/server/helpers/custom-validators/pods.ts new file mode 100644 index 000000000..e4c827feb --- /dev/null +++ b/server/helpers/custom-validators/pods.ts @@ -0,0 +1,24 @@ +import expressValidator = require('express-validator') +// TODO: use .validator when express-validator typing will have validator field +const validator = expressValidator['validator'] + +import { isArray } from './misc' + +function isHostValid (host) { + return validator.isURL(host) && host.split('://').length === 1 +} + +function isEachUniqueHostValid (hosts) { + return isArray(hosts) && + hosts.length !== 0 && + hosts.every(function (host) { + return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) + }) +} + +// --------------------------------------------------------------------------- + +export { + isEachUniqueHostValid, + isHostValid +} diff --git a/server/helpers/custom-validators/remote/index.js b/server/helpers/custom-validators/remote/index.js deleted file mode 100644 index 1939a95f4..000000000 --- a/server/helpers/custom-validators/remote/index.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const remoteVideosValidators = require('./videos') - -const validators = { - videos: remoteVideosValidators -} - -// --------------------------------------------------------------------------- - -module.exports = validators diff --git a/server/helpers/custom-validators/remote/index.ts b/server/helpers/custom-validators/remote/index.ts new file mode 100644 index 000000000..d6f9a7e77 --- /dev/null +++ b/server/helpers/custom-validators/remote/index.ts @@ -0,0 +1 @@ +export * from './videos'; diff --git a/server/helpers/custom-validators/remote/videos.js b/server/helpers/custom-validators/remote/videos.js deleted file mode 100644 index 24715b4b3..000000000 --- a/server/helpers/custom-validators/remote/videos.js +++ /dev/null @@ -1,118 +0,0 @@ -'use strict' - -const has = require('lodash/has') -const values = require('lodash/values') - -const constants = require('../../../initializers/constants') -const videosValidators = require('../videos') -const miscValidators = require('../misc') - -const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] - -const remoteVideosValidators = { - isEachRemoteRequestVideosValid, - isEachRemoteRequestVideosQaduValid, - isEachRemoteRequestVideosEventsValid -} - -function isEachRemoteRequestVideosValid (requests) { - return miscValidators.isArray(requests) && - requests.every(function (request) { - const video = request.data - - if (!video) return false - - return ( - isRequestTypeAddValid(request.type) && - isCommonVideoAttributesValid(video) && - videosValidators.isVideoAuthorValid(video.author) && - videosValidators.isVideoThumbnailDataValid(video.thumbnailData) - ) || - ( - isRequestTypeUpdateValid(request.type) && - isCommonVideoAttributesValid(video) - ) || - ( - isRequestTypeRemoveValid(request.type) && - videosValidators.isVideoRemoteIdValid(video.remoteId) - ) || - ( - isRequestTypeReportAbuseValid(request.type) && - videosValidators.isVideoRemoteIdValid(request.data.videoRemoteId) && - videosValidators.isVideoAbuseReasonValid(request.data.reportReason) && - videosValidators.isVideoAbuseReporterUsernameValid(request.data.reporterUsername) - ) - }) -} - -function isEachRemoteRequestVideosQaduValid (requests) { - return miscValidators.isArray(requests) && - requests.every(function (request) { - const video = request.data - - if (!video) return false - - return ( - videosValidators.isVideoRemoteIdValid(video.remoteId) && - (has(video, 'views') === false || videosValidators.isVideoViewsValid) && - (has(video, 'likes') === false || videosValidators.isVideoLikesValid) && - (has(video, 'dislikes') === false || videosValidators.isVideoDislikesValid) - ) - }) -} - -function isEachRemoteRequestVideosEventsValid (requests) { - return miscValidators.isArray(requests) && - requests.every(function (request) { - const eventData = request.data - - if (!eventData) return false - - return ( - videosValidators.isVideoRemoteIdValid(eventData.remoteId) && - values(constants.REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 && - videosValidators.isVideoEventCountValid(eventData.count) - ) - }) -} - -// --------------------------------------------------------------------------- - -module.exports = remoteVideosValidators - -// --------------------------------------------------------------------------- - -function isCommonVideoAttributesValid (video) { - return videosValidators.isVideoDateValid(video.createdAt) && - videosValidators.isVideoDateValid(video.updatedAt) && - videosValidators.isVideoCategoryValid(video.category) && - videosValidators.isVideoLicenceValid(video.licence) && - videosValidators.isVideoLanguageValid(video.language) && - videosValidators.isVideoNSFWValid(video.nsfw) && - videosValidators.isVideoDescriptionValid(video.description) && - videosValidators.isVideoDurationValid(video.duration) && - videosValidators.isVideoInfoHashValid(video.infoHash) && - videosValidators.isVideoNameValid(video.name) && - videosValidators.isVideoTagsValid(video.tags) && - videosValidators.isVideoRemoteIdValid(video.remoteId) && - videosValidators.isVideoExtnameValid(video.extname) && - videosValidators.isVideoViewsValid(video.views) && - videosValidators.isVideoLikesValid(video.likes) && - videosValidators.isVideoDislikesValid(video.dislikes) -} - -function isRequestTypeAddValid (value) { - return value === ENDPOINT_ACTIONS.ADD -} - -function isRequestTypeUpdateValid (value) { - return value === ENDPOINT_ACTIONS.UPDATE -} - -function isRequestTypeRemoveValid (value) { - return value === ENDPOINT_ACTIONS.REMOVE -} - -function isRequestTypeReportAbuseValid (value) { - return value === ENDPOINT_ACTIONS.REPORT_ABUSE -} diff --git a/server/helpers/custom-validators/remote/videos.ts b/server/helpers/custom-validators/remote/videos.ts new file mode 100644 index 000000000..4b904d011 --- /dev/null +++ b/server/helpers/custom-validators/remote/videos.ts @@ -0,0 +1,138 @@ +import { has, values } from 'lodash' + +import { + REQUEST_ENDPOINTS, + REQUEST_ENDPOINT_ACTIONS, + REQUEST_VIDEO_EVENT_TYPES +} from '../../../initializers' +import { isArray } from '../misc' +import { + isVideoAuthorValid, + isVideoThumbnailDataValid, + isVideoRemoteIdValid, + isVideoAbuseReasonValid, + isVideoAbuseReporterUsernameValid, + isVideoViewsValid, + isVideoLikesValid, + isVideoDislikesValid, + isVideoEventCountValid, + isVideoDateValid, + isVideoCategoryValid, + isVideoLicenceValid, + isVideoLanguageValid, + isVideoNSFWValid, + isVideoDescriptionValid, + isVideoDurationValid, + isVideoInfoHashValid, + isVideoNameValid, + isVideoTagsValid, + isVideoExtnameValid +} from '../videos' + +const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] + +function isEachRemoteRequestVideosValid (requests) { + return isArray(requests) && + requests.every(function (request) { + const video = request.data + + if (!video) return false + + return ( + isRequestTypeAddValid(request.type) && + isCommonVideoAttributesValid(video) && + isVideoAuthorValid(video.author) && + isVideoThumbnailDataValid(video.thumbnailData) + ) || + ( + isRequestTypeUpdateValid(request.type) && + isCommonVideoAttributesValid(video) + ) || + ( + isRequestTypeRemoveValid(request.type) && + isVideoRemoteIdValid(video.remoteId) + ) || + ( + isRequestTypeReportAbuseValid(request.type) && + isVideoRemoteIdValid(request.data.videoRemoteId) && + isVideoAbuseReasonValid(request.data.reportReason) && + isVideoAbuseReporterUsernameValid(request.data.reporterUsername) + ) + }) +} + +function isEachRemoteRequestVideosQaduValid (requests) { + return isArray(requests) && + requests.every(function (request) { + const video = request.data + + if (!video) return false + + return ( + isVideoRemoteIdValid(video.remoteId) && + (has(video, 'views') === false || isVideoViewsValid) && + (has(video, 'likes') === false || isVideoLikesValid) && + (has(video, 'dislikes') === false || isVideoDislikesValid) + ) + }) +} + +function isEachRemoteRequestVideosEventsValid (requests) { + return isArray(requests) && + requests.every(function (request) { + const eventData = request.data + + if (!eventData) return false + + return ( + isVideoRemoteIdValid(eventData.remoteId) && + values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 && + isVideoEventCountValid(eventData.count) + ) + }) +} + +// --------------------------------------------------------------------------- + +export { + isEachRemoteRequestVideosValid, + isEachRemoteRequestVideosQaduValid, + isEachRemoteRequestVideosEventsValid +} + +// --------------------------------------------------------------------------- + +function isCommonVideoAttributesValid (video) { + return isVideoDateValid(video.createdAt) && + isVideoDateValid(video.updatedAt) && + isVideoCategoryValid(video.category) && + isVideoLicenceValid(video.licence) && + isVideoLanguageValid(video.language) && + isVideoNSFWValid(video.nsfw) && + isVideoDescriptionValid(video.description) && + isVideoDurationValid(video.duration) && + isVideoInfoHashValid(video.infoHash) && + isVideoNameValid(video.name) && + isVideoTagsValid(video.tags) && + isVideoRemoteIdValid(video.remoteId) && + isVideoExtnameValid(video.extname) && + isVideoViewsValid(video.views) && + isVideoLikesValid(video.likes) && + isVideoDislikesValid(video.dislikes) +} + +function isRequestTypeAddValid (value) { + return value === ENDPOINT_ACTIONS.ADD +} + +function isRequestTypeUpdateValid (value) { + return value === ENDPOINT_ACTIONS.UPDATE +} + +function isRequestTypeRemoveValid (value) { + return value === ENDPOINT_ACTIONS.REMOVE +} + +function isRequestTypeReportAbuseValid (value) { + return value === ENDPOINT_ACTIONS.REPORT_ABUSE +} diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js deleted file mode 100644 index 2fc026e98..000000000 --- a/server/helpers/custom-validators/users.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict' - -const validator = require('express-validator').validator -const values = require('lodash/values') - -const constants = require('../../initializers/constants') -const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS - -const usersValidators = { - isUserPasswordValid, - isUserRoleValid, - isUserUsernameValid, - isUserDisplayNSFWValid -} - -function isUserPasswordValid (value) { - return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) -} - -function isUserRoleValid (value) { - return values(constants.USER_ROLES).indexOf(value) !== -1 -} - -function isUserUsernameValid (value) { - const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max - const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min - return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) -} - -function isUserDisplayNSFWValid (value) { - return validator.isBoolean(value) -} - -// --------------------------------------------------------------------------- - -module.exports = usersValidators diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts new file mode 100644 index 000000000..8fd2dac4f --- /dev/null +++ b/server/helpers/custom-validators/users.ts @@ -0,0 +1,34 @@ +import { values } from 'lodash' +import expressValidator = require('express-validator') +// TODO: use .validator when express-validator typing will have validator field +const validator = expressValidator['validator'] + +import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' +const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS + +function isUserPasswordValid (value) { + return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) +} + +function isUserRoleValid (value) { + return values(USER_ROLES).indexOf(value) !== -1 +} + +function isUserUsernameValid (value) { + const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max + const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min + return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) +} + +function isUserDisplayNSFWValid (value) { + return validator.isBoolean(value) +} + +// --------------------------------------------------------------------------- + +export { + isUserPasswordValid, + isUserRoleValid, + isUserUsernameValid, + isUserDisplayNSFWValid +} diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js deleted file mode 100644 index 196731e04..000000000 --- a/server/helpers/custom-validators/videos.js +++ /dev/null @@ -1,148 +0,0 @@ -'use strict' - -const validator = require('express-validator').validator -const values = require('lodash/values') - -const constants = require('../../initializers/constants') -const usersValidators = require('./users') -const miscValidators = require('./misc') -const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS -const VIDEO_ABUSES_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_ABUSES -const VIDEO_EVENTS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_EVENTS - -const videosValidators = { - isVideoAuthorValid, - isVideoDateValid, - isVideoCategoryValid, - isVideoLicenceValid, - isVideoLanguageValid, - isVideoNSFWValid, - isVideoDescriptionValid, - isVideoDurationValid, - isVideoInfoHashValid, - isVideoNameValid, - isVideoTagsValid, - isVideoThumbnailValid, - isVideoThumbnailDataValid, - isVideoExtnameValid, - isVideoRemoteIdValid, - isVideoAbuseReasonValid, - isVideoAbuseReporterUsernameValid, - isVideoFile, - isVideoViewsValid, - isVideoLikesValid, - isVideoRatingTypeValid, - isVideoDislikesValid, - isVideoEventCountValid -} - -function isVideoAuthorValid (value) { - return usersValidators.isUserUsernameValid(value) -} - -function isVideoDateValid (value) { - return validator.isDate(value) -} - -function isVideoCategoryValid (value) { - return constants.VIDEO_CATEGORIES[value] !== undefined -} - -function isVideoLicenceValid (value) { - return constants.VIDEO_LICENCES[value] !== undefined -} - -function isVideoLanguageValid (value) { - return value === null || constants.VIDEO_LANGUAGES[value] !== undefined -} - -function isVideoNSFWValid (value) { - return validator.isBoolean(value) -} - -function isVideoDescriptionValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) -} - -function isVideoDurationValid (value) { - return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) -} - -function isVideoExtnameValid (value) { - return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 -} - -function isVideoInfoHashValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) -} - -function isVideoNameValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) -} - -function isVideoTagsValid (tags) { - return miscValidators.isArray(tags) && - validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && - tags.every(function (tag) { - return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) - }) -} - -function isVideoThumbnailValid (value) { - return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) -} - -function isVideoThumbnailDataValid (value) { - return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) -} - -function isVideoRemoteIdValid (value) { - return validator.isUUID(value, 4) -} - -function isVideoAbuseReasonValid (value) { - return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) -} - -function isVideoAbuseReporterUsernameValid (value) { - return usersValidators.isUserUsernameValid(value) -} - -function isVideoViewsValid (value) { - return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) -} - -function isVideoLikesValid (value) { - return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) -} - -function isVideoDislikesValid (value) { - return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) -} - -function isVideoEventCountValid (value) { - return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) -} - -function isVideoRatingTypeValid (value) { - return values(constants.VIDEO_RATE_TYPES).indexOf(value) !== -1 -} - -function isVideoFile (value, files) { - // Should have files - if (!files) return false - - // Should have videofile file - const videofile = files.videofile - if (!videofile || videofile.length === 0) return false - - // The file should exist - const file = videofile[0] - if (!file || !file.originalname) return false - - return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) -} - -// --------------------------------------------------------------------------- - -module.exports = videosValidators diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts new file mode 100644 index 000000000..2b2370be4 --- /dev/null +++ b/server/helpers/custom-validators/videos.ts @@ -0,0 +1,153 @@ +import { values } from 'lodash' +import expressValidator = require('express-validator') +// TODO: use .validator when express-validator typing will have validator field +const validator = expressValidator['validator'] + +import { + CONSTRAINTS_FIELDS, + VIDEO_CATEGORIES, + VIDEO_LICENCES, + VIDEO_LANGUAGES, + VIDEO_RATE_TYPES +} from '../../initializers' +import { isUserUsernameValid } from './users' +import { isArray } from './misc' + +const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS +const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES +const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS + +function isVideoAuthorValid (value) { + return isUserUsernameValid(value) +} + +function isVideoDateValid (value) { + return validator.isDate(value) +} + +function isVideoCategoryValid (value) { + return VIDEO_CATEGORIES[value] !== undefined +} + +function isVideoLicenceValid (value) { + return VIDEO_LICENCES[value] !== undefined +} + +function isVideoLanguageValid (value) { + return value === null || VIDEO_LANGUAGES[value] !== undefined +} + +function isVideoNSFWValid (value) { + return validator.isBoolean(value) +} + +function isVideoDescriptionValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) +} + +function isVideoDurationValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) +} + +function isVideoExtnameValid (value) { + return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 +} + +function isVideoInfoHashValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) +} + +function isVideoNameValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) +} + +function isVideoTagsValid (tags) { + return isArray(tags) && + validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && + tags.every(function (tag) { + return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) + }) +} + +function isVideoThumbnailValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) +} + +function isVideoThumbnailDataValid (value) { + return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) +} + +function isVideoRemoteIdValid (value) { + return validator.isUUID(value, 4) +} + +function isVideoAbuseReasonValid (value) { + return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) +} + +function isVideoAbuseReporterUsernameValid (value) { + return isUserUsernameValid(value) +} + +function isVideoViewsValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) +} + +function isVideoLikesValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) +} + +function isVideoDislikesValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) +} + +function isVideoEventCountValid (value) { + return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) +} + +function isVideoRatingTypeValid (value) { + return values(VIDEO_RATE_TYPES).indexOf(value) !== -1 +} + +function isVideoFile (value, files) { + // Should have files + if (!files) return false + + // Should have videofile file + const videofile = files.videofile + if (!videofile || videofile.length === 0) return false + + // The file should exist + const file = videofile[0] + if (!file || !file.originalname) return false + + return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) +} + +// --------------------------------------------------------------------------- + +export { + isVideoAuthorValid, + isVideoDateValid, + isVideoCategoryValid, + isVideoLicenceValid, + isVideoLanguageValid, + isVideoNSFWValid, + isVideoDescriptionValid, + isVideoDurationValid, + isVideoInfoHashValid, + isVideoNameValid, + isVideoTagsValid, + isVideoThumbnailValid, + isVideoThumbnailDataValid, + isVideoExtnameValid, + isVideoRemoteIdValid, + isVideoAbuseReasonValid, + isVideoAbuseReporterUsernameValid, + isVideoFile, + isVideoViewsValid, + isVideoLikesValid, + isVideoRatingTypeValid, + isVideoDislikesValid, + isVideoEventCountValid +} -- cgit v1.2.3