sequelize-joi
Advanced tools
Comparing version 2.0.0 to 2.0.1
"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, | ||
}; |
{ | ||
"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' | ||
}); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
52
0
4386
38