codecombat/server/commons/parse.coffee

65 lines
1.5 KiB
CoffeeScript
Raw Permalink Normal View History

errors = require '../commons/errors'
_ = require 'lodash'
Promise = require 'bluebird'
module.exports =
getLimitFromReq: (req, options) ->
options = _.extend({
max: 1000
default: 100
2016-03-30 16:57:19 -04:00
param: 'limit'
}, options)
limit = options.default
2016-03-30 16:57:19 -04:00
if req.query[options.param]
limit = parseInt(req.query[options.param])
valid = tv4.validate(limit, {
type: 'integer'
maximum: options.max
minimum: 1
})
if not valid
2016-03-30 16:57:19 -04:00
throw new errors.UnprocessableEntity("Invalid #{options.param} parameter.")
return limit
getSkipFromReq: (req, options) ->
options = _.extend({
max: 1000000
default: 0
2016-03-30 16:57:19 -04:00
param: 'skip'
}, options)
skip = options.default
2016-03-30 16:57:19 -04:00
if req.query[options.param]
skip = parseInt(req.query[options.param])
valid = tv4.validate(skip, {
type: 'integer'
maximum: options.max
minimum: 0
})
if not valid
2016-03-30 16:57:19 -04:00
throw new errors.UnprocessableEntity("Invalid #{options.param} parameter.")
return skip
getProjectFromReq: (req, options) ->
options = _.extend({}, options)
return null unless req.query.project
result = {}
project = req.query.project
if project is 'true'
result = {original: 1, name: 1, version: 1, description: 1, slug: 1, kind: 1, created: 1, permissions: 1}
else
if _.isString(project)
project = project.split(',')
for field in project
result[field] = 1
return result