Chapter 6. Validation with Joi

 

This chapter covers

  • Working with Joi
  • Adding validation to hapi.js apps
  • Customizing validation errors
  • Consuming and presenting validation errors

Humans often make mistakes, and as a result the systems we create need to be prepared for misuse. Validation is an essential part of almost every system we use in our daily lives. An example of such a system is a snack vending machine.

A vending machine has several inputs that it needs to validate. If any one input doesn’t match its expectations, the machine will halt normal functioning and give feedback to the user on what went wrong. For instance, if you place a foreign coin in the slot, the machine will reject the coin, spitting it out into the coin return tray, as shown in figure 6.1.

Figure 6.1. An example of validation that occurs in vending machines

We rely on the feedback we get from validation to make sure we can use systems the correct way. Without it, we’d be clueless about what we’re doing wrong. We use the term validation to apply to software as well. This chapter shows you both the tools and methods hapi.js offers that allow you, in your applications, to define your expectations and give useful feedback to users when things don’t go according to plan.

6.1. Introducing Joi

6.2. Mastering Joi

6.3. Validation in hapi

6.4. Bringing it all together: web form validation with hapi and Joi

6.5. Summary

sitemap