使用 fastify-swagger 指定一个可选的 'object' 参数

Specifying an optional 'object' parameter with fastify-swagger

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

  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 对象将为空。不然payment有一堆东西被Stripe还给我

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

错误如下:

"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?

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

      payment: {
        type: ['object', 'null'],
        description: 'stripe payment object'
      }