Files
freeCodeCamp/curriculum/test/utils/mongoIds.js
Naomi Carrigan fe71a9cf25 feat: new js cert (#45873)
* chore: move files

* wip: i have no idea what i am doing

* feat: finish up the logic

* chore: use order from randy sheet

* chore: standardise titles

* feat: fix layout for new js projects

* fix: utils test

* fix: challenge tests

* fix: ignore show new curriculum on non-english tests

* chore: help category????

* fix: if these tests would fail locally that'd be great

* chore: duplicate ids
2022-05-16 11:53:45 +02:00

41 lines
1.1 KiB
JavaScript

const Joi = require('joi');
const findIndex = require('lodash/findIndex');
Joi.objectId = require('joi-objectid')(Joi);
const schema = Joi.objectId();
const duplicatedProjectIds = [
'bd7158d8c442eddfaeb5bd18',
'587d78af367417b2b2512b03',
'587d78af367417b2b2512b04',
'587d78b0367417b2b2512b05',
'bd7158d8c242eddfaeb5bd13',
'aaa48de84e1ecc7c742e1124',
'a7f4d8f2483413a6ce226cac',
'56533eb9ac21ba0edf2244e2',
'aff0395860f5d3034dc0bfc9',
'aa2e6f85cab2ab736c9a9b24'
];
class MongoIds {
constructor() {
this.knownIds = [];
}
check(id, title) {
try {
schema.validate(id);
} catch {
throw Error(`Expected a valid ObjectId for ${title}, but got ${id}`);
}
const idIndex = findIndex(this.knownIds, existing => id === existing);
if (idIndex !== -1 && !duplicatedProjectIds.includes(id)) {
throw Error(`The id for challenge ${title} appears more than once.
With the exception of duplicatedProjectIds this should not happen.
`);
}
this.knownIds = [...this.knownIds, id];
}
}
module.exports = MongoIds;