Skip to content

Commit

Permalink
Fix/issue request require method (#31)
Browse files Browse the repository at this point in the history
* feat: fix is required request method when we don't want request object

* feat: add request object test scenario
  • Loading branch information
kevinccbsg authored Apr 11, 2021
1 parent 5475724 commit 066c963
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 14 deletions.
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,20 @@ const validate = (openApiDef, options = {}) => {
};

const isRequestRequired = (endpoint, method, contentType = 'application/json') => {
if (method === 'get') return false;
const argsValidationError = argsValidation('request', endpoint, method);
configError(argsValidationError, errorHandler);
const requestEndpoint = endpointValidation.request(openApiDef, endpoint, method, contentType);
configError(requestEndpoint, errorHandler);
return !!openApiDef.paths[endpoint][method].requestBody.required;
try {
if (method === 'get') return false;
const argsValidationError = argsValidation('request', endpoint, method);
configError(argsValidationError, errorHandler);
const requestEndpoint = endpointValidation.request(openApiDef, endpoint, method, contentType);
configError(requestEndpoint, errorHandler);
return !!openApiDef.paths[endpoint][method].requestBody.required;
} catch (error) {
// When we receive this is because it was not documented
// Document request body it is not required there might be endpoints
// where you don't want request body
if (error.message.includes('does not have requestBody definition')) return false;
throw error;
}
};

const validateRequest = (value, endpoint, method, contentType = 'application/json') => {
Expand Down
6 changes: 6 additions & 0 deletions test/isRequestRequired/fake-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ app.post('/api/v1/albums', (req, res) => res.send('Hello World!'));
* @return {object} 200 - song response
*/
app.post('/api/v1/name', (req, res) => res.send('Hello World!'));

/**
* PATCH /api/v1/name
* @return {object} 200 - song response
*/
app.patch('/api/v1/name', (req, res) => res.send('Hello World!'));
15 changes: 7 additions & 8 deletions test/isRequestRequired/isRequestRequired.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ describe('isRequestRequired method', () => {
}).toThrow('Endpoint: "/api/v1/non-valid-endpoint" not found in the OpenAPI definition');
});

it('should throw errors when endpoint\'s method does not exist', () => {
expect(() => {
isRequestRequired('/api/v1/name', 'patch');
}).toThrow('Method: "patch" not found in the OpenAPI definition for "/api/v1/name" endpoint');
});

it('should throw errors when endpoint\'s content-type does not exist', () => {
expect(() => {
isRequestRequired('/api/v1/name', 'post', 'html');
Expand All @@ -50,6 +44,11 @@ describe('isRequestRequired method', () => {
expect(result).toBeFalsy();
});

it('should return false when request is not documented', () => {
const result = isRequestRequired('/api/v1/name', 'patch');
expect(result).toBeFalsy();
});

it('should always return false when we validate a get method', () => {
const result = isRequestRequired('/api/v1/albums', 'get');
expect(result).toBeFalsy();
Expand All @@ -76,10 +75,10 @@ describe('isRequestRequired method with custom Handler', () => {

it('should throw errors when reference type is not valid', () => {
try {
isRequestRequired('/api/v1/name', 'patch');
isRequestRequired('/api/v1/name', 'post', 'html');
} catch (err) {
expect(customErrorCallback).toHaveBeenCalledWith(
'Method: "patch" not found in the OpenAPI definition for "/api/v1/name" endpoint',
'Method: "post" and Endpoint: "/api/v1/name" does not have requestBody with this ContentType: "html"',
undefined,
);
}
Expand Down
19 changes: 19 additions & 0 deletions test/isRequestRequired/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@
"description": "name body description",
"required": true
}
},
"patch": {
"deprecated": false,
"summary": "",
"security": [],
"responses": {
"200": {
"description": "song response",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
},
"parameters": [],
"tags": []
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions test/validateRequest/fake-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ app.post('/api/v1/albums', (req, res) => res.send('Hello World!'));
* @return {object} 200 - song response
*/
app.post('/api/v1/name', (req, res) => res.send('Hello World!'));

/**
* PATCH /api/v1/name
* @return {object} 200 - song response
*/
app.patch('/api/v1/name', (req, res) => res.send('Hello World!'));
19 changes: 19 additions & 0 deletions test/validateRequest/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@
"description": "name body description",
"required": true
}
},
"patch": {
"deprecated": false,
"summary": "",
"security": [],
"responses": {
"200": {
"description": "song response",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
},
"parameters": [],
"tags": []
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions test/validateRequest/validateRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ describe('ValidateRequest method', () => {
}).toThrow('Method: "post" and Endpoint: "/api/v1/name" does not have requestBody with this ContentType: "html"');
});

it('should throw errors when endpoint\'s don\'t have requestBody definition', () => {
expect(() => {
validateRequest({}, '/api/v1/name', 'patch');
}).toThrow('Method: "patch" and Endpoint: "/api/v1/name" does not have requestBody definition');
});

it('should validate basic string type', () => {
const result = validateRequest('valid string', '/api/v1/name', 'post');
expect(result).toBeTruthy();
Expand Down

0 comments on commit 066c963

Please sign in to comment.