{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/6662","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/6662/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/6662/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/6662/events","html_url":"https://github.com/sequelize/sequelize/issues/6662","id":181016698,"number":6662,"title":"Instance.validate not validating data type","user":{"login":"dsbert","id":1320090,"avatar_url":"https://avatars2.githubusercontent.com/u/1320090?v=4","gravatar_id":"","url":"https://api.github.com/users/dsbert","html_url":"https://github.com/dsbert","followers_url":"https://api.github.com/users/dsbert/followers","following_url":"https://api.github.com/users/dsbert/following{/other_user}","gists_url":"https://api.github.com/users/dsbert/gists{/gist_id}","starred_url":"https://api.github.com/users/dsbert/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsbert/subscriptions","organizations_url":"https://api.github.com/users/dsbert/orgs","repos_url":"https://api.github.com/users/dsbert/repos","events_url":"https://api.github.com/users/dsbert/events{/privacy}","received_events_url":"https://api.github.com/users/dsbert/received_events","type":"User","site_admin":false},"labels":[{"id":636646701,"url":"https://api.github.com/repos/sequelize/sequelize/labels/stale","name":"stale","color":"aaaaaa","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-10-04T22:03:02Z","updated_at":"2017-08-31T23:18:45Z","closed_at":null,"author_association":"NONE","body":"## What you are doing?\n\nI'm trying to validate an instance of a model before running an update. I have typeValidation set to true in Sequelize options. My model is defined as follows.\n\n``` js\nmodule.exports = function (sequelize, DataTypes) {\n  return sequelize.define('MyModel', {\n    itemId: {\n      type: DataTypes.INTEGER,\n      allowNull: false,\n      primaryKey: true\n    },\n    field1: {\n      field: 'MyField',\n      type: DataTypes.INTEGER,\n      allowNull: false,\n    },\n    field2: {\n      field: 'MyOtherField',\n      type: DataTypes.STRING,\n      allowNull: false,\n      defaultValue: 'Yes',\n      validate: {\n        isIn: [[ 'Yes', 'No' ]]\n      },\n      get: function () {\n        let val = this.getDataValue('field2');\n        return val && val.toLowerCase() === 'yes';\n      },\n      set: function (val) {\n        this.setDataValue('field2', val ? 'Yes' : 'No');\n      }\n    }\n  }, {\n    tableName: 'MyTable',\n    schema: 'MySchema',\n    timestamps: true,\n    updatedAt: 'LastUpdateTime',\n    createdAt: false\n  });\n};\n```\n\nHere is the code I'm running to validate the model.\n\n``` js\n    var instance= yield MyModel.findById(req.params.id);\n\n    // Initialize update object with default field values\n    // then apply incoming values from request\n    var replacement = MyModel.build({});\n    _.assign(replacement, { field1: 'not an integer' }, { itemId: instance.itemId});\n\n    var err = yield replacement.validate();\n    if (err) {\n      // expect field1 to fail validation due to data type\n      // but this does not happen\n      throw err;\n    }\n\n    var updateCount = yield instance.update(replacement.get({ plain: true }));\n\n    // validation error is thrown here indicating field1 should be an integer\n```\n## What do you expect to happen?\n\nI expect that instance.validate will perform the same validations as instance.update. However, \n## What is actually happening?\n\ninstance.validate is not performing a data type validation, when instance.update is. If I replace the value of field1 with null as below, then validate does return an error as expected.\n\n``` js\n_.assign(replacement, { field1: null }, { itemId: instance.itemId});\n```\n\n**Dialect:** sqlite / mssql\n**Sequelize version:** sequelize@3.24.3 \n"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/326445545","html_url":"https://github.com/sequelize/sequelize/issues/6662#issuecomment-326445545","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/6662","id":326445545,"user":{"login":"stale[bot]","id":26384082,"avatar_url":"https://avatars2.githubusercontent.com/u/26350515?v=4","gravatar_id":"","url":"https://api.github.com/users/stale%5Bbot%5D","html_url":"https://github.com/apps/stale","followers_url":"https://api.github.com/users/stale%5Bbot%5D/followers","following_url":"https://api.github.com/users/stale%5Bbot%5D/following{/other_user}","gists_url":"https://api.github.com/users/stale%5Bbot%5D/gists{/gist_id}","starred_url":"https://api.github.com/users/stale%5Bbot%5D/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stale%5Bbot%5D/subscriptions","organizations_url":"https://api.github.com/users/stale%5Bbot%5D/orgs","repos_url":"https://api.github.com/users/stale%5Bbot%5D/repos","events_url":"https://api.github.com/users/stale%5Bbot%5D/events{/privacy}","received_events_url":"https://api.github.com/users/stale%5Bbot%5D/received_events","type":"Bot","site_admin":false},"created_at":"2017-08-31T23:18:45Z","updated_at":"2017-08-31T23:18:45Z","author_association":"NONE","body":"This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂\n"}}
{"actor":{"display_login":"stale"}}