Reputation: 467
Suppose I have a User
model and a table named users
will be created based on that model.
I define its fields as follows.
User.init({
username: {....},
password: {....}
}, {sequelize, modelName: 'user'});
Let's suppose that I ask user to confirm their password (a very common practice), and want to validate if those two fields (password
and cpassword
) are identical, and I want to do it as part of Model validation using Model-wide validation, so my model will become something like this.
User.init({
username: {....},
password: {....},
cpassword: {...},
validate: {
checkIfEqual() {
if (cpassword !=== password) throw new Error("not same")
}
}
}, {sequelize, modelName: 'user'});
BUT Obviously I don't want to have the column cpassword in the users table.
I can check it manually, before calling User.Create, but I'd like to have that check as part of overall User model validation.
Is there any workaround for that?
Upvotes: 0
Views: 285
Reputation: 2315
I think what you're looking for is VIRTUAL
attributes. They're not stored inside your table but you can use them in your models
.
And I found great example on Sequelize documentation. Please check that out.
Upvotes: 1