Mongoose schema set min and max dates

I want to create an “event” object, events obviously need to happen on a date, I want users to:

  1. not set a date in the past,
  2. and not less than 1 day in the future (haven’t tried to implement this)
  3. and not more than 3 months in the future

Pretty sure I need a function, the code below obviously does not work.

const mongoose = require("mongoose");


const eventSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 1,
    maxlength: 50,
    unique: true,
  },
  date: {
    type: Date,
    required: true,
    min: Date.now - 24 * 60 * 60 * 1000,
    max: Date.now + 90 * 24 * 60 * 60 * 1000,
  }

Answer

I managed to figure it out, using javascript getTime() method to get a timestamp in milliseconds then comparing that to points in the future (also in milliseconds).

const mongoose = require("mongoose");     

const eventSchema = new mongoose.Schema({
      date: {
        type: Date,
        required: true,
        validate: {
          validator: function (v) {
            return (
              v && // check that there is a date object
              v.getTime() > Date.now() + 24 * 60 * 60 * 1000 &&
              v.getTime() < Date.now() + 90 * 24 * 60 * 60 * 1000
            );
          },
          message:
            "An event must be at least 1 day from now and not more than 90 days.",
        }
      }})