chore(curriculum): replace validator with Joi (#39959)

We only used validator in one place and Joi could do the same job.
This commit is contained in:
Oliver Eyton-Williams
2020-10-13 13:55:24 +02:00
committed by GitHub
parent 38323f858f
commit 3e00167ba4
3 changed files with 11 additions and 12 deletions

View File

@ -1,15 +1,21 @@
const _ = require('lodash');
const { isMongoId } = require('validator');
const findIndex = require('lodash/findIndex');
const Joi = require('joi');
Joi.objectId = require('joi-objectid')(Joi);
const schema = Joi.objectId();
class MongoIds {
constructor() {
this.knownIds = [];
}
check(id, title) {
if (!isMongoId(id)) {
try {
Joi.validate(id, schema);
} catch {
throw new Error(`Expected a valid ObjectId for ${title}, but got ${id}`);
}
const idIndex = _.findIndex(this.knownIds, existing => id === existing);
const idIndex = findIndex(this.knownIds, existing => id === existing);
if (idIndex !== -1) {
throw new Error(`
All challenges must have a unique id.