Sequelize Airtight
Make your Sequelize models foolproof with extensive validation checks, setters and getters.
Why
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
string
orstring
andDate
, ornumber
only. - ensure a field isn't changed inadvertedly eg. auto-increment
id
should 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
unique
indexes aren't "fooled" by white-space or case differences - that all white-space strings aren't considered valid input
- that
number
be set to aDate
field - etc...
Dependencies
Over 160 tests run against:
- Node 10.x, 12.x and 14.x
- Sequelize v5.x and v6.x
Install
npm install sequelize-airtight
Where you initialize sequelize
:
const airtight = ;const sequelize = ... ; // Init Airtight into sequelize. (must be called before loading models)airtight;
In each model's attributes:
sequelize
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'
Validators
use in airtight.vet
isType
since v0.0.1
airtight: vet: isType: 'string'
- Allowed in
vet
- Throws
SequelizeValidationError
if 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
value
is null or undefined check is skipped
Types supported:
string
since v0.0.1
airtight: vet: isType: 'string'
number
since v0.2.0
airtight: vet: isType: 'number'
bool
since v0.3.0 (aliased asboolean
)
airtight: vet: isType: 'bool'
readOnly
TODO
Mutators
use in airtight.set
or airtight.get
trim
since v0.0.1
airtight: set: trim: true
- Allowed in
set
orget
- If set to
true
, trims leading and trailing white-space from strings - Returns
value
unchanged if it's not astring
- If set to
false
returnsvalue
unchanged Ex:airtight: { set: { trim: false } }
upper
since v0.5.0
airtight: set: upper: true
- Allowed in
set
orget
- If set to
true
, converts string to full uppercase.SeQuElIzE
>>SEQUELIZE
- Returns
value
unchanged if it's not astring
- If set to
false
returnsvalue
unchanged Ex:airtight: { set: { upper: false } }
lower
since v0.4.0
airtight: set: lower: true
- Allowed in
set
orget
- If set to
true
, converts string to full lowercase.SeQuElIzE
>>sequelize
- Returns
value
unchanged if it's not astring
- If set to
false
returnsvalue
unchanged Ex:airtight: { set: { lower: false } }
decimals
since v0.6.0
airtight: set: decimals: 2
- Allowed in
set
orget
- Values are rounded eg.
12.344
>>12.34
and12.345
>>12.35
- If set to
true
, rounds value to 2 decimals - If set to
number
, rounds value tonumber
decimals - Returns
value
unchanged if value is notfloat
- If set to
false
returnsvalue
unchanged Ex:airtight: { set: { decimals: false } }
ifNull
TODO
Contributing
- fork this repo and clone it locally
- run
npm install
- run
npm test
to run tests agaist the default version of sequelize (v6) - run
npm test:v5
to run tests agaist sequelize v5.x - run
npm run build
to compile from TypeScript in./src
toJavaScript
in./lib
Credit
Created by Alex Parra on Oct 17th, 2020.