-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove dependency on openapi-schema-validator
- Loading branch information
Carmine DiMascio
committed
Jul 20, 2019
1 parent
667a3a2
commit 7f41282
Showing
7 changed files
with
1,534 additions
and
186 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as Ajv from 'ajv'; | ||
import * as merge from 'lodash.merge' | ||
const draftSchema = require('ajv/lib/refs/json-schema-draft-04.json'); | ||
// https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json | ||
const openapi3Schema = require('./openapi.v3.schema.json'); | ||
|
||
export default class OpenAPISchemaValidator{ | ||
private validator: Ajv.ValidateFunction; | ||
constructor({ | ||
version, | ||
extensions, | ||
}) { | ||
const v = new Ajv({ schemaId: 'auto', allErrors: true }); | ||
v.addMetaSchema(draftSchema); | ||
const ver = (version && parseInt(String(version), 10)); | ||
if (!ver) throw Error('version missing from OpenAPI specification.'); | ||
if (ver != 3) throw Error('OpenAPI v3 specification version is required.') | ||
const schema = merge( | ||
{}, | ||
openapi3Schema, | ||
extensions || {} | ||
); | ||
v.addSchema(schema); | ||
this.validator = v.compile(schema); | ||
} | ||
|
||
public validate(openapiDoc) { | ||
const valid = this.validator(openapiDoc); | ||
if (!valid) { | ||
return { errors: this.validator.errors }; | ||
} else { | ||
return { errors: [] }; | ||
} | ||
} | ||
} |
Oops, something went wrong.