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
6
curriculum/package-lock.json
generated
6
curriculum/package-lock.json
generated
@ -7437,12 +7437,6 @@
|
||||
"spdx-expression-parse": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"validator": {
|
||||
"version": "10.11.0",
|
||||
"resolved": "https://registry.npmjs.org/validator/-/validator-10.11.0.tgz",
|
||||
"integrity": "sha512-X/p3UZerAIsbBfN/IwahhYaBbY68EN/UQBWHtsbXGT5bfrH/p4NQzUCG1kF/rtKaNpnJ7jAu6NGTdSNtyNIXMw==",
|
||||
"dev": true
|
||||
},
|
||||
"value-or-function": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/value-or-function/-/value-or-function-3.0.0.tgz",
|
||||
|
@ -57,7 +57,6 @@
|
||||
"puppeteer": "^5.3.1",
|
||||
"readdirp-walk": "^1.7.0",
|
||||
"rx": "^4.1.0",
|
||||
"string-similarity": "^4.0.2",
|
||||
"validator": "^10.4.0"
|
||||
"string-similarity": "^4.0.2"
|
||||
}
|
||||
}
|
||||
|
@ -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