我有一个带有以下端点的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对象将为空。否则,付款有一堆东西返回给我的条纹。
但是,如果我使用fastify-swagger记录上面的schema,现金支付的验证就会失败-例如,当payment设置为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?
发布于 2021-04-19 00:59:20
我找到了一个可行的解决方案:
payment: {
type: ['object', 'null'],
description: 'stripe payment object'
}https://stackoverflow.com/questions/67151083
复制相似问题