mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
First typescript iteration
This commit is contained in:
parent
d5f345ed4c
commit
65fcc3119c
113 changed files with 1961 additions and 1784 deletions
|
@ -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
|
6
server/helpers/custom-validators/index.ts
Normal file
6
server/helpers/custom-validators/index.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
export * from './remote'
|
||||
export * from './misc'
|
||||
export * from './pods'
|
||||
export * from './pods'
|
||||
export * from './users'
|
||||
export * from './videos'
|
|
@ -1,10 +1,3 @@
|
|||
'use strict'
|
||||
|
||||
const miscValidators = {
|
||||
exists,
|
||||
isArray
|
||||
}
|
||||
|
||||
function exists (value) {
|
||||
return value !== undefined && value !== null
|
||||
}
|
||||
|
@ -15,4 +8,7 @@ function isArray (value) {
|
|||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = miscValidators
|
||||
export {
|
||||
exists,
|
||||
isArray
|
||||
}
|
|
@ -1,20 +1,15 @@
|
|||
'use strict'
|
||||
import expressValidator = require('express-validator')
|
||||
// TODO: use .validator when express-validator typing will have validator field
|
||||
const validator = expressValidator['validator']
|
||||
|
||||
const validator = require('express-validator').validator
|
||||
|
||||
const miscValidators = require('./misc')
|
||||
|
||||
const podsValidators = {
|
||||
isEachUniqueHostValid,
|
||||
isHostValid
|
||||
}
|
||||
import { isArray } from './misc'
|
||||
|
||||
function isHostValid (host) {
|
||||
return validator.isURL(host) && host.split('://').length === 1
|
||||
}
|
||||
|
||||
function isEachUniqueHostValid (hosts) {
|
||||
return miscValidators.isArray(hosts) &&
|
||||
return isArray(hosts) &&
|
||||
hosts.length !== 0 &&
|
||||
hosts.every(function (host) {
|
||||
return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
|
||||
|
@ -23,4 +18,7 @@ function isEachUniqueHostValid (hosts) {
|
|||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = podsValidators
|
||||
export {
|
||||
isEachUniqueHostValid,
|
||||
isHostValid
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const remoteVideosValidators = require('./videos')
|
||||
|
||||
const validators = {
|
||||
videos: remoteVideosValidators
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = validators
|
1
server/helpers/custom-validators/remote/index.ts
Normal file
1
server/helpers/custom-validators/remote/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './videos';
|
|
@ -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
|
||||
}
|
138
server/helpers/custom-validators/remote/videos.ts
Normal file
138
server/helpers/custom-validators/remote/videos.ts
Normal file
|
@ -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
|
||||
}
|
|
@ -1,24 +1,17 @@
|
|||
'use strict'
|
||||
import { values } from 'lodash'
|
||||
import expressValidator = require('express-validator')
|
||||
// TODO: use .validator when express-validator typing will have validator field
|
||||
const validator = expressValidator['validator']
|
||||
|
||||
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
|
||||
}
|
||||
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(constants.USER_ROLES).indexOf(value) !== -1
|
||||
return values(USER_ROLES).indexOf(value) !== -1
|
||||
}
|
||||
|
||||
function isUserUsernameValid (value) {
|
||||
|
@ -33,4 +26,9 @@ function isUserDisplayNSFWValid (value) {
|
|||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = usersValidators
|
||||
export {
|
||||
isUserPasswordValid,
|
||||
isUserRoleValid,
|
||||
isUserUsernameValid,
|
||||
isUserDisplayNSFWValid
|
||||
}
|
|
@ -1,43 +1,24 @@
|
|||
'use strict'
|
||||
import { values } from 'lodash'
|
||||
import expressValidator = require('express-validator')
|
||||
// TODO: use .validator when express-validator typing will have validator field
|
||||
const validator = expressValidator['validator']
|
||||
|
||||
const validator = require('express-validator').validator
|
||||
const values = require('lodash/values')
|
||||
import {
|
||||
CONSTRAINTS_FIELDS,
|
||||
VIDEO_CATEGORIES,
|
||||
VIDEO_LICENCES,
|
||||
VIDEO_LANGUAGES,
|
||||
VIDEO_RATE_TYPES
|
||||
} from '../../initializers'
|
||||
import { isUserUsernameValid } from './users'
|
||||
import { isArray } from './misc'
|
||||
|
||||
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
|
||||
}
|
||||
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 usersValidators.isUserUsernameValid(value)
|
||||
return isUserUsernameValid(value)
|
||||
}
|
||||
|
||||
function isVideoDateValid (value) {
|
||||
|
@ -45,15 +26,15 @@ function isVideoDateValid (value) {
|
|||
}
|
||||
|
||||
function isVideoCategoryValid (value) {
|
||||
return constants.VIDEO_CATEGORIES[value] !== undefined
|
||||
return VIDEO_CATEGORIES[value] !== undefined
|
||||
}
|
||||
|
||||
function isVideoLicenceValid (value) {
|
||||
return constants.VIDEO_LICENCES[value] !== undefined
|
||||
return VIDEO_LICENCES[value] !== undefined
|
||||
}
|
||||
|
||||
function isVideoLanguageValid (value) {
|
||||
return value === null || constants.VIDEO_LANGUAGES[value] !== undefined
|
||||
return value === null || VIDEO_LANGUAGES[value] !== undefined
|
||||
}
|
||||
|
||||
function isVideoNSFWValid (value) {
|
||||
|
@ -81,7 +62,7 @@ function isVideoNameValid (value) {
|
|||
}
|
||||
|
||||
function isVideoTagsValid (tags) {
|
||||
return miscValidators.isArray(tags) &&
|
||||
return isArray(tags) &&
|
||||
validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
|
||||
tags.every(function (tag) {
|
||||
return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
|
||||
|
@ -105,7 +86,7 @@ function isVideoAbuseReasonValid (value) {
|
|||
}
|
||||
|
||||
function isVideoAbuseReporterUsernameValid (value) {
|
||||
return usersValidators.isUserUsernameValid(value)
|
||||
return isUserUsernameValid(value)
|
||||
}
|
||||
|
||||
function isVideoViewsValid (value) {
|
||||
|
@ -125,7 +106,7 @@ function isVideoEventCountValid (value) {
|
|||
}
|
||||
|
||||
function isVideoRatingTypeValid (value) {
|
||||
return values(constants.VIDEO_RATE_TYPES).indexOf(value) !== -1
|
||||
return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
|
||||
}
|
||||
|
||||
function isVideoFile (value, files) {
|
||||
|
@ -145,4 +126,28 @@ function isVideoFile (value, files) {
|
|||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = videosValidators
|
||||
export {
|
||||
isVideoAuthorValid,
|
||||
isVideoDateValid,
|
||||
isVideoCategoryValid,
|
||||
isVideoLicenceValid,
|
||||
isVideoLanguageValid,
|
||||
isVideoNSFWValid,
|
||||
isVideoDescriptionValid,
|
||||
isVideoDurationValid,
|
||||
isVideoInfoHashValid,
|
||||
isVideoNameValid,
|
||||
isVideoTagsValid,
|
||||
isVideoThumbnailValid,
|
||||
isVideoThumbnailDataValid,
|
||||
isVideoExtnameValid,
|
||||
isVideoRemoteIdValid,
|
||||
isVideoAbuseReasonValid,
|
||||
isVideoAbuseReporterUsernameValid,
|
||||
isVideoFile,
|
||||
isVideoViewsValid,
|
||||
isVideoLikesValid,
|
||||
isVideoRatingTypeValid,
|
||||
isVideoDislikesValid,
|
||||
isVideoEventCountValid
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue