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 🙏

© 2024 – Pkg Stats / Ryan Hefner

joi-compiler

v1.1.0

Published

Build and manage the JOI instances for the fastify framework

Downloads

94

Readme

joi-compiler

js-standard-style Continuous Integration

Build and manage the joi instances for the Fastify framework.

It isolates the joi dependency so its version is not tightly coupled to the Fastify version. This allows the user to decide which version of joi to use in their Fastify based application.

Versions

| joi-compiler | joi | fastify | |---------------:|------:|----------:| | v1.x | v17.x | ^4.18 |

JOI Configuration

The joi's default configuration is:

{
  stripUnknown: true
}

To know all the possible options, please refer to the joi.validate() documentation.

Usage

This module is a Fastify schema compiler, it is used by Fastify to build the schema validator.
You can configure it by setting the Schema Controller, but let's see how to use it directly:

const fastify = require('fastify')
const JoiCompiler = require('joi-compiler')

// Instantiate the compiler
const factory = JoiCompiler()

// Install it to Fastify
const app = fastify({
  schemaController: {
    compilersFactory: {
      buildValidator: factory.buildValidator
    }
  }
})

// Use it!
app.get('/', {
  handler: echo,
  schema: {
    headers: factory.joi.object({
      foo: factory.joi.string().required()
    })
  }
})

Options

The JoiCompiler accepts an optional configuration object:

const factory = JoiCompiler({
  // optionally: provide all the JOI options you need
  prefs: {
    allowUnknown: true
  },

// optionally: an array with custom JOI extensions
  extensions: [
    require('@joi/date')
  ],

  // optionally: if you want to use the async validation. Default: false
  // Note that this option is supported ONLY by Fastify since v4.18.0
  asyncValidation: true
})

When you instantiate the compiler, the returned object has the following properties:

  • buildValidator: the function to pass to the schemaController option of Fastify
  • bucket: the object to support fastify.addSchema()
  • joi: a customized joi instance that contains the installed extensions.

Warning The async joi extensions are supported by Fastify since v4.18.0.

How to use ref

The joi module supports the ref feature to link the context configuration.

You can configure the context option by using the fastify.addSchema() method.
Note that, as the Fastify default compiler, this feature is fully encapsulated!

In this case you will need to use a special schema object:

app.addSchema({
  $id: 'x', // the ID you want to use
  $value: 42 // the value you want to store in the context
})

Here is an example:

const factory = JoiCompiler()

const app = fastify({
  schemaController: {
    bucket: factory.bucket,
    compilersFactory: {
      buildValidator: factory.buildValidator
    }
  }
})

app.addSchema({ $id: 'x', $value: 42 })

app.post('/', {
  handler: echo,
  schema: {
    body: Joi.object({
      a: Joi.ref('b.c'),
      b: { c: Joi.any() },
      c: Joi.ref('$x')
    })
  }
})

License

Licensed under MIT.