mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
018a44eeb5
[misc] normalize mongo imports GitOrigin-RevId: ac653d9982e0d36736b90f4c03d4c00be88ea76a
32 lines
716 B
JavaScript
32 lines
716 B
JavaScript
const { Joi: CelebrateJoi, celebrate, errors } = require('celebrate')
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const objectIdValidator = {
|
|
name: 'objectId',
|
|
language: {
|
|
invalid: 'needs to be a valid ObjectId'
|
|
},
|
|
pre(value, state, options) {
|
|
if (!ObjectId.isValid(value)) {
|
|
return this.createError('objectId.invalid', { value }, state, options)
|
|
}
|
|
|
|
if (options.convert) {
|
|
return new ObjectId(value)
|
|
}
|
|
|
|
return value
|
|
}
|
|
}
|
|
|
|
const Joi = CelebrateJoi.extend(objectIdValidator)
|
|
const errorMiddleware = errors()
|
|
|
|
module.exports = { Joi, validate, errorMiddleware }
|
|
|
|
/**
|
|
* Validation middleware
|
|
*/
|
|
function validate(schema) {
|
|
return celebrate(schema, { allowUnknown: true })
|
|
}
|