0

我在 joi 语法上定义了一个名为 TestBean 的 bean。然后定义了另一个使用 TestBean 模式/bean 的 bean BeanMethodDocument。生成的 Swagger/model 会忽略此参数,但使用 TestBean 定义的数组有效吗?

以下 JOI 语法似乎丢失了 TestBean 定义:“arg: joi.object().schema(TestBean).required()”

'use strict';
var createRouter = require('@arangodb/foxx/router');
var joi = require('joi');
var router = createRouter();

module.context.use(router);

const TestBean = joi.object().required().keys({
    member1: joi.array().items(joi.string().required()),
    member2: joi.number().required()
});

const BeanMethodDocument = joi.object().required().keys({
    arg: joi.object().schema(TestBean).required(),
    argArray: joi.array().items(TestBean).required(),
    option: joi.string().valid('Empty','Full','HalfFull','HalfEmpty')
});


router.post('/beanMethod', function (req, res) {
    const arg = req.body.arg;
    const argArray = req.body.argArray;
    const option = req.body.option;

    res.send({result:true});
})
    .body(BeanMethodDocument, 'beanMethod POST request')
    .response(joi.boolean().required(), 'beanMethod POST response')
    .summary('beanMethod summary')
    .description('beanMethod description');

生成的 Swagger 文档显示 arg 参数为空?"arg": {
"type": "object",
"properties": {},
"additionalProperties": false },

4

1 回答 1

0

根据 JOI 文档(https://github.com/hapijs/joi/blob/v15.1.0/API.md#objectschema),您在那里使用的架构函数确实返回了当前对象的架构,它没有设置它. 您可以像这样简单地使用 TestBean 而不是 joi.object() :

const BeanMethodDocument = joi.object().required().keys({
  arg: TestBean.required(),
  argArray: joi.array().items(TestBean).required(),
  option: joi.string().valid('Empty','Full','HalfFull','HalfEmpty')
});

在我的本地测试中,这项工作最终得到:

{
  "arg": {
    "member1": [
      "string"
    ],
    "member2": 0
  },
  "argArray": [
    {
       "member1": [
         "string"
       ],
       "member2": 0
    }
  ],
  "option": "Empty"
}
于 2019-08-08T13:54:52.633 回答