1sequelize.define('foo', {
2 bar: {
3 type: DataTypes.STRING,
4 validate: {
5 is: /^[a-z]+$/i, // matches this RegExp
6 is: ["^[a-z]+$",'i'], // same as above, but constructing the RegExp from a string
7 not: /^[a-z]+$/i, // does not match this RegExp
8 not: ["^[a-z]+$",'i'], // same as above, but constructing the RegExp from a string
9 isEmail: true, // checks for email format (foo@bar.com)
10 isUrl: true, // checks for url format (http://foo.com)
11 isIP: true, // checks for IPv4 (129.89.23.1) or IPv6 format
12 isIPv4: true, // checks for IPv4 (129.89.23.1)
13 isIPv6: true, // checks for IPv6 format
14 isAlpha: true, // will only allow letters
15 isAlphanumeric: true, // will only allow alphanumeric characters, so "_abc" will fail
16 isNumeric: true, // will only allow numbers
17 isInt: true, // checks for valid integers
18 isFloat: true, // checks for valid floating point numbers
19 isDecimal: true, // checks for any numbers
20 isLowercase: true, // checks for lowercase
21 isUppercase: true, // checks for uppercase
22 notNull: true, // won't allow null
23 isNull: true, // only allows null
24 notEmpty: true, // don't allow empty strings
25 equals: 'specific value', // only allow a specific value
26 contains: 'foo', // force specific substrings
27 notIn: [['foo', 'bar']], // check the value is not one of these
28 isIn: [['foo', 'bar']], // check the value is one of these
29 notContains: 'bar', // don't allow specific substrings
30 len: [2,10], // only allow values with length between 2 and 10
31 isUUID: 4, // only allow uuids
32 isDate: true, // only allow date strings
33 isAfter: "2011-11-05", // only allow date strings after a specific date
34 isBefore: "2011-11-05", // only allow date strings before a specific date
35 max: 23, // only allow values <= 23
36 min: 23, // only allow values >= 23
37 isCreditCard: true, // check for valid credit card numbers
38
39 // Examples of custom validators:
40 isEven(value) {
41 if (parseInt(value) % 2 !== 0) {
42 throw new Error('Only even values are allowed!');
43 }
44 }
45 isGreaterThanOtherField(value) {
46 if (parseInt(value) <= parseInt(this.otherField)) {
47 throw new Error('Bar must be greater than otherField.');
48 }
49 }
50 }
51 }
52});
53
1key: {
2 // needs to be unique
3 type: DataTypes.UUID,
4 allowNull: false,
5 unique: true
6}
1sequelize.define('foo', {
2 bar: {
3 type: DataTypes.STRING,
4 validate: {
5
6 isEmail: true, // checks for email format (foo@bar.com)
7
8
9
10 // Examples of custom validators:
11 isEven(value) {
12 if (parseInt(value) % 2 !== 0) {
13 throw new Error('Only even values are allowed!');
14 }
15 }
16 isGreaterThanOtherField(value) {
17 if (parseInt(value) <= parseInt(this.otherField)) {
18 throw new Error('Bar must be greater than otherField.');
19 }
20 }
21 }
22 }
23});
24