Module @sequelize/validator.js

Sequelize logo

Sequelize

This library contains model attribute validators built on top of validator.js. Read more about model validation in the Sequelize documentation.

Using npm:

npm install @sequelize/validator.js

Or using yarn:

yarn add @sequelize/validator.js

⚠️ As indicated in the validator.js documentation, the library validates and sanitizes strings only.

To add validation to your model, decorate your model attributes with the decorators exported by this library.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsEmail } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsEmail
declare email: string;
}

This package exports the following validators:

Checks if the string attribute contains the seed.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Contains } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Contains('foo')
declare username: string;
}

Checks if the string attribute does not contain the seed.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { NotContains } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@NotContains('foo')
declare username: string;
}

Checks if the string attribute is exactly a value.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Equals } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Equals('foo')
declare username: string;
}

Checks if the string attribute matches a regex.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Is } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Is(/foo/)
declare username: string;
}

Checks if the string attribute does not match a regex.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Not } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Not(/foo/)
declare username: string;
}

Checks if the string attribute is a date that's after the specified date.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsAfter } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsAfter('2021-01-01')
declare createdAt: string;
}

Checks if the string attribute is a date that's before the specified date.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsBefore } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsBefore('2021-01-01')
declare createdAt: string;
}

Checks if the string attribute contains only letters (a-zA-Z).

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsAlpha } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsAlpha
declare username: string;
}

Checks if the string attribute contains only letters and numbers.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsAlphanumeric } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsAlphanumeric
declare username: string;
}

Checks if the string attribute is a credit card number.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsCreditCard } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsCreditCard
declare creditCard: string;
}

Checks if the string attribute is a date.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsDate
declare createdAt: string;
}

Checks if the string attribute is a decimal number.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsDecimal } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsDecimal
declare price: string;
}

Checks if the string attribute is an email.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsEmail } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsEmail
declare email: string;
}

Checks if the string attribute is a float number.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsFloat } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsFloat
declare price: string;
}

Checks if the string attribute is an IP address (4 or 6).

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsIP } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsIP(4) // optionally pass 4 or 6 to check for a specific IP version
declare ip: string;
}

Checks if the string attribute is an IPv4 address.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsIPv4 } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsIPv4
declare ipV4: string;
}

Checks if the string attribute is an IPv6 address.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsIPv6 } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsIPv6
declare ipV6: string;
}

Checks if the string attribute is in a array of allowed values.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsIn } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsIn(['admin', 'user'])
declare role: string;
}

Checks if the string attribute is not in a array of disallowed values.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { NotIn } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@NotIn(['admin', 'user'])
declare role: string;
}

Checks if the string attribute is an integer.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsInt } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsInt
declare age: string;
}

Checks if the string attribute is lowercase.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsLowercase } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsLowercase
declare username: string;
}

Checks if the string attribute is numeric.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsNumeric } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsNumeric
declare age: string;
}

Checks if the string attribute is a UUID.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsUUID } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsUUID(4) // UUID version is optional
declare uuid: string;
}

Checks if the string attribute is uppercase.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsUppercase } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsUppercase
declare username: string;
}

Checks if the string attribute is a URL.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { IsUrl } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@IsUrl
declare website: string;
}

Checks if the string attribute has a length between min and max.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Length } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Length([3, 10])
declare username: string;
}

Checks if the string attribute is not longer than the specified number of characters.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Max } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Max(10)
declare username: string;
}

Checks if the string attribute is not shorter than the specified number of characters.

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Min } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@Min(3)
declare username: string;
}

Checks if the string attribute is not an empty string (after trimming).

import { Model, DataTypes } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { NotEmpty } from '@sequelize/validator.js';

class User extends Model {
@Attribute(DataTypes.STRING)
@NotNull
@NotEmpty
declare username: string;
}

Modules

<internal>

Functions

Contains
Equals
Is
IsAfter
IsAlpha
IsAlphanumeric
IsArray
IsBefore
IsCreditCard
IsDate
IsDecimal
IsEmail
IsFloat
IsIn
IsInt
IsIP
IsIPv4
IsIPv6
IsLowercase
IsNumeric
IsUppercase
IsUrl
IsUUID
Len
Max
Min
Not
NotContains
NotEmpty
NotIn