Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to suppress usage of use of property setter methods in case they use instance methods [feedback wanted] #88

Open
matmar10 opened this issue Jul 22, 2018 · 1 comment

Comments

@matmar10
Copy link
Collaborator

matmar10 commented Jul 22, 2018

Proposed Feature

Adds a fixture-level flag to disable the user of setter methods. This is a necessary feature if your model's setter methods us instance methods other than simple this.setDataValue.

The new flag will be called ignoreSet or something like:

  • ignoreSetter
  • ignoreSetters
  • ignorePropertySetter
  • ignorePropertySetters
  • Something else? Thoughts on a better name?

Usage

  - model: User
    ignoreSet: true
    data:
      email: [email protected]

Other Thoughts

Considered putting this under an options object as:

  - model: User
    options:    
      ignoreSet: true
    data:
      email: [email protected]

But so far there are no other sequelize-fixtures specific options (the options are pass-through options for sequelize build / create methods only.

Background

If you use property setters, this library attempts to feed values from fixtures into them in case there is some coercion or formatting, etc.

The Problem

This library calls the setters in a simulated way, without a real object instance.

Steps to reproduce

  • Define a model with at least one set method using any instance method inside (e.g. this.previous or anything except this.setDataValue).

Example:

  const User = sequelize.define('User', {
    id: {
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER
    },
    email: {
      type: DataTypes.STRING,
      validate: {
        isEmail: true
      },
      set: function set(val) {
        if (this.previous('email')) {
          throw new Error('Updating email is not allowed');
        }
        this.setDataValue('email', val);
      }
    }
  });

Actual behavior

[TypeError: this.previous is not a function

Expected behavior: using defined instance methods should not fail

// caused by this:
Model.rawAttributes[k].set.call(Model, data[k]);
matmar10 added a commit to matmar10/sequelize-fixtures that referenced this issue Jul 22, 2018
@matmar10
Copy link
Collaborator Author

Fixed by #100

@matmar10 matmar10 changed the title Use of instance methods in setter methods breaks import Add flag to suppress usage of use of property setter methods in case they use instance methods [feedback wanted] Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant