NodeJS 正在验证和序列化中检索数据,fastify出现问题

wr98u20j  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(132)

Fastify验证和序列化问题:

const Joi = require('@hapi/joi')

fastify.post('/the/url', {
  schema: {
    body: Joi.object().keys({
      hello: Joi.string().required()
    }).required()
  },
  validatorCompiler: ({ schema, method, url, httpPart }) => {
    return data => schema.validate(data)
  }
}, handler)

我的问题是如何使用在handler函数中验证过的数据。所以在验证req.body的数据之后:

, handler: (req,reply) => { // how can i retrieve the filtered data ? }

谢谢大家。

blmhpbnm

blmhpbnm1#

有一个路由选项:attachValidation

const fastify = require('./')()
const Joi = require('@hapi/joi')

fastify.post('/the/url', {
  schema: {
    body: Joi.object().keys({
      hello: Joi.string().required()
    }).required()
  },
  attachValidation: true,
  validatorCompiler: ({ schema, method, url, httpPart }) => {
    return data => schema.validate(data)
  }
}, (request) => {
  console.log(request.validationError)
})

fastify.inject({
  method: 'POST',
  url: '/the/url',
  payload: {}
})

相关问题