npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@novice1/validator-joi

v0.11.0

Published

Joi validator to use with @novice1/routing.

Readme

@novice1/validator-joi

joi validator to use with @novice1/routing.

It provides a middleware that can validate req.params, req.body, req.query, req.headers, req.cookies and req.files against a schema using joi validation.

Installation

npm install @novice1/validator-joi

Usage

Set validator

// router.ts

import routing from '@novice1/routing'
import { validatorJoi } from '@novice1/validator-joi'

export default const router = routing()

router.setValidators(
  validatorJoi(
    // Joi.AsyncValidationOptions
    { stripUnknown: true },
    // middleware in case validation fails
    function onerror(err, req, res, next) {
      res.status(400).json(err)
    }
    // name of the property containing the schema
    'schema'
  )
)

Create schema

// schema.ts

import Joi from 'joi'
import { ValidatorJoiSchema } from '@novice1/validator-joi'
import router from './router'

// schema for "req.body"
const bodySchema = Joi.object({
  name: Joi.string().required()
}).required()

export const routeSchema: ValidatorJoiSchema = Joi.object().keys({
    body: bodySchema
})

// or
/*
export const routeSchema: ValidatorJoiSchema = {
    body: bodySchema
}
*/

// or
/*
export const routeSchema: ValidatorJoiSchema = {
    body: {                
       name: Joi.string().required()
    }
}
*/

Create route

import routing from '@novice1/routing'
import express from 'express'
import router from './router'
import { routeSchema } from './schema'

router.post(
  {
    name: 'Post item',
    path: '/items',

    parameters: {
        // the schema to validate
        schema: routeSchema
    },

    // body parser
    preValidators: express.json()
  },
  function (req: routing.Request<unknown, { name: string }, { name: string }>, res) {
    res.json({ name: req.body.name })
  }
)

Transformations

Data can be transformed with methods like Joi.string().trim() and more.

req.query being readonly, its tranformed values can be retrieved from req.validated().query.

For req.params, req.body, req.headers, req.cookies and req.files, the tranformation is applied on them and req.validated() can still be used if you prefer.

| | raw data | transformed | |---------------|-------------|---------------------------------------------| | params | | req.params or req.validated().params | | query | req.query | req.validated().query | | body | | req.body or req.validated().body | | headers | | req.headers or req.validated().headers | | cookies | | req.cookies or req.validated().cookies | | files | | req.files or req.validated().files |

/**
 * Since express@5 and @novice1/routing@2, req.query is readonly. 
 * The parsed and validated result can be found by calling the function 'req.validated()'.
 */
router.get(
  {
    name: 'Main app',
    path: '/app',
    parameters: {
      query: {
        version: Joi.number()
      }
    }
  },
  function (req, res) {
    res.json(req.validated?.<{ version?: number }>().query?.version)
  }
)

Overrides

Override the error handler for a route.

import routing from '@novice1/routing'
import router from './router'

const onerror: routing.ErrorRequestHandler = (err, req, res) => {
  res.status(400).json(err)
}

router.get(
  {
    path: '/override',
    parameters: {
      // overrides
      onerror

    },
  },
  function (req, res) {
    // ...
  }
)

References