sequelize: customizable allowNull error messages
something like this:
var User = sequelize.define('User', {
'email': {
type: DataTypes.STRING,
unique: true,
allowNull: { args: false, msg: 'Email is required.' },
validate: { isEmail: { msg: 'Invalid email.' } },
},
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 10
- Comments: 65 (21 by maintainers)
Can we reopen this? As of 4.2.0, @sushantdhiman 's solution does not work.
leads to
if firstName value is not null.
Thanks @timscott. The workaround worked as expected:
How does an ORM have a 3+ year old issue for customizing the validation error message when a required field is missing? This shouldn’t be labeled as a feature request…
Building off what @boulosda provided above, you can monkeypatch this from the bottom of
models/index.js
😭Getting this error
So what’s the proper way to set custom message on notNull ?
My workaround right now is
So any workaround to set custom validate error message when it’s null? The @timscott solution didn’t work for me
It looks like there’s isn’t a way at the moment, possibly an oversight of deprecating
notNull
?https://github.com/sequelize/sequelize/blob/d00f75df89a2e94178740d7893ce2418e0db5f61/lib/instance-validator.js#L325-L327
@PauloLira this has already been implemented, you need to use
>=5.0.0-beta.9
for the deprecated message to go away. See this comment above.The fix we are using replaces the
notNull
function in validator-extras with,validator.notNull = function(item){ return !validator.isNull(item); };
@samkelleher I just realize that even in master this message shows up. Just need send the field with value and the message appears. I saw that in master, file
validator-extras.js
, that we have this piece of code: ` // Deprecate this. validator.notNull = function() { throw new Error(‘Warning “notNull” validation has been deprecated in favor of Schema based “allowNull”’); };In some situations this function is been called and cause this.
`