我试图修改Fastify路由为模式错误返回的内容,这里的文档如下: https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/#error-handling
我遵循示例的格式,但当我将{ schema, attachValidation: true }添加到路由时,它将否定验证。顺便说一句--我只是在更改模式,希望得到一个数字,然后在文本输入中输入“带鞋”,所以验证应该总是失败的。
如果我使用这个::
app.post("/create-item", { schema, attachValidation: true }, (request, reply) => {
if (request.validationError) {
console.log("error: ", request.validationError);
return;
}
}...the函数只是继续,并且console.log输出是“未定义的”;绕过了我的条件检查。当然,无论表单数据是否有效,Fastify的"stock“400响应都不会发生。
如果我使用这个::
app.post("/create-listing", schema, (request, reply) => {
它将返回“股票”Fastify 400错误。
有什么想法吗?是否需要配置或其他步骤来使attachValidation选项工作?
发布于 2022-10-31 08:19:19
Fastify应用字符串默认胁迫,所以如果设置"99",它将变成99。
这里有一个失败的例子:
const fastify = require('fastify')()
const schema = {
properties: {
foo: { type: 'number' }
}
}
fastify.post('/', {
attachValidation: true,
schema: { body: schema }
}, async (request, reply) => {
console.log({
body: request.body,
validation: request.validationError
})
return 'ok'
})
fastify.listen(8080)在这里卷发
curl -X 'POST' \
'http://localhost:8080/' \
-H 'Content-Type: application/json' \
-d '{
"foo": "string"
}'
ok
curl -X 'POST' \
'http://localhost:8080/' \
-H 'Content-Type: application/json' \
-d '{
"foo": "99"
}'
okhttps://stackoverflow.com/questions/74257076
复制相似问题