forked from jormaechea/open-api-mocker
-
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.
fix(Validation): Schema validator now accepts OpenAPI formats
Added ajv-openapi package to add support for the known formats that OpenAPI defines. Unknown formats will no longer cause errors to be thrown and will instead write warnings to the console. Fixes jormaechea#19
- Loading branch information
1 parent
3473313
commit 1128a05
Showing
3 changed files
with
70 additions
and
15 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 |
---|---|---|
@@ -1,15 +1,30 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const Ajv = require('ajv'); | ||
const Ajv = require("ajv"); | ||
const openApi = require("ajv-openapi"); | ||
|
||
const ajv = new Ajv({ unknownFormats: ['password'] }); | ||
// Configuration as defined in ajv-openapi example: https://www.npmjs.com/package/ajv-openapi#configuration-for-full-openapi-compatibility | ||
const ajvOptions = { | ||
schemaId: "auto", | ||
format: "full", | ||
coerceTypes: true, | ||
unknownFormats: "ignore", | ||
useDefaults: true | ||
}; | ||
|
||
class SchemaValidator { | ||
const openApiOptions = { | ||
useDraft04: true | ||
}; | ||
|
||
static validate(data, schema) { | ||
return !ajv.validate(schema, data) ? ajv.errors : []; | ||
} | ||
const ajv = openApi( | ||
new Ajv(ajvOptions), | ||
openApiOptions | ||
); | ||
|
||
class SchemaValidator { | ||
static validate(data, schema) { | ||
return !ajv.validate(schema, data) ? ajv.errors : []; | ||
} | ||
} | ||
|
||
module.exports = SchemaValidator; |
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