Skip to content

Validation Rework #15497

Open

Description

@ephys

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Feature Description

Describe why you would like this feature to be added to Sequelize

The current way validators work isn't very typescript friendly.

It looks like this:

const User = sequelize.define('User', {
  email: {
    type: DataTypes.STRING,
    validate: {
      isEmail: true,
      isIn: [['[email protected]']]
    },
  },
});

We're adding validation decorators from sequelize-typescript. We have maybe 20 decorators, but validators.js proposes hundred of validators

We also have legacy validators that have been added for backward compat

Some validators, like isNull and notNull don't make sense in our context. Others are very badly named (such as is which does regexp matching)

Validators that accept an array as the first parameter require wrapping that array in another array due to a broken implementation (in, notIn).

Users also often use a different library than validator.js. Different validation libraries have different validation rules. email validation is not identical in validator.js and joi.

Describe the feature you'd like to see implemented

Make the validate attribute option accept an array of functions instead of a map. Each function will be called during validation.

Provide validation library adapters for popular libraries @sequelize/validator.js, @sequelize/joi, @sequelize/zod, @sequelize/class-validators. We'll start with just @sequelize/validator.js though.

Plan

In v7: Support the new validation system & deprecate the old one.
In v8: Remove the old validation system.

Validator Libraries

Here is what using these libraries could look like:

validatorjs with decorators:

import { IsEmail } from '@sequelize/validator.js';

class User extends Model {
  @IsEmail
  declare email: string;
}

validatorjs without decorators:

import { isEmail, isIn } from '@sequelize/validator.js';

const User = sequelize.define('User', {
  email: {
    type: DataTypes.string,
    // validate can be a function or an array of functions. Each function will be called when validating this attribute
    validate: [isEmail, isIn('[email protected]')],
  },
});

joi with decorators:

import { Validate } from '@sequelize/joi';
import { Joi } from 'joi';

class User extends Model {
  @Validate(Joi.string().email())
  declare email: string;
}

joi without decorators:

import { validate } from '@sequelize/joi';
import { Joi } from 'joi';

const User = sequelize.define('User', {
  email: {
    type: DataTypes.string,
    validate: validate(Joi.string().email()),
  },
});

class validators:

import { ClassValidatorPlugin } from '@sequelize/class-validators';
import { IsEmail } from 'class-validators';

class User extends Model {
  @IsEmail
  declare email: string;
}

new Sequelize({
  models: [User],
  // makes the validation system run class-validator's "validate" function
  plugins: [ClassValidatorPlugin],
});

class validators are not available without decorators

Extending the Validator System

Users should of course continue to be able to provide custom validators, just like they do today. The following should continue to work:

function notEmpty(value: unknown, user: User, attributeName: string) {
  // this function will run when this attribute is validated.
  if (name.length === 0) {
    throw new Error('Name cannot be empty');
  }
}

class User extends Model {
  @Attribute(DataTypes.STRING)
  @ValidateAttribute(notEmpty)
  declare name: string;
}

or without decorators:

import { isEmail, isIn } from '@sequelize/validator.js';

const User = sequelize.define('User', {
  name {
    type: DataTypes.string,
    validate: notEmpty,
  },
});

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s):

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.

Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    type: feature
    pending-approval
    on Dec 24, 2022
  2. self-assigned this
    on Dec 24, 2022
  3. removed
    pending-approval
    on Dec 24, 2022
  4. WikiRik commented on Dec 24, 2022

    @WikiRik
    Member

    Good write up! Validation is a nice first usage of a plugin system

  5. added theissue type on Mar 9, 2025
  6. removed
    type: feature
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions