Skip to content

customizable allowNull error messages #1500

Closed

Description

@cjroth

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.' } },
    },

Activity

  1. fixe commented on Mar 22, 2014

    @fixe
    Contributor

    My suggestion:

    • allowNull should be independent from the validation component.
    • Create a PR in chriso/validator.js adding the isNotNull validator.
    • Use the isNotNull validator as any other one.
    var User = sequelize.define('User', {
      'email': {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false,
        validate: {
          isEmail: { msg: 'Invalid email.' },
          isNotNull: { msg: 'The email is required' }
        }
      }
    })
  2. nunofgs commented on Apr 1, 2014

    @nunofgs
    Contributor

    +1. @mickhansen what do you think?

  3. mickhansen commented on Apr 1, 2014

    @mickhansen
    Contributor

    allowNull is currently it's own type of validator, a schema validator - So it doesn't really follow the same logic as regular validators.

    But yeah we can easily extend the validator in our own codebase (afaik PRs are hardly ever accepted to validator.js): https://github.com/sequelize/sequelize/blob/master/lib/instance-validator.js#L19

  4. mickhansen commented on Apr 1, 2014

    @mickhansen
    Contributor

    @fixe @nunofgs you should come hang out in IRC too, #sequelizejs @ freenode

  5. olive75 commented on Jun 12, 2014

    @olive75

    +1, nice feature

  6. tiblu commented on Oct 1, 2014

    @tiblu
    Contributor

    +1 to add custom message for "allowNull".

  7. bushev commented on Dec 12, 2014

    @bushev

    +1

  8. jackwilsdon commented on Dec 31, 2014

    @jackwilsdon

    +1

  9. epsyan commented on Jan 10, 2015

    @epsyan

    +1

  10. goodthing commented on Jan 20, 2015

    @goodthing

    +1

  11. cithukyaw commented on Feb 16, 2015

    @cithukyaw

    What is the solution for this? notEmpty or notNull did not work.

    travelTime: {
        type: Sequelize.INTEGER,
        allowNull: false,
        validate: {
            notNull: { msg: "Travel time is required" },
            isNumeric: true
        }
    }
    

    or

    travelTime: {
        type: Sequelize.INTEGER,
        allowNull: false,
        validate: {
            notEmpty: { msg: "Travel time is required" },
            isNumeric: true
        }
    }
    

    My sequelize version is 2.0.2.

  12. cithukyaw commented on Feb 16, 2015

    @cithukyaw

    @epsyan Thanks. I have seen that workaround. I expect the Sequelize to be fixed internally, otherwise I would have to use the hook in every schema that is not efficient.

    [Edit]
    Just tried that workaround is not working in 2.0.2.

  13. 43 remaining items

  14. ericsorenson commented on Nov 2, 2017

    @ericsorenson

    👍

  15. DmytroMysak commented on Nov 6, 2017

    @DmytroMysak

    +1

  16. brsalviano commented on Nov 29, 2017

    @brsalviano

    Thanks @timscott. The workaround worked as expected:

        name: {
            type: Sequelize.STRING(50),
            allowNull: false,
            defaultValue: '',
            validate: {
                notEmpty: {
                    msg: 'The name is required.'
                }
            }
        }
    
  17. tybro0103 commented on Jan 13, 2018

    @tybro0103

    +1

  18. dydome commented on May 10, 2018

    @dydome

    Is there any update on it?

  19. acodercat commented on Oct 15, 2018

    @acodercat
     username: {
          type: STRING(32), allowNull: false, unique: true,
          validate: {
            notNull: { args: true, msg: "用户名不能为空" },
          }
        },

    {
    "message": "Warning "notNull" validation has been deprecated in favor of Schema based "allowNull""
    }

  20. sushantdhiman commented on Oct 15, 2018

    @sushantdhiman
    Contributor

    @acodercat only 5.0.0-beta.9 or above

  21. acodercat commented on Oct 15, 2018

    @acodercat

    @acodercat only 5.0.0-beta.9 or above

    How should I modify allowNull validate msg now?

  22. sushantdhiman commented on Oct 15, 2018

    @sushantdhiman
    Contributor

    @acodercat Not possible in v4, there are some hacks discussed in this thread though

  23. Marcosaurios commented on Dec 7, 2018

    @Marcosaurios

    So any workaround to set custom validate error message when it's null? The @timscott solution didn't work for me

  24. jackwilsdon commented on Jan 31, 2019

    @jackwilsdon

    @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.

  25. xuxuewen commented on Jul 12, 2019

    @xuxuewen
    var User = sequelize.define('User', {
        email: {
          type: DataTypes.STRING,
          unique: true,
          allowNull: { args: false, msg: 'Email is required.' }, // not work
          validate: { isEmail: { msg: 'Invalid email.' } },
        },
    
    // modify
    email:{
      type:Data.Types.STRING,
     unique:true,
     allowNull:false,
     validate:{
       notNull:{
          msg:'this is custom allowNull error message'
       }
     }
    }
  26. mafei-dev commented on Jan 4, 2020

    @mafei-dev

    @cjroth
    You can use validate with isNull. if value is null, you can throw an error with custom message like bellow. (allowNull must be true)
    input

    Then your output will be appear like this .

    output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions