const { ObjectId } = require('bson');
const { MixedSchema } = require('yup');

class ObjectIdSchema extends MixedSchema {
  constructor() {
    super({ type: 'objectId' });

    this.withMutation((schema) => {
      schema.transform((value) => new ObjectId(value));
    });
  }

  static _typeCheck(value) {
    return ObjectId.isValid(value);
  }
}

module.exports = ObjectIdSchema;
