Server: reorganize express validators
This commit is contained in:
parent
e62f6ef741
commit
e4c556196d
|
@ -53,7 +53,7 @@ app.use(bodyParser.json())
|
||||||
app.use(bodyParser.urlencoded({ extended: false }))
|
app.use(bodyParser.urlencoded({ extended: false }))
|
||||||
// Validate some params for the API
|
// Validate some params for the API
|
||||||
app.use(expressValidator({
|
app.use(expressValidator({
|
||||||
customValidators: customValidators
|
customValidators: Object.assign({}, customValidators.misc, customValidators.users, customValidators.videos)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// ----------- Views, routes and static files -----------
|
// ----------- Views, routes and static files -----------
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const miscValidators = require('./misc')
|
||||||
|
const usersValidators = require('./users')
|
||||||
|
const videosValidators = require('./videos')
|
||||||
|
|
||||||
|
const validators = {
|
||||||
|
misc: miscValidators,
|
||||||
|
users: usersValidators,
|
||||||
|
videos: videosValidators
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
module.exports = validators
|
|
@ -0,0 +1,18 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const miscValidators = {
|
||||||
|
exists: exists,
|
||||||
|
isArray: isArray
|
||||||
|
}
|
||||||
|
|
||||||
|
function exists (value) {
|
||||||
|
return value !== undefined && value !== null
|
||||||
|
}
|
||||||
|
|
||||||
|
function isArray (value) {
|
||||||
|
return Array.isArray(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
module.exports = miscValidators
|
|
@ -0,0 +1,18 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const validator = require('express-validator').validator
|
||||||
|
|
||||||
|
const constants = require('../../initializers/constants')
|
||||||
|
const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
|
||||||
|
|
||||||
|
const usersValidators = {
|
||||||
|
isUserUsernameValid: isUserUsernameValid
|
||||||
|
}
|
||||||
|
|
||||||
|
function isUserUsernameValid (value) {
|
||||||
|
return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
module.exports = usersValidators
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
const validator = require('express-validator').validator
|
const validator = require('express-validator').validator
|
||||||
|
|
||||||
const constants = require('../initializers/constants')
|
const constants = require('../../initializers/constants')
|
||||||
const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
|
const usersValidators = require('./users')
|
||||||
|
const miscValidators = require('./misc')
|
||||||
|
const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS
|
||||||
|
|
||||||
const customValidators = {
|
const videosValidators = {
|
||||||
exists: exists,
|
|
||||||
isEachRemoteVideosValid: isEachRemoteVideosValid,
|
isEachRemoteVideosValid: isEachRemoteVideosValid,
|
||||||
isArray: isArray,
|
|
||||||
isVideoAuthorValid: isVideoAuthorValid,
|
isVideoAuthorValid: isVideoAuthorValid,
|
||||||
isVideoDateValid: isVideoDateValid,
|
isVideoDateValid: isVideoDateValid,
|
||||||
isVideoDescriptionValid: isVideoDescriptionValid,
|
isVideoDescriptionValid: isVideoDescriptionValid,
|
||||||
|
@ -21,10 +21,6 @@ const customValidators = {
|
||||||
isVideoThumbnail64Valid: isVideoThumbnail64Valid
|
isVideoThumbnail64Valid: isVideoThumbnail64Valid
|
||||||
}
|
}
|
||||||
|
|
||||||
function exists (value) {
|
|
||||||
return value !== undefined && value !== null
|
|
||||||
}
|
|
||||||
|
|
||||||
function isEachRemoteVideosValid (requests) {
|
function isEachRemoteVideosValid (requests) {
|
||||||
return requests.every(function (request) {
|
return requests.every(function (request) {
|
||||||
const video = request.data
|
const video = request.data
|
||||||
|
@ -48,20 +44,8 @@ function isEachRemoteVideosValid (requests) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function isArray (value) {
|
|
||||||
return Array.isArray(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
function isRequestTypeAddValid (value) {
|
|
||||||
return value === 'add'
|
|
||||||
}
|
|
||||||
|
|
||||||
function isRequestTypeRemoveValid (value) {
|
|
||||||
return value === 'remove'
|
|
||||||
}
|
|
||||||
|
|
||||||
function isVideoAuthorValid (value) {
|
function isVideoAuthorValid (value) {
|
||||||
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
|
return usersValidators.isUserUsernameValid(usersValidators)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isVideoDateValid (value) {
|
function isVideoDateValid (value) {
|
||||||
|
@ -90,7 +74,7 @@ function isVideoPodUrlValid (value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isVideoTagsValid (tags) {
|
function isVideoTagsValid (tags) {
|
||||||
return isArray(tags) &&
|
return miscValidators.isArray(tags) &&
|
||||||
validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
|
validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
|
||||||
tags.every(function (tag) {
|
tags.every(function (tag) {
|
||||||
return validator.isAlphanumeric(tag) &&
|
return validator.isAlphanumeric(tag) &&
|
||||||
|
@ -109,6 +93,14 @@ function isVideoThumbnail64Valid (value) {
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
module.exports = customValidators
|
module.exports = videosValidators
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function isRequestTypeAddValid (value) {
|
||||||
|
return value === 'add'
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRequestTypeRemoveValid (value) {
|
||||||
|
return value === 'remove'
|
||||||
|
}
|
|
@ -3,6 +3,23 @@
|
||||||
// API version of our pod
|
// API version of our pod
|
||||||
const API_VERSION = 'v1'
|
const API_VERSION = 'v1'
|
||||||
|
|
||||||
|
const CONSTRAINTS_FIELDS = {
|
||||||
|
USERS: {
|
||||||
|
USERNAME: { min: 3, max: 20 }, // Length
|
||||||
|
PASSWORD: { min: 6, max: 255 } // Length
|
||||||
|
},
|
||||||
|
VIDEOS: {
|
||||||
|
NAME: { min: 3, max: 50 }, // Length
|
||||||
|
DESCRIPTION: { min: 3, max: 250 }, // Length
|
||||||
|
MAGNET_URI: { min: 10 }, // Length
|
||||||
|
DURATION: { min: 1, max: 7200 }, // Number
|
||||||
|
TAGS: { min: 1, max: 3 }, // Number of total tags
|
||||||
|
TAG: { min: 2, max: 10 }, // Length
|
||||||
|
THUMBNAIL: { min: 2, max: 30 },
|
||||||
|
THUMBNAIL64: { min: 0, max: 20000 } // Bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Score a pod has when we create it as a friend
|
// Score a pod has when we create it as a friend
|
||||||
const FRIEND_SCORE = {
|
const FRIEND_SCORE = {
|
||||||
BASE: 100,
|
BASE: 100,
|
||||||
|
@ -55,29 +72,18 @@ const THUMBNAILS_SIZE = '200x110'
|
||||||
// Path for access to thumbnails with express router
|
// Path for access to thumbnails with express router
|
||||||
const THUMBNAILS_STATIC_PATH = '/static/thumbnails'
|
const THUMBNAILS_STATIC_PATH = '/static/thumbnails'
|
||||||
|
|
||||||
const VIDEOS_CONSTRAINTS_FIELDS = {
|
|
||||||
NAME: { min: 3, max: 50 }, // Length
|
|
||||||
DESCRIPTION: { min: 3, max: 250 }, // Length
|
|
||||||
MAGNET_URI: { min: 10 }, // Length
|
|
||||||
DURATION: { min: 1, max: 7200 }, // Number
|
|
||||||
AUTHOR: { min: 3, max: 20 }, // Length
|
|
||||||
TAGS: { min: 1, max: 3 }, // Number of total tags
|
|
||||||
TAG: { min: 2, max: 10 }, // Length
|
|
||||||
THUMBNAIL: { min: 2, max: 30 },
|
|
||||||
THUMBNAIL64: { min: 0, max: 20000 } // Bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special constants for a test instance
|
// Special constants for a test instance
|
||||||
if (isTestInstance() === true) {
|
if (isTestInstance() === true) {
|
||||||
FRIEND_SCORE.BASE = 20
|
FRIEND_SCORE.BASE = 20
|
||||||
INTERVAL = 10000
|
INTERVAL = 10000
|
||||||
VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14
|
CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
API_VERSION: API_VERSION,
|
API_VERSION: API_VERSION,
|
||||||
|
CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
|
||||||
FRIEND_SCORE: FRIEND_SCORE,
|
FRIEND_SCORE: FRIEND_SCORE,
|
||||||
INTERVAL: INTERVAL,
|
INTERVAL: INTERVAL,
|
||||||
OAUTH_LIFETIME: OAUTH_LIFETIME,
|
OAUTH_LIFETIME: OAUTH_LIFETIME,
|
||||||
|
@ -90,8 +96,7 @@ module.exports = {
|
||||||
SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL,
|
SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL,
|
||||||
SORTABLE_COLUMNS: SORTABLE_COLUMNS,
|
SORTABLE_COLUMNS: SORTABLE_COLUMNS,
|
||||||
THUMBNAILS_SIZE: THUMBNAILS_SIZE,
|
THUMBNAILS_SIZE: THUMBNAILS_SIZE,
|
||||||
THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH,
|
THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH
|
||||||
VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
|
@ -4,7 +4,7 @@ const mongoose = require('mongoose')
|
||||||
|
|
||||||
const checkErrors = require('./utils').checkErrors
|
const checkErrors = require('./utils').checkErrors
|
||||||
const constants = require('../../initializers/constants')
|
const constants = require('../../initializers/constants')
|
||||||
const customValidators = require('../../helpers/custom-validators')
|
const customVideosValidators = require('../../helpers/custom-validators').videos
|
||||||
const logger = require('../../helpers/logger')
|
const logger = require('../../helpers/logger')
|
||||||
|
|
||||||
const Video = mongoose.model('Video')
|
const Video = mongoose.model('Video')
|
||||||
|
@ -33,8 +33,8 @@ function videosAdd (req, res, next) {
|
||||||
return res.status(400).send('Cannot retrieve metadata of the file.')
|
return res.status(400).send('Cannot retrieve metadata of the file.')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!customValidators.isVideoDurationValid(duration)) {
|
if (!customVideosValidators.isVideoDurationValid(duration)) {
|
||||||
return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).')
|
return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
|
||||||
}
|
}
|
||||||
|
|
||||||
videoFile.duration = duration
|
videoFile.duration = duration
|
||||||
|
|
|
@ -9,7 +9,7 @@ const pathUtils = require('path')
|
||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
|
|
||||||
const constants = require('../initializers/constants')
|
const constants = require('../initializers/constants')
|
||||||
const customValidators = require('../helpers/custom-validators')
|
const customVideosValidators = require('../helpers/custom-validators').videos
|
||||||
const logger = require('../helpers/logger')
|
const logger = require('../helpers/logger')
|
||||||
const utils = require('../helpers/utils')
|
const utils = require('../helpers/utils')
|
||||||
const webtorrent = require('../lib/webtorrent')
|
const webtorrent = require('../lib/webtorrent')
|
||||||
|
@ -39,18 +39,18 @@ const VideoSchema = mongoose.Schema({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
VideoSchema.path('name').validate(customValidators.isVideoNameValid)
|
VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
|
||||||
VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid)
|
VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
|
||||||
VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid)
|
VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
|
||||||
VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid)
|
VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
|
||||||
VideoSchema.path('author').validate(customValidators.isVideoAuthorValid)
|
VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
|
||||||
VideoSchema.path('duration').validate(customValidators.isVideoDurationValid)
|
VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
|
||||||
// The tumbnail can be the path or the data in base 64
|
// The tumbnail can be the path or the data in base 64
|
||||||
// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
|
// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
|
||||||
VideoSchema.path('thumbnail').validate(function (value) {
|
VideoSchema.path('thumbnail').validate(function (value) {
|
||||||
return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value)
|
return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
|
||||||
})
|
})
|
||||||
VideoSchema.path('tags').validate(customValidators.isVideoTagsValid)
|
VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
|
||||||
|
|
||||||
VideoSchema.methods = {
|
VideoSchema.methods = {
|
||||||
isOwned: isOwned,
|
isOwned: isOwned,
|
||||||
|
|
Loading…
Reference in New Issue