When updating, Sequelize performs validation on all fields / attributes (excepting those with allowNull set to true) - even if only a subset of fields are modified (via updateAttributes). Is it possible to only validate those fields that are to be updated? That is, to partially validate a model instance?
I have the following Users table:
create table users (
id serial primary key,
email varchar(192) not null,
username varchar(32) not null,
password char(32) not null,
unique(username)
);
Model definition:
// Inside User model definition:
email: { ... },
username: { ... },
password: {
type: DataTypes.STRING,
validate: {
is: {
// Reference: http://stackoverflow.com/a/1559788
args: ['(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+'],
msg: "Password must contain one at least one uppercase, lowercase, number, and special character"
}
}
},
All works as expected when creating and retrieving user records. Unfortunately, when I try to update an existing user's email address, the password validation fails. This happens because I store a hashed version (hexadecimal digest) of each user's password, which does not contain any special characters (required by the validation).
One possible workaround would be to only apply this validation when creating users; however, that introduces the problem of validating when the user changes their password.
Ideally, the password validation would only be applied when creating a new user or updating the password field for an existing user. Any ideas?