hapi supports request validation out of the box using the joi module. Request path parameters, payloads, and querystring parameters can be validated with joi's simple,

'use strict'
const Hapi = require('hapi')
const Joi = require('joi')
const server = new Hapi.Server()
server.connection({ port: 8000 }) server.route({
method: ['POST','PUT'],
path: '/user/{id?}',
config: {
validate: {
params: Joi.object().keys({
id: Joi.number()
}),
payload: Joi.object().keys({
id: Joi.number()
email: Joi.string()
}).unknown(),
query: Joi.object().keys({
id: Joi.number()
})
},
handler: function(request, reply) {
reply({
params: request.params,
query: request.query
payload: request.payload
})
}
}
}) server.start(() => console.log(`Started at: ${server.info.uri}`))

[Hapi.js] Request Validation with Joi的更多相关文章

  1. jquery.form.js+jquery.validation.js实现表单校验和提交

      一.jquery引用 主要用到3个js: jquery.js jquery.form.js jquery.validation.js 另外,为了校验结果提示本地化,还需要引入jquery.vali ...

  2. [Hapi.js] Extending the request with lifecycle events

    Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...

  3. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  4. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

  5. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  6. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

  7. [Hapi.js] Friendly error pages with extension events

    hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...

  8. [Hapi.js] Replying to Requests

    hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...

  9. [Hapi.js] Route parameters

    Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path param ...

随机推荐

  1. 命令行下如何安装VMware Tools并与windows资料共享

    安装VMware Tools: 选择菜单栏“虚拟机”——“安装VMware tools” ,等待系统自动更换ISO光盘 mount /dev/cdrom /mntcd /mnttar zxvf VMw ...

  2. Sql Server根据表名获得所有列及其属性

    select a.name columnname,c.name as typename,case when a.is_nullable =0 then 'Not Null' else 'Null' e ...

  3. SQL cmd 实用工具学习 -1

    启动 sqlcmd 实用工具并连接到 SQL Server 的默认实例 在"开始"菜单上,单击"运行". 在"打开"框中,键入 cmd,然后 ...

  4. C# 如何获取错误所在行数

    两种方法,一种是利用error.StackTrace,另外一种是try-catch找到错误行数,具体如下: 一.error.StackTrace代码 int i = ex.StackTrace.Ind ...

  5. 洛谷 P3367 【模板】并查集

    P3367 [模板]并查集 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数 ...

  6. CODEVS 3139 栈练习3

    3139 栈练习3  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保 ...

  7. [转发]Gulp开发教程(翻译)

    Building With Gulp =================== 转载出处 原文地址 翻译出处 对网站资源进行优化,并使用不同浏览器测试并不是网站设计过程中最有意思的部分,但是这个过程中的 ...

  8. js获取IP地址的方法小结

    s代码获取IP地址的三种方法,在js中取得客户端的IP地址. 原文地址:http://www.jbxue.com/article/11338.html 1,js取得IP地址的方法一 <scrip ...

  9. 文成小盆友python-num2 数据类型、列表、字典

    一.先聊下python的运行过程 计算机是不能够识别高级语言的,所以当我们运行一个高级语言程序的时候,就需要一个“翻译机”来从事把高级语言转变成计算机能读懂的机器语言的过程.这个过程分成两类,第一种是 ...

  10. discuz@功能的代码

    //转载 $atlist = $atlist_tmp = $ateduids = array(); preg_match_all("/@([^\r\n]*?)\s/i", $mes ...