Unhandled rejection SequelizeValidationError: Validation error: Cannot create property 'allow_display_name' on boolean 'true'

Viewed 827

I have installed sequelize ORM for nodejs project and getting below error

Unhandled rejection SequelizeValidationError: Validation error: Cannot create property 'allow_display_name' on boolean 'true'

2 Answers

In my models I have put one validation for valid email where I have value like below

validate: {
        isEmail: {
          args: true,
          message: 'Please enter a valid Email address'
        }
      }

I just changed the arg: true to arg: false and its worked.

Tested as of Sequelize 6.5.1, you should just remove args from the validation. Also it should now be msg and not message. This gives:

validate: {
  isEmail: {
    msg: 'Please enter a valid Email address'
  }
}

Alternatively, if you didn't want a custom message, you can just use:

validate: {
  isEmail: true
}

args: is now only needed when the validator needs extra arguments, which is not the case for email, e.g.:

isIn: {
  args: [['en', 'zh']],
  msg: "Must be English or Chinese"
}

would ensure that the value is in the Array ['en', 'zh'], so we needed this extra argument to specify the validator. In those cases, if you don't want a custom error message, you can just pass the argument directly as in:

isIn: [['en', 'zh']],

Documented at: https://sequelize.org/master/manual/validations-and-constraints.html (archive and also mentioned on this ticket: https://github.com/sequelize/sequelize/issues/3770.

Note that most validators come from this third party library: Agithub.com/validatorjs/validator.js though that is not very clear in the docs. Their README shows that allow_display_name which is related to where it comes from.

BTW: the reason we have to do [['en', 'zh']] rather than just ['en', 'zh'] is that for array arguments, we have to differentiate between:

  • argument 1: 'en', argument 2: zh (not what we want)
  • argument 1: ['en', 'zh']

For non-Arrays, the typechecks just make an array with:

  • argument 1: [nonArray]

and splat that into validatejs. Coded here.

Related