overleaf/services/web/app/src/infrastructure/Validation.js
Jakob Ackermann 018a44eeb5 Merge pull request #3185 from overleaf/jpa-normalize-mongo-imports
[misc] normalize mongo imports

GitOrigin-RevId: ac653d9982e0d36736b90f4c03d4c00be88ea76a
2020-09-25 02:04:20 +00:00

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 })
}