Skip to content

instance.validate does not do typeValidation #11266

Closed

Description

@simonbrent

What are you doing?

const sequelize = new Sequelize('database', 'username', 'password', { typeValidation: true });
const model = sequelize.define('foo', {
  bar: { type: Sequelize.INTEGER },
});
model.build({ id: 1, bar: 'bar' }).validate();
model.create({ id: 1, bar: 'baz' });

To Reproduce
Run the code above

What do you expect to happen?

When creating a Sequelize instance with typeValidation turned on, I expect that types are validated when calling the validate method on an instance. I expect output from the above code
to be two SequelizeValidationErrors - one saying '"bar" is not a valid integer' and one saying '"baz" is not a valid integer'.

What is actually happening?

Only one error way thrown - the one saying '"baz" is not a valid integer'.
This is because the InstanceValidator does not do anything with typeValidation, while the QueryGenerator (which isn't being used in the build/validate process) does.

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any
    Dialect library version: 10.5
    Database version: 10
    Sequelize version: 5.10.0
    Node Version: 10.16.0
    OS: Ubuntu 16.04

Tested with latest release:

  • No (but looking at the code on github, nothing seems to have changed with regards to typeValidation since 5.10.0)
  • Yes, specify that version:

Activity

  1. added
    status: understood
    type: bug
    docs
    status: awaiting response
    type: feature
    and removed
    status: understood
    type: bug
    docs
    on Aug 1, 2019
  2. papb commented on Aug 1, 2019

    @papb
    Member

    Actually, I have just checked the docs, what is says for options.typeValidation is this:

    Run built in type validators on insert and update, e.g. validate that arguments passed to integer fields are integer-like.

    So apparently I think your example behaves like the docs say it should.

    However, I agree this might be a valid change...

  3. added
    status: in discussion
    and removed
    status: awaiting response
    on Aug 1, 2019
  4. simonbrent commented on Aug 2, 2019

    @simonbrent
    Author

    I think the behaviour as it stands is problematic because all the write functions in model.js (save, update, upsert, bulkCreate) start by doing instance.validate if options.validate is true. It should therefore be possible to skip a lot of code if type validation fails, instead of finding out much later that the query is invalid.

  5. simonbrent commented on Nov 3, 2020

    @simonbrent
    Author

    I have come across another problem relating to this issue.

    If you are doing bulkCreate with an array of data containing multiple entries with type validation errors, the way typeValidation works results in a ValidationError being thrown containing only the details of the first validation error, rather than a BulkRecordError like you get for other types of validation.

  6. github-actions commented on Nov 8, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  7. simonbrent commented on Nov 10, 2021

    @simonbrent
    Author

    This is still an issue with version 6.9.0

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

    status: in discussiontype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions