Oliver Eyton-Williams 3e00167ba4
chore(curriculum): replace validator with Joi (#39959)
We only used validator in one place and Joi could do the same job.
2020-10-13 17:25:24 +05:30

31 lines
684 B
JavaScript

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) {
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);
if (idIndex !== -1) {
throw new Error(`
All challenges must have a unique id.
The id for ${title} is already assigned
`);
}
this.knownIds = [...this.knownIds, id];
}
}
module.exports = MongoIds;