首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用fastify-swagger指定可选的'object‘参数

使用fastify-swagger指定可选的'object‘参数
EN

Stack Overflow用户
提问于 2021-04-19 00:48:03
回答 1查看 354关注 0票数 0

我有一个带有以下端点的fastify API服务。

代码语言:javascript
复制
  server.route({
    method: 'PUT',
    url: '/user/order/new',
    schema: {
      description: 'place a new order',
      body: {
        type: 'object',
        required: ['gross', 'name', 'surname', 'address', 'city', 'country', 'phone', 'email', 'cart'],
        properties: {
          gross: {
            type: 'number',
            description: 'order total'
          },
          name: {
            type: 'string',
            description: 'first name'
          },
          surname: {
            type: 'string',
            description: 'last name'
          },
          address: {
            type: 'string',
            description: 'street address'
          },
          city: {
            type: 'string',
            description: 'mailing city'
          },
          province: {
            type: 'string',
            description: 'state'
          },
          country: {
            type: 'string',
            description: 'country of residence'
          },
          phone: {
            type: 'string',
            description: 'telephone number'
          },
          email: {
            type: 'string',
            description: 'email address'
          },
          subscribe: {
            type: 'boolean',
            description: 'subscribe to newsletter'
          },
          privacy: {
            type: 'boolean',
            description: 'accepted privacy policy'
          },
          waiver: {
            type: 'boolean',
            description: 'accepted waiver'
          },
          card: {
            type: 'boolean',
            description: 'first name'
          },
          expires: {
            type: 'boolean',
            description: 'expiration date entered'
          },
          cvv: {
            type: 'boolean',
            description: 'CVV entered'
          },
          postal: {
            type: 'string',
            description: 'card billing zip'
          },
          cart: {
            type: 'array',
            description: 'shopping cart',
            items: {
              type: 'object',
              properties: {
                id: {
                  type: 'integer',
                  description: 'item id'
                },
                title: {
                  type: 'string',
                  description: 'entry title'
                },
                price: {
                  type: 'number',
                  description: 'price of a single item'
                },
                quantity: {
                  type: 'integer',
                  description: 'number of items of this kind'
                }
              }
            }
          },
          coupon: {
            type: 'string',
            description: 'discount code'
          },
          payment: {
            type: 'object',
            description: 'stripe payment object'
          }
        }
      },
      response: {
        200: {
          description: 'Successful response',
          type: 'object',
          properties: {
            id: {
              type: 'integer',
              description: 'the new order id'
            }
          }
        },
        500: {
          description: 'Internal Server Error',
          type: 'object',
          properties: {
            statusCode: { type: 'integer' },
            code: { type: 'integer' },
            error: { type: 'string' },
            message: { type: 'string' }
          }
        }
      }
    },
    handler: async (req, res) => {
      ...

我的处理程序代码运行得很好。处理程序内部的逻辑规定,如果卡标志设置为false,则处理现金支付,并且payment对象将为空。否则,付款有一堆东西返回给我的条纹。

但是,如果我使用fastify-swagger记录上面的schema,现金支付的验证就会失败-例如,当payment设置为null时。

错误如下:

代码语言:javascript
复制
"validation":[{"keyword":"type","dataPath":".payment","schemaPath":"#/properties/payment/type","params":{"type":"object"},"message":"should be object"}],"validationContext":"body"},"msg":"body.payment should be object","v":1}

我应该怎么做才能让payment接受'object‘或null?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-19 00:59:20

我找到了一个可行的解决方案:

代码语言:javascript
复制
      payment: {
        type: ['object', 'null'],
        description: 'stripe payment object'
      }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67151083

复制
相关文章

相似问题

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