Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sequelize-joi

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-joi

Joi schema validation for Sequelize models

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Sequelize Joi

Allows specifying Joi validation schema for JSONB model attributes in Sequelize.

Installation

npm install --save sequelize-joi

Usage

const Sequelize = require("sequelize");
const sequelize = new Sequelize();
const sequelizeJoi = require("sequelize-joi");
sequelizeJoi(sequelize);

Custom Joi object may be passed:

const Joi = require("joi");
const CustomJoi = Joi.extend((joi) => {});
sequelizeJoi(sequelize, { Joi: CustomJoi });

Example

const SampleModel = sequelize.define("SampleModel", {
  details: {
    type: Sequelize.JSONB,
    allowNull: false,
    schema: Joi.object().keys({
      requiredString: Joi.string().required(),
      optionalString: Joi.string().default(null),
      optionalObject: Joi.object().keys({
        requiredSubNumber: Joi.number().required(),
      }),
    }),
  },
});

// Validation passes
await SampleModel.build({
  details: {
    requiredString: "One",
    optionalString: "Two",
  },
})
  .validate()
  .then((instance) => {
    // instance contains default values appended by Joi
  });

// Validation fails
await SampleModel.build({
  details: {
    optionalString: 123,
  },
})
  .validate()
  .catch((error) => {
    // error is a 'SequelizeValidationError'
    // error.errors is an array of 'SequelizeValidationErrorItem'
  });

FAQs

Package last updated on 03 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc