Skip to content
This repository has been archived by the owner on Jan 31, 2025. It is now read-only.

Feature/208 deep object #211

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ const create = function (options = {}) {
schema,
routeExt: function (request, h) {
const p = parameter.in === 'query' ? 'query' : 'params';

if (parameter.style === 'deepObject') {
request[p] = Object.keys(request[p]).reduce((data, key) => {
const match = key.match(new RegExp(`^${parameter.name}\\[([^\\]]*)]$`));
if (match) {
data[parameter.name] = {
...data[parameter.name] || {},
[match[1]]:request[p][key]
};
delete request[p][key];
}

return data;
},request[p]);
}

if (request[p][parameter.name] !== undefined) {
request[p][parameter.name] = coerce && request[p][parameter.name] && coerce(request[p][parameter.name]);
}
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/openapi3/defs/pets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ paths:
schema:
type: integer
format: int32
- name: filter
in: query
description: maximum number of results to return
schema:
type: object
properties:
breeds:
type: array
items:
type: string
style: deepObject
responses:
200:
description: pet response
Expand Down
9 changes: 8 additions & 1 deletion test/test-hapi-openapi3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const StubAuthTokenScheme = require('./fixtures/lib/stub-auth-token-scheme');

Test('test plugin', function (t) {
t.test('basic API', async function (t) {
t.plan(8);
t.plan(9);

const server = new Hapi.Server();

Expand Down Expand Up @@ -68,6 +68,13 @@ Test('test plugin', function (t) {
});

t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`);

response = await server.inject({
method: 'GET',
url: '/v1/petstore/pets?filter%5Bbreeds%5D=bully&filter%5Bbreeds%5D=chartreux'
});

t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`);
}
catch (error) {
t.fail(error.message);
Expand Down