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

koa2-json-schema

v1.0.0

Published

Middleware for validate request body by JSON schemas.

Downloads

29

Readme

Koa2 JSON Schema

Middleware for validate request body by JSON schemas.

Install

$ npm i koa2-json-schema

Tests

$ npm test

Basic usage

const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const jsonSchema = require('koa2-json-schema')()

const app = new Koa()
const router = new Router()

router.post('/users', jsonSchema({
  first_name: 'string',
  last_name: 'string',
  phone: 'number'
}), (ctx) => {
  // API...
})

app.use(bodyParser())
app.use(router.routes())

app.listen(3000)

Advanced usage

const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const jsonSchema = require('koa2-json-schema')()

const app = new Koa()
const router = new Router()

router.post('/users', jsonSchema({
  preferences: {
    type: 'array',
    items: {
      type: 'object',
      required: [
        'title',
        'description'
      ],
      properties: {
        title: 'string',
        description: 'string'
      }
    }
  }
}), (ctx) => {
  // API...
})

app.use(bodyParser())
app.use(router.routes())

app.listen(3000)

Error handling

By default, by sending the incorrect data in body, the server will return an error. This is the default error handler in koa2-json-schema.

ctx.status = 400
ctx.body = { error: errors }

But if you want to transfer control to the next middleware you must set the flag.

router.post('/users', jsonSchema({
  preferences: {
    type: 'array',
    items: {
      type: 'object',
      required: [
        'title',
        'description'
      ],
      properties: {
        title: 'string',
        description: 'string'
      }
    }
  }
}, true), (ctx) => { // flag is true
  // now you can handle errors from ctx.errors
  // these are errors if the body is empty
  // ctx.errors = [ 'preferences must be array' ]
})

Strict mode

You can enable strict mode which throw errors when client sent extra field in body. So, if you want enable strict mode, set true in third argument in jsonSchema function.

router.post('/users', jsonSchema({
  preferences: 'array'
}, true, true), (ctx) => {
  // ^ transfer errors is true, ^ strict mode is true (enable)
  
  // now you can handle errors from ctx.errors
  // these are errors if the body is valid preferences and unsued field age
  // ctx.errors = [ 'age field(s) are unused, you mustn\'t send them' ]
})

Locales

You can easily switch locales or create custom.

Native locales:

// import russian locales
const ru = require('koa2-json-schema/locales/ru')
// passed new locales into argument when require
const jsonSchema = require('koa2-json-schema')(ru)

Custom locales:

// %property% and %type% are dynamic variables
const jsonSchema = require('koa2-json-schema')({
  propertyIsRequired: '%property% is really required, bro 😞',
  invalidValueType: '%property% must be %type% type 😊'
})

License

MIT.