7.0.0-alpha.40 • Published 29 days ago

@sequelize/validator.js v7.0.0-alpha.40

Weekly downloads
-
License
MIT
Repository
github
Last release
29 days ago

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

Installation

Using npm:

npm install @sequelize/validator.js

Or using yarn:

yarn add @sequelize/validator.js

Usage

⚠️ 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;
}

List of validators

This package exports the following validators:

Contains

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;
}

NotContains

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;
}

Equals

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;
}

Is

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;
}

Not

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;
}

IsAfter

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;
}

IsBefore

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;
}

IsAlpha

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;
}

IsAlphanumeric

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;
}

IsCreditCard

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;
}

IsDate

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;
}

IsDecimal

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;
}

IsEmail

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;
}

IsFloat

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;
}

IsIP

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;
}

IsIPv4

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;
}

IsIPv6

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;
}

IsIn

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;
}

NotIn

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;
}

IsInt

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;
}

IsLowercase

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;
}

IsNumeric

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;
}

IsUUID

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;
}

IsUppercase

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;
}

IsUrl

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;
}

Length

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;
}

Max

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;
}

Min

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;
}

NotEmpty

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;
}
7.0.0-alpha.40

29 days ago

7.0.0-alpha.39

2 months ago

7.0.0-alpha.38

2 months ago

7.0.0-alpha.37

3 months ago

7.0.0-alpha.36

3 months ago

7.0.0-alpha.35

4 months ago

7.0.0-alpha.34

5 months ago

7.0.0-alpha.33

7 months ago

7.0.0-alpha.31

8 months ago

7.0.0-alpha.32

8 months ago

7.0.0-alpha.28

8 months ago

7.0.0-alpha.29

8 months ago

7.0.0-alpha.30

8 months ago

7.0.0-alpha.27

11 months ago

7.0.0-alpha.22

1 year ago

7.0.0-alpha.23

1 year ago

7.0.0-alpha.26

1 year ago

7.0.0-alpha.24

1 year ago

7.0.0-alpha.25

1 year ago

7.0.0-alpha.21

1 year ago