Git Product home page Git Product logo

micro-joi's Introduction

⚠️ Deprecated ⚠️

Build Status npm

micro-joi

A Joi wrapper for Micro to validate your request body and query parameters.

It's possible to validate both body and query parameters, or only one of these. To validate both, use body and query key in the schema:

Joi.object({
    body: Joi.object({
        ...
    }),
    query: Joi.object({
        ...
    })
});

To keep api backward compatible, you can write the shape of your request body directly, look at the examples below.

Examples

const { json, send } = require('micro')
const validation = require('micro-joi')
const Joi = require('@hapi/joi')

const validator = validation(Joi.object({
    foo: Joi.number().required(),
    bar: Joi.number().required()
}))

async function handler (req, res) {
  const body = await json(req)
  send(res, 200, body)
}

module.exports = validator(handler)

Sending a POST with a wrong body, e.g. { foo: 42, bar: "fortytwo" }, will return an error with a Joi validation message, status code 400.

or with custom message

const { json, send } = require('micro')
const validation = require('micro-joi')
const Joi = require('@hapi/joi')

const validator = validation(Joi.object({
    foo: Joi.number().required(),
    bar: Joi.number().required()
}), 'hei! send a correct body plz')

async function handler (req, res) {
  const body = await json(req)
  send(res, 200, body)
}

It will return an error with your custom message, status code 400.

micro-joi's People

Contributors

cyberwombat avatar dependabot[bot] avatar stearm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

micro-joi's Issues

Maintain query data from joi into handler

Joi has the ability to transform data - for example it automatically casts 'true' as true. However micro-joi only validates but doesn't overwrite the original query like it does with the body. Would that be possible?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.