首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在时尚工作中实现schemaListingCreate

如何在时尚工作中实现schemaListingCreate
EN

Stack Overflow用户
提问于 2022-10-30 22:01:35
回答 1查看 32关注 0票数 1

我试图修改Fastify路由为模式错误返回的内容,这里的文档如下: https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/#error-handling

我遵循示例的格式,但当我将{ schema, attachValidation: true }添加到路由时,它将否定验证。顺便说一句--我只是在更改模式,希望得到一个数字,然后在文本输入中输入“带鞋”,所以验证应该总是失败的。

如果我使用这个:

代码语言:javascript
复制
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选项工作?

EN

回答 1

Stack Overflow用户

发布于 2022-10-31 08:19:19

Fastify应用字符串默认胁迫,所以如果设置"99",它将变成99

这里有一个失败的例子:

代码语言:javascript
复制
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)

在这里卷发

代码语言:javascript
复制
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"    
}'
ok
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74257076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档