GithubHelp home page GithubHelp logo

alex-parra / sequelize-airtight Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 461 KB

Make your Sequelize models foolproof with extensive validation checks, setters and getters.

Home Page: https://www.npmjs.com/package/sequelize-airtight

License: MIT License

JavaScript 91.59% TypeScript 8.41%
sequelize nodejs validation mutators getters setters

sequelize-airtight's Introduction

Sequelize Airtight

Make your Sequelize models foolproof with extensive validation checks, setters and getters.

NPM version Sequelize v6 Sequelize v5 Downloads

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 or string and Date, or number 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 a Date 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 = 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 to dataValues

  • 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 as boolean)
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 or get
  • If set to true, trims leading and trailing white-space from strings
  • Returns value unchanged if it's not a string
  • If set to false returns value unchanged Ex: airtight: { set: { trim: false } }

upper since v0.5.0

airtight: { set: { upper: true } }
  • Allowed in set or get
  • If set to true, converts string to full uppercase. SeQuElIzE >> SEQUELIZE
  • Returns value unchanged if it's not a string
  • If set to false returns value unchanged Ex: airtight: { set: { upper: false } }

lower since v0.4.0

airtight: { set: { lower: true } }
  • Allowed in set or get
  • If set to true, converts string to full lowercase. SeQuElIzE >> sequelize
  • Returns value unchanged if it's not a string
  • If set to false returns value unchanged Ex: airtight: { set: { lower: false } }

decimals since v0.6.0

airtight: { set: { decimals: 2 } }
  • Allowed in set or get
  • Values are rounded eg. 12.344 >> 12.34 and 12.345 >> 12.35
  • If set to true, rounds value to 2 decimals
  • If set to number, rounds value to number decimals
  • Returns value unchanged if value is not float
  • If set to false returns value 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 to JavaScript in ./lib

Credit

Created by Alex Parra on Oct 17th, 2020.


sequelize-airtight's People

Contributors

alex-parra avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.