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

@ekino/express-validation

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ekino/express-validation

[![version](https://img.shields.io/npm/v/@ekino/express-validation.svg?style=flat-square)](https://www.npmjs.com/package/@ekino/express-validation)

  • 0.2.0-alpha.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

@ekino/express-validation

version

:warning: Work in progress :warning:

This package helps validating/normalizing incoming express API requests using Joi schemas in the form of express middlewares. When you add one of the provided middelwares to your app, it will try to validate the given source (body, path, query, headers) against a Joi schema, replace the source data with the validated data if validation passes or send back the validation errors along with a 400 HTTP status code otherwise.

Please be aware that the provided middlewares mutate the request's source data.

It can be used for several purposes, for example:

  • validating payload when attempting to write to your API
  • validating pagination/filters/sorting when retrieving a list of items
  • validating path parameters such as ids
  • validating tokens present in request headers (not the fact that they actually exists)

It's written in TypeScript, so there's no need to install external types if you're working on a TypeScript based project. However TypeScript is not required as the published package contains a compiled version.

It supports several sources:

You can easily adapt it to your needs using the configuration object.

Installation

You also have to install Joi as it's a peer dependency of this package.

yarn add joi @ekino/express-validation

Usage

Validating request body

import * as Joi from 'joi'
import { validateRequestBody } from '@ekino/express-validation'

app.post('/post', validateRequestBody(schema), (req, res) => {})

Validating request path

import * as Joi from 'joi'
import { validateRequestPath } from '@ekino/express-validation'

const schema = Joi.object().keys({
    id: Joi.number().required()
})

app.get('/post/:id', validateRequestPath(schema), (req, res) => {
    // now you're sure that `id` is a number,
    // it also have been casted to a number
    const { id } = req.params
})

Validating request query

import * as Joi from 'joi'
import { validateRequestQuery } from '@ekino/express-validation'

const schema = Joi.object().keys({
    sort: Joi.string().required()
})

app.get('/posts', validateRequestQuery(schema), (req, res) => {
    // assuming you made a request such as `GET /posts?sort=title`
    // now you're sure that `sort` exists
    const { sort } = req.query
})

Validating request headers

import * as Joi from 'joi'
import { validateRequestHeaders } from '@ekino/express-validation'

app.get('/posts', validateRequestHeaders(), (req, res) => {})

Configuration

You can completely customize the behaviour of the middlewares, this module can act as a simple bridge between Joi & express.

The available options are:

  • joiOptions
  • logger
  • errorStatusCode
  • errorBody
  • errorHandler

Let's now see which use cases can be covered using those options.

Customizing Joi options

Adding logging support

Customizing error response status code

By default, all the middlewares issue a 400 HTTP status code, the errorStatusCode option allows you to use another one.

app.get('/post/:id', validateRequestPath(schema, { errorStatusCode: 404 }), (req, res) => {
    // ...
})

Now, if the provided :id doesn't conform to schema, the client will receive a 404 HTTP status code.

You can also use a function to determine response status code, which can be useful if you have to add some extra logic to define it.

app.get(
    '/post/:id',
    validateRequestPath(schema, {
        errorStatusCode: (req: Request, error: ValidationError) => 401
    }),
    (req, res) => {
        /* ... */
    }
)

Customizing error response body

Using your own error handler

FAQs

Package last updated on 17 Feb 2019

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