Skip to content

Instantly share code, notes, and snippets.

@andrewjmead
Last active April 3, 2025 07:19
Show Gist options
  • Select an option

  • Save andrewjmead/a227b79c04af149b78f4 to your computer and use it in GitHub Desktop.

Select an option

Save andrewjmead/a227b79c04af149b78f4 to your computer and use it in GitHub Desktop.
Sequelize model validation
module.exports = function (sequelize, DataTypes) {
return sequelize.define('todo', {
description: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1, 250],
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Description must be a string');
}
}
}
},
completed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: function (value) {
if (typeof value !== 'boolean') {
throw new Error('Completed must be a boolean');
}
}
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment