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:
committed by
GitHub
parent
38323f858f
commit
3e00167ba4
@ -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.
|
||||
|
Reference in New Issue
Block a user