Make your Sequelize models foolproof with extensive validation checks, setters and getters.
Sequelize includes builtin validation through validator.js and while there are several validators available, some are missing which can be implemented with custom validators.
But doing so in each project is error prone so that's one of the goals of this project.
Examples:
- ensure a field only accepts
stringorstringandDate, ornumberonly. - ensure a field isn't changed inadvertedly eg. auto-increment
idshould be read-only
And to ensure data is truly consistent, the usefulness of "normalizers" is indisputable and where better than close to the "metal".
As such, this project also packs mutators that allow data to be transformed on read and on write, ensuring for example:
- that
uniqueindexes aren't "fooled" by white-space or case differences - that all white-space strings aren't considered valid input
- that
numberbe set to aDatefield - etc...
Over 160 tests run against:
- Node 10.x, 12.x and 14.x
- Sequelize v5.x and v6.x
npm install sequelize-airtightWhere you initialize sequelize:
const airtight = require('sequelize-airtight');
const sequelize = new Sequelize({ ... });
// Init Airtight into sequelize. (must be called before loading models)
airtight.init(sequelize);In each model's attributes:
sequelize.define('MyModel', {
name: {
type: DataTypes.STRING,
allowNull: true, // or false
validate: { ... }, // as always
airtight: {
vet: { isType: 'string|Date' }, // these throw ValidationError
set: { trim: true }, // these run on set, before validation
get: { upper: true } // these run on get
}
}
})The airtight property of each field can contain the following:
-
vet– Validators that throw error if unmet
(verb - make a careful and critical examination of something) -
set- Mutators that transform the value on setting todataValues -
get- Mutators that transform the value when 'read'
use in airtight.vet
airtight: { vet: { isType: 'string' } }- Allowed in
vet - Throws
SequelizeValidationErrorif the value is not one of the specified {isType: 'string'}- Only allow setting a field to string{isType: 'string|number'}- only allow setting the field to string or number- If
valueis null or undefined check is skipped
Types supported:
stringsince v0.0.1
airtight: { vet: { isType: 'string' } }numbersince v0.2.0
airtight: { vet: { isType: 'number' } }boolsince v0.3.0 (aliased asboolean)
airtight: { vet: { isType: 'bool' } }use in airtight.set or airtight.get
airtight: { set: { trim: true } }- Allowed in
setorget - If set to
true, trims leading and trailing white-space from strings - Returns
valueunchanged if it's not astring - If set to
falsereturnsvalueunchanged Ex:airtight: { set: { trim: false } }
airtight: { set: { upper: true } }- Allowed in
setorget - If set to
true, converts string to full uppercase.SeQuElIzE>>SEQUELIZE - Returns
valueunchanged if it's not astring - If set to
falsereturnsvalueunchanged Ex:airtight: { set: { upper: false } }
airtight: { set: { lower: true } }- Allowed in
setorget - If set to
true, converts string to full lowercase.SeQuElIzE>>sequelize - Returns
valueunchanged if it's not astring - If set to
falsereturnsvalueunchanged Ex:airtight: { set: { lower: false } }
airtight: { set: { decimals: 2 } }- Allowed in
setorget - Values are rounded eg.
12.344>>12.34and12.345>>12.35 - If set to
true, rounds value to 2 decimals - If set to
number, rounds value tonumberdecimals - Returns
valueunchanged if value is notfloat - If set to
falsereturnsvalueunchanged Ex:airtight: { set: { decimals: false } }
- fork this repo and clone it locally
- run
npm install - run
npm testto run tests agaist the default version of sequelize (v6) - run
npm test:v5to run tests agaist sequelize v5.x - run
npm run buildto compile from TypeScript in./srctoJavaScriptin./lib
Created by Alex Parra on Oct 17th, 2020.