Reputation: 301
I want to create an "event" object, events obviously need to happen on a date, I want users to:
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,
}
Upvotes: 1
Views: 1831
Reputation: 301
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.",
}
}})
Upvotes: 1