【问题标题】:Hapi validation for req.user.id and payload idreq.user.id 和有效负载 id 的 Hapi 验证
【发布时间】:2018-06-12 18:32:42
【问题描述】:

我在 hapi 有简单的路线

const handler = async(request, reply) => {
  const id = Helpers.extractUserId(request)
  const payload = request.payload
  if (payload.recipient !== id) {
    // my code....
  } else {
    return reply({ success: false, message: '_id and recipient id should not match' })
    //I want to return this from routeConfig itself
  }
}

const routeConfig = {
  method: 'POST',
  path: '/requestfriend',
  config: {
    auth:'jwt',
    validate: {
      payload: {
        recipient: Joi.string().required().error(new Error('recipient is required'))
      }
    },
    handler
  }
}

我为if (payload.recipient !== id) 设置了一个条件,这意味着登录用户的 id 和有效负载接收者的相同,那么它应该抛出错误...

我不想这样做,我想把这个条件放在routeConfig 本身......那么这里有没有可以使用的参数,就像authvalidatehandler

【问题讨论】:

    标签: node.js validation hapijs joi


    【解决方案1】:

    嗯,这取决于你想做什么......如果你试图根据 id 来验证用户,我建议你检查the relevant part of the doc

    真的很容易实现:

    • 在你的 server.js 中定义一个 validate 函数
    • 在您的路由配置中添加身份验证选项

    如果验证函数返回 false,您的端点将返回 401 错误。

    【讨论】:

    • 您是否尝试将您的条件验证为 Joi 验证?这也许也是可能的。也许您只想对用户进行身份验证?
    • 你能举个例子吗
    【解决方案2】:

    您可以使用hapi route validation options 验证您的有效负载 这是符号。

    validate: {
        payload: async (value, options) => {
        }
    }
    

    这里是解释

    使用签名异步函数(值, 选项)其中:

    value - 包含请求查询参数的 request.payload 对象。

    选项 - 选项。

    如果返回一个值,则该值为 用作新的 request.payload 值并存储原始值 在 request.orig.payload 中。

    否则,有效负载保持不变。如果 抛出错误,根据failAction处理错误。

    这里是根据你的应用程序的示例代码,这只是一个转储示例,但我希望你能理解这里的想法。您可以在每个请求中单独验证有效负载、查询和参数对象。只需传递 Joi 验证器或函数来验证传入数据。

    const routeConfig = {
        method: 'POST',
        path: '/requestfriend',
        config: {
            auth: 'jwt',
            validate: {
                payload: async (value, options) => {
                    // extract recipient data from payload
                    const {recipient} = value;
                    // you can now validate your recipient
                    const result = Joi.validate({recipient}, Joi.string().required().error(new Error('recipient is required')), {abortEarly: false});
                    if(result.error) throw Boom.badRequest(result.error);
                    // there is no request object here, you have to dig in options.context parameter
                    // this is how it's look like
                    // {
                    //     context:
                    //         {
                    //             headers:
                    //                 {
                    //                     host: 'localhost:3009',
                    //                     'user-agent': 'curl/7.54.0',
                    //                     accept: '*/*',
                    //                     'content-length': '13',
                    //                     'content-type': 'application/x-www-form-urlencoded'
                    //                 },
                    //             params: {},
                    //             query: {x: 'y'},
                    //             auth:
                    //                 {
                    //                     isAuthenticated: false,
                    //                     isAuthorized: false,
                    //                     credentials: null,
                    //                     artifacts: null,
                    //                     strategy: null,
                    //                     mode: null,
                    //                     error: null
                    //                 },
                    //             app: {route: {}, request: {}}
                    //         },
                    //     abortEarly: false
                    // }
    
                    // let's say we got id parameters here
                    const id = Helpers.extractUserId(options.context);
                    if(id !== recipient) throw Boom.badRequest('_id and recipient id should not match')
                }
            },
            handler
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 2023-01-05
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多