-
-
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.
- Loading branch information
Carmine DiMascio
committed
Jul 20, 2019
1 parent
fd014dd
commit 9fff6ce
Showing
2 changed files
with
25 additions
and
30 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
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,35 +1,30 @@ | ||
import * as Ajv from 'ajv'; | ||
import * as merge from 'lodash.merge' | ||
import * as merge from 'lodash.merge'; | ||
import * as draftSchema from 'ajv/lib/refs/json-schema-draft-04.json'; | ||
// https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json | ||
import * as openapi3Schema from './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: [] }; | ||
} | ||
export 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: [] }; | ||
} | ||
} | ||
} | ||
} |