ID photo of Ciro Santilli taken in 2013 right eyeCiro Santilli OurBigBook logoOurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
nodejs/sequelize/validation.js
#!/usr/bin/env node
// https://cirosantilli.com/sequelize-example
const assert = require('assert')
const { DataTypes, Op } = require('sequelize')
const common = require('./common')
const sequelize = common.sequelize(__filename, process.argv[2])
;(async () => {
// Datetime. Automatically converts to/from date objects.
const IntegerNames = sequelize.define('IntegerNames', {
  value: {
    type: DataTypes.INTEGER,
    validate: {
      isIn: [[1, 2]]
    },
  },
  name: {
    type: DataTypes.STRING,
  },
});
await IntegerNames.sync({force: true})
await IntegerNames.create({value: 1, name: 'one'})
await IntegerNames.create({value: 2, name: 'two'})
let err
try {
  await IntegerNames.create({value: 3, name: 'three'})
} catch(e) {
  err = e
}
assert(err instanceof sequelize.Sequelize.ValidationError)
})().finally(() => { return sequelize.close() })