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 - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

8

index.js
"use strict";
const { ValidationError, ValidationErrorItem } = require("sequelize");
const Joi = require("joi");
module.exports = (sequelize) => {
const sequelizeJoi = (sequelize) => {
if (!sequelize) {

@@ -61,1 +62,6 @@ throw new Error("The required sequelize instance option is missing");

};
module.exports = {
sequelizeJoi,
Joi,
};

12

package.json
{
"name": "sequelize-joi",
"version": "2.0.0",
"version": "2.0.1",
"description": "Joi schema validation for Sequelize models",
"main": "index.js",
"license": "MIT",
"keywords": [
"sequelize",
"sequelize-plugin",
"sequelize-extension",
"joi"
],
"repository": {

@@ -26,5 +32,5 @@ "type": "git",

"devDependencies": {
"mysql2": "^2.3.2",
"pg": "^7.3.0"
"mysql2": "^2.0.0",
"pg": "^8.0.0"
}
}
# Sequelize Joi
Allows specifying [Joi](https://github.com/hapijs/joi) validation schema for `JSONB` model attributes in [Sequelize](https://github.com/sequelize/sequelize).
Allows specifying [Joi](https://github.com/sideway/joi) validation schema for model attributes in [Sequelize](https://github.com/sequelize/sequelize).
### Installation
```bash
npm install sequelize-joi
```
npm install --save sequelize-joi
```
### Usage
```js
const Sequelize = require("sequelize");
const sequelize = new Sequelize();
const sequelizeJoi = require("sequelize-joi");
sequelizeJoi(sequelize);
```
```javascript
const { Sequelize, DataTypes } = require("sequelize");
const { sequelizeJoi, Joi } = require("sequelize-joi");
Custom Joi object may be passed:
const database = new Sequelize({
...sequelizeConnectionOptions,
});
```js
const Joi = require("joi");
const CustomJoi = Joi.extend((joi) => {});
sequelizeJoi(sequelize, { Joi: CustomJoi });
```
sequelizeJoi(database);
### Example
```js
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(),
}),
}),
const User = database.define("User", {
username: {
type: DataTypes.STRING,
schema: Joi.string().trim().alphanum().min(6).max(30),
},
});
// Validation passes
await SampleModel.build({
details: {
requiredString: "One",
optionalString: "Two",
email: {
type: DataTypes.STRING,
schema: Joi.string().trim().required().email(),
},
})
.validate()
.then((instance) => {
// instance contains default values appended by Joi
});
// Validation fails
await SampleModel.build({
details: {
optionalString: 123,
password: {
type: DataTypes.STRING,
schema: Joi.string().trim().required().min(8),
},
})
.validate()
.catch((error) => {
// error is a 'SequelizeValidationError'
// error.errors is an array of 'SequelizeValidationErrorItem'
});
});
```
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