New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

joi4express

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

joi4express - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

78

lib/index.js

@@ -12,4 +12,59 @@ 'use strict'

* @param {object} options - Joi's options
* @param {function} formatFcn - Optional function to format Joi Errors.
* Must return an object of the form:
* {
* message: 'Some Joi error message',
* details: [
* {
* // Joi error details
* }
* ]
* }
*/
module.exports = (route, options) => {
module.exports = (route, options, formatFcn) => {
// If no formatFcn is provided, define a default behavior
// which doesn't modify the error data
formatFcn = formatFcn || (err => ({
message: err.message,
details: err.details
}))
/**
* Attemps to run a user-defined error handler. This can be used to
* provide custom formatting to Joi Errors. If the user-defined handler
* throws an error for whatever reason, the original error message and
* details are returned (not taking into account the custom error handler)
* @param {Error} originalError The Joi Error
* @return {Object} returns an object of the form:
* {
* message: 'some Joi error message',
* details: [
* {
* // Joi error details
* },
* {}
* ]
* }
*/
function handleError (originalError) {
let message
let details
try {
const formattedDetails = formatFcn(originalError)
message = formattedDetails.message
details = formattedDetails.details
} catch (e) {
message = originalError.message
details = originalError.details
}
message = message || ''
details = details || []
return {
message,
details
}
}
// This function will be the actual route

@@ -26,5 +81,13 @@ return (req, res, next) => {

/**
* Called after a response validation. If an error occured,
* it is processed (formatted if necessary) and returned
* @param {Error} err
* @return Returns the response body if successful,
* else, calls the next() callback with a Boom Error
*/
function responseValidationDone (err) {
if (err) {
return next(Boom.badImplementation(err.message, err.details))
const { message, details } = handleError(err)
return next(Boom.badImplementation(message, details))
}

@@ -46,6 +109,15 @@

/**
* Called after a request validation. If an error occured,
* it is processed (formatted if necessary) and returned
* @param {Error} err
* @return Calls the route handler if successful,
* else, calls the next() callback with a Boom Error
*/
function requestValidationDone (err, request) {
request = request || {}
if (err) {
return next(Boom.badRequest(err.message, err.details))
const { message, details } = handleError(err)
return next(Boom.badRequest(message, details))
}

@@ -52,0 +124,0 @@

24

package.json
{
"name": "joi4express",
"version": "1.1.1",
"version": "1.2.0",
"description": "Joi validator for express, validates the request and the reponse",

@@ -8,3 +8,3 @@ "main": "lib/index.js",

"lint": "./node_modules/.bin/standard",
"test": "./node_modules/.bin/mocha test/**/*.js"
"test": "NODE_ENV=test node_modules/.bin/nyc --reporter=text --reporter html --cache false node_modules/.bin/mocha --timeout=60000 test/**/*.js --reporter mochawesome"
},

@@ -38,10 +38,22 @@ "repository": {

"chai": "^4.0.2",
"dirty-chai": "^1.2.2",
"joi": ">= 10.x.x",
"mocha": "^3.4.2",
"standard": "^10.0.2"
"mocha": "^2.3.3",
"nyc": "^13.3.0",
"standard": "^12.0.1",
"mochawesome": "^2.0.2",
"dirty-chai": "^2.0.1"
},
"standard": {
"env": "mocha"
"globals": [
"expect",
"sinon",
"assert",
"Promise",
"describe",
"it"
],
"env": [
"mocha"
]
}
}

@@ -53,2 +53,39 @@ # joi4express [![pipeline status](https://gitlab.com/xsellier/joi4express/badges/master/pipeline.svg)](https://gitlab.com/xsellier/joi4express/commits/master)

The library also accepts a Joi Options object, and a Custom Joi Error Formatting function.
In the following example, double quotes will be removed due to the custom error formatting function.
```js
const customJoiErrorFormatFcn = (e) => {
const message = e.message.replace(/"/g, '')
const details = e.details.map(detail => ({
...detail,
message: detail.message.replace(/"/g, '')
}))
return {
message,
details
}
}
const joiOptions = null // Or some desired options
app.get('/', joi4express(helloWorld, joiOptions, customJoiErrorFormatFcn))
```
Any custom formatting function must return an object of the form:
```js
{
message: 'a message string',
details: [
{
// Joi Error Details Object
}
]
}
```
If the user-defined formatting function fails, then the original message/details will be returned (as if the function was never defined in the first place)
## Installation

@@ -55,0 +92,0 @@

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