Server: reorganize express validators

This commit is contained in:
Chocobozzz 2016-07-31 20:58:43 +02:00
parent e62f6ef741
commit e4c556196d
8 changed files with 100 additions and 52 deletions

View File

@ -53,7 +53,7 @@ app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// Validate some params for the API
app.use(expressValidator({
customValidators: customValidators
customValidators: Object.assign({}, customValidators.misc, customValidators.users, customValidators.videos)
}))
// ----------- Views, routes and static files -----------

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -2,13 +2,13 @@
const validator = require('express-validator').validator
const constants = require('../initializers/constants')
const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
const constants = require('../../initializers/constants')
const usersValidators = require('./users')
const miscValidators = require('./misc')
const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS
const customValidators = {
exists: exists,
const videosValidators = {
isEachRemoteVideosValid: isEachRemoteVideosValid,
isArray: isArray,
isVideoAuthorValid: isVideoAuthorValid,
isVideoDateValid: isVideoDateValid,
isVideoDescriptionValid: isVideoDescriptionValid,
@ -21,10 +21,6 @@ const customValidators = {
isVideoThumbnail64Valid: isVideoThumbnail64Valid
}
function exists (value) {
return value !== undefined && value !== null
}
function isEachRemoteVideosValid (requests) {
return requests.every(function (request) {
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) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
return usersValidators.isUserUsernameValid(usersValidators)
}
function isVideoDateValid (value) {
@ -90,7 +74,7 @@ function isVideoPodUrlValid (value) {
}
function isVideoTagsValid (tags) {
return isArray(tags) &&
return miscValidators.isArray(tags) &&
validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
tags.every(function (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'
}

View File

@ -3,6 +3,23 @@
// API version of our pod
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
const FRIEND_SCORE = {
BASE: 100,
@ -55,29 +72,18 @@ const THUMBNAILS_SIZE = '200x110'
// Path for access to thumbnails with express router
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
if (isTestInstance() === true) {
FRIEND_SCORE.BASE = 20
INTERVAL = 10000
VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14
CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
}
// ---------------------------------------------------------------------------
module.exports = {
API_VERSION: API_VERSION,
CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
FRIEND_SCORE: FRIEND_SCORE,
INTERVAL: INTERVAL,
OAUTH_LIFETIME: OAUTH_LIFETIME,
@ -90,8 +96,7 @@ module.exports = {
SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL,
SORTABLE_COLUMNS: SORTABLE_COLUMNS,
THUMBNAILS_SIZE: THUMBNAILS_SIZE,
THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH,
VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS
THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH
}
// ---------------------------------------------------------------------------

View File

@ -4,7 +4,7 @@ const mongoose = require('mongoose')
const checkErrors = require('./utils').checkErrors
const constants = require('../../initializers/constants')
const customValidators = require('../../helpers/custom-validators')
const customVideosValidators = require('../../helpers/custom-validators').videos
const logger = require('../../helpers/logger')
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.')
}
if (!customValidators.isVideoDurationValid(duration)) {
return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).')
if (!customVideosValidators.isVideoDurationValid(duration)) {
return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
}
videoFile.duration = duration

View File

@ -9,7 +9,7 @@ const pathUtils = require('path')
const mongoose = require('mongoose')
const constants = require('../initializers/constants')
const customValidators = require('../helpers/custom-validators')
const customVideosValidators = require('../helpers/custom-validators').videos
const logger = require('../helpers/logger')
const utils = require('../helpers/utils')
const webtorrent = require('../lib/webtorrent')
@ -39,18 +39,18 @@ const VideoSchema = mongoose.Schema({
}
})
VideoSchema.path('name').validate(customValidators.isVideoNameValid)
VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid)
VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid)
VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid)
VideoSchema.path('author').validate(customValidators.isVideoAuthorValid)
VideoSchema.path('duration').validate(customValidators.isVideoDurationValid)
VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
// 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
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 = {
isOwned: isOwned,