This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: configurable api documentation path * chore: add top level jest config * chore: ignore coverage * fix: disable validation logging * feat: allow configuring base path (closes #107) * feat: validate responses (closes #59) * docs: update documentation with limitations * docs: add documentation on document hosting
- Loading branch information
Showing
21 changed files
with
249 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ generated/ | |
yarn-error.log | ||
.DS_STORE | ||
dist/ | ||
*.tsbuildinfo | ||
*.tsbuildinfo | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"cSpell.words": ["APIV", "Taeschner", "codegen", "commitlint", "lerna", "openapi"] | ||
"cSpell.words": ["APIV", "Taeschner", "codegen", "commitlint", "lerna", "openapi"], | ||
"daddy-jest.jestPath": "${workspaceFolder}/node_modules/.bin/jest" | ||
} |
3 changes: 2 additions & 1 deletion
3
codegen/src/generators/ResourcesGenerator/OpenApiConstantFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
- to: / | ||
name: Home | ||
- to: /faq | ||
name: FAQ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testRegex: '__tests__.*.spec.[jt]sx?$', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Ajv from 'ajv' | ||
import { OpenAPIV3 } from 'openapi-types' | ||
import { ResponseValidationError } from './errors' | ||
import { isReferenceObject } from './helpers/isReferenceObject' | ||
import { SlushyProps } from './SlushyProps' | ||
|
||
export class ResponseValidator { | ||
private readonly validator: Ajv.Ajv | ||
|
||
public constructor(_props: SlushyProps<any>) { | ||
this.validator = new Ajv({ | ||
allErrors: true, | ||
useDefaults: true, | ||
unknownFormats: 'ignore', | ||
logger: false, | ||
}) | ||
} | ||
|
||
public validateResponse( | ||
status: number, | ||
payload: any, | ||
contentType: string, | ||
operationObject: OpenAPIV3.OperationObject, | ||
) { | ||
// FIXME: Validate more content types | ||
if (contentType !== 'application/json') { | ||
return | ||
} | ||
|
||
if (!operationObject.responses) { | ||
// No responses expected | ||
return | ||
} | ||
|
||
// FIXME: Handle status code range | ||
const response = operationObject.responses[status.toString()] || operationObject.responses.default | ||
if (!response) { | ||
// Status code not expected | ||
return | ||
} | ||
|
||
if (isReferenceObject(response)) { | ||
// Should be resolved | ||
return | ||
} | ||
|
||
if (!response.content) { | ||
if (payload != null) { | ||
throw new ResponseValidationError('Invalid response content, no content expected') | ||
} | ||
// No content expected | ||
return | ||
} | ||
|
||
if (!response.content[contentType]) { | ||
throw new ResponseValidationError('Invalid content type') | ||
} | ||
|
||
const schema = response.content[contentType].schema | ||
if (!schema) { | ||
return | ||
} | ||
|
||
const isValid = this.validator.validate(schema, payload) | ||
if (!isValid && this.validator.errors) { | ||
throw new ResponseValidationError('Invalid response content, validation failed', this.validator.errors) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.