2

I have the following code for my swagger documentation. Here I am trying to create an API where I want to take input as an array of objects. so far I am doing like following.

{
    method: 'POST',
    path: '/api/Route',
    handler: function(request, reply) {
        //some operations
        reply('Success');
    },

    config: {
        description: 'Create a Route',
        tags: ['api', 'user'],
        auth: 'UserAuth',
        validate: {
            payload: {
                "array": [{
                        userId: Joi.string().trim().required(),
                        status: Joi.number().required()
                    },
                    {
                        userId: Joi.string().trim().required(),
                        status: Joi.number().required()
                    },
                    {
                        userId: Joi.string().trim().required(),
                        status: Joi.number().required()
                    }
                ]
            }
        },
        plugins: {
            'hapi-swagger': {
                responseMessages: swaggerDefaultResponseMessages
            }
        }
    }
}

so what actually going on when I run the above code the swagger creates the documentation like this. this is a link to the image. so please can anybody tell me why I am not getting the whole array in swagger documentation instead getting only one element of the array. and I also saw the Following question but unable to understand in which file they are making these changes. Could anybody help? Thanks in advance.

1 Answer 1

2

You could use the array validation of Joi. Then in the input, you could just pass the array into it. for that, you just need to write the following code in your payload instead of what you are writing at present.

payload: {
         userData: Joi.array().items({
             userName: Joi.string(),
             status: Joi.string()
         })
     }

after that you swagger documentation looks something like this.

enter image description here

userData is your array of objects. and you could give input array of objects as following.

"userData": [{
             "userName": "string", // objects
             "status": "string"
         },
         {
             "userName": "string", // objects this way you can add more
             "status": "string"
         }
     ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.