From 3e00167ba442fb489bb2a3abd7e2dbcd48cb3e6f Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 13 Oct 2020 13:55:24 +0200 Subject: [PATCH] chore(curriculum): replace validator with Joi (#39959) We only used validator in one place and Joi could do the same job. --- curriculum/package-lock.json | 6 ------ curriculum/package.json | 3 +-- curriculum/test/utils/mongoIds.js | 14 ++++++++++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/curriculum/package-lock.json b/curriculum/package-lock.json index af9de82389..8f6938a298 100644 --- a/curriculum/package-lock.json +++ b/curriculum/package-lock.json @@ -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", diff --git a/curriculum/package.json b/curriculum/package.json index a6af835376..297ecd3d66 100644 --- a/curriculum/package.json +++ b/curriculum/package.json @@ -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" } } diff --git a/curriculum/test/utils/mongoIds.js b/curriculum/test/utils/mongoIds.js index 2ef3c3a2f9..ee5e42d469 100644 --- a/curriculum/test/utils/mongoIds.js +++ b/curriculum/test/utils/mongoIds.js @@ -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.