concept Joi in category hapi

appears as: Joi, Joi
hapi.js in Action

This is an excerpt from Manning's book hapi.js in Action.

Joi is a validation library you’ll learn all about in the next chapter.

6.1. Introducing Joi

Joi is an npm package for data validation. Joi can validate any kind of JavaScript values, from simple scalar data types such as strings, numbers, or Booleans, to complex values consisting of several levels of nested objects and arrays. To run the examples in this chapter, install Joi like any other npm package with npm install joi@8. If you use a different version of Joi, some of the examples in this chapter may not work.

Figure 6.2. The steps taken when validating with Joi
Listing 6.6. Validating path parameters
const Hapi = require('hapi'); const Joi = require('joi'); const server = new Hapi.Server(); server.connection({ port: 4000 }); server.route({ method: 'GET', path: '/products/{id}', handler: function (request, reply) { reply('Success'); }, config: { validate: { params: { id: Joi.number().integer().min(1) } } } }); server.start(() => { console.log('Started server'); });