I have a sequelize model for a tour, which has a beginning and an ending:
this.model = db.define('Tour', {
id: {
type: Sequelize.BIGINT(20),
allowNull: false,
autoIncrement: true,
primaryKey: true
},
from_time: {
type: Sequelize.DATE,
allowNull: true
},
to_time: {
type: Sequelize.DATE,
allowNull: true
}
}
How can I make sure that to_time
is after from_time
?
(I guess validate
can be used, but I don't know how)
this
in validatar will reference to the current model. So it can be written like:
// ...
to_time: {
type: Sequelize.DATE,
allowNull: true,
validate: {
isAfterFrom: function(toTime) {
if (this.fromTime > toTime) {
throw new Error('To time must be less then from time')
}
}
}
},
// ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With