mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 10:07:46 -05:00
28 lines
608 B
JavaScript
28 lines
608 B
JavaScript
const Joi = require('joi');
|
|
Joi.objectId = require('joi-objectid')(Joi);
|
|
|
|
const schema = Joi.objectId();
|
|
|
|
class MongoIds {
|
|
constructor() {
|
|
this.known = [];
|
|
}
|
|
check(id, block) {
|
|
try {
|
|
schema.validate(id);
|
|
} catch {
|
|
return `Expected a valid ObjectId for block ${block}, but got ${id}`;
|
|
}
|
|
|
|
const key = `${block}:${id}`;
|
|
|
|
const exists = this.known.includes(key);
|
|
this.known.push(key);
|
|
return exists
|
|
? `The id, ${id}, appears more than once in block ${block}. All the ids in a single block should be unique.`
|
|
: null;
|
|
}
|
|
}
|
|
|
|
module.exports = MongoIds;
|