Skip to content

bulkCreate ignores virtual attribute validation #9947

Closed

Description

@ryanzyy

What are you doing?

  await User.bulkCreate([
      {
        email: '[email protected]',
        password: '123',
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    ],{ validate: true})

// In model attribute:
      password: {
        type: dt.VIRTUAL,
        set: function (val) { this.setDataValue('password', val)  },
        validate: { 
          len: [10,200],
          virtualFieldValidator: function() {
            console.log('---validator---') // never called
            return false;
          },
        },
      },

What do you expect to happen?

Expected Validation Error in promise

Add a new option of bulkCreate to explicitly enable virtual field validators.

{ 
  validate: true,
  validateVirtual: true,
}

Or

implicitly enable virtual field validators whenever 'validate' is set to true

What is actually happening?

User records are successfully created since validators of virtual field are ignored

Sequelize version: "4.38.1"

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. added
    type: bug
    on Oct 10, 2018
  2. sushantdhiman commented on Oct 10, 2018

    @sushantdhiman
    Contributor

    This happens because we are using tableAttributes for setting options.fields

    options.fields = options.fields || Object.keys(this.tableAttributes);
    which skips password (as VIRTUAL) from validation

    Not sure what are the implications of setting options.fields to rawAttributes but we can at least pass full fields when doing validation

    instance.validate(validateOptions).catch(err => {

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: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions