【问题标题】:Fastify schema validation isn't working. Do I have something configured the wrong way?Fastify 模式验证不起作用。我是否以错误的方式配置了某些东西?
【发布时间】:2020-01-23 22:25:22
【问题描述】:

我试图弄清楚为什么架构验证在 Fastify 中不起作用。我有以下代码:

const postOptions = {
        schema: { 
            body: {
                type: 'object',
                properties: {
                    name: { type: 'string' },
                    parentId: { type: 'number' },
                    requiredKey: { foo: { type: 'string'} }
                }
            },
            response: {
                201: {
                    type: 'object',
                    properties: {
                        id: { type: 'number'},
                        name: { type: 'string'},
                        parentId: { type: 'number' }
                    }
                }
            }
        }
    }
    fastify.post('/sponsor', postOptions, async (request, reply) => {

        console.log(`POST /sponsor called`)

        return { id: 2, name: 'Zenotis', parentId: 1 }
    })

当我使用邮递员对其进行测试时,我可以将任何键和值与正文一起发送,并且一切正常。好像根本不检查。与响应相同。我正在使用 Fastify 版本 2.11.0

编辑:这是我要发送的 json 正文:

{
  "name": "Test",
  "parentId": 5555555,
  "foo": "bar"
}

这是我预计会失败的:

{
  "myName": "the field is not name",
  "parentID": "The D is capitalized and this is a string",
  "bar": "where did this field come from, it's not foo"
}

如果我发送这个身体,它会顺利通过。如何将其配置为在所有这些情况下都失败?

【问题讨论】:

  • 您可以添加例如您发送的请求吗?
  • 请同时提供您希望通过验证和验证失败的数据示例=]
  • 更新了 json 负载

标签: javascript jsonschema fastify


【解决方案1】:

您的架构使用需要应用一些修复:

  • 如果您不设置状态码 201,您设置的响应模式将不起作用。使用'2xx' 或在reply 对象中设置正确的代码
  • 要删除不在架构中的字段,您需要添加additionalProperties
  • 如果架构中不设置required字段,则所有字段都是可选的

这里是一个阻塞示例:


const fastify = require('fastify')()
const postOptions = {
  schema: {
    body: {
      type: 'object',
      additionalProperties: false, // it will remove all the field that is NOT in the JSON schema
      required: [
        'name',
        'parentId',
        'requiredKey'
      ],
      properties: {
        name: { type: 'string' },
        parentId: { type: 'number' },
        requiredKey: { foo: { type: 'string' } }
      }
    },
    response: {
      201: {
        type: 'object',
        properties: {
          id: { type: 'number' },
          name: { type: 'string' },
          parentId: { type: 'number' }
        }
      }
    }
  }
}
fastify.post('/sponsor', postOptions, async (request, reply) => {
  console.log('POST /sponsor called')
  reply.code(201) // if you don't set the code 201, the response schema you set will not work
  return request.body
})

fastify.inject({
  method: 'POST',
  url: '/sponsor',
  payload: {
    name: 'Test',
    parentId: 5555555,
    foo: 'bar'
  }
}, (_, res) => {
  console.log(res.json())
    /* it will print
    {
      statusCode: 400,
      error: 'Bad Request',
      message: "body should have required property 'requiredKey'"
    }
    */

})

【讨论】:

  • 我试试看。
  • @manuel,感谢您的明确回复。关于设置代码,请您指出解释此规则的文档中的哪个位置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
相关资源
最近更新 更多