Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/one of support #49

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- uses: actions/setup-node@v2
with:
registry-url: https://registry.npmjs.org/
node-version: '16'
- run: npm ci
- run: npm test

Expand All @@ -23,10 +24,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- uses: actions/setup-node@v2
with:
registry-url: https://registry.npmjs.org/
node-version: '16'
- run: npm ci
- run: npm publish
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/runTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [14.x, 16.x, 18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: '12'
node-version: '16'
- run: npm ci
- uses: paambaati/[email protected]
env:
Expand Down
12 changes: 11 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
module.exports = {
// Options to override default ajv settings (https://ajv.js.org/options.html)
ajvConfig: {},
ajvConfig: {
// support OAS discriminator https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
discriminator: true,
// allow example props from OAS
keywords: ['example'],
},

// Custom error handler
errorHandler: null,

// If enabled, validation will throw errors if the payload has additional
// properties not defined in the schema
strictValidation: true,

// currently, AJV doesn't support discriminator mappings as used in OAS https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
// if one of the next versions of AJV supports it, this option can be removed
// Issue on AJV: https://github.com/ajv-validator/ajv/issues/2003
discriminatorMappingSupported: false,
};
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ const {
* @param {object} options Options to extend the errorHandler or Ajv configuration
* @returns {ValidatorMethods} validator methods
*/
const validate = (openApiDef, userOptions = {}) => {
const validate = (openApiDef, userOptions = { ajvConfig: {} }) => {
const options = {
...defaultOptions,
...(userOptions || {}),
ajvConfig: {
...defaultOptions.ajvConfig,
...(userOptions?.ajvConfig || {}),
},
};

const { errorHandler } = options;
Expand All @@ -89,7 +93,13 @@ const validate = (openApiDef, userOptions = {}) => {
const optionsValidationError = optionsValidation(userOptions);
configError(optionsValidationError, errorHandler);

const schemaOptions = { strictValidation: options.strictValidation };
const schemaOptions = {
strictValidation: options.strictValidation,
discriminatorMappingSupported: options.discriminatorMappingSupported,
};
const discriminatorOptions = {
discriminatorMappingSupported: options.discriminatorMappingSupported,
};
const defsSchema = {
$id: 'defs.json',
definitions: {
Expand Down Expand Up @@ -155,7 +165,7 @@ const validate = (openApiDef, userOptions = {}) => {
let responseSchema = {
...openApiDef.paths[endpoint][method].responses[status].content[contentType].schema,
};
responseSchema = formatComponents(responseSchema);
responseSchema = formatComponents(responseSchema, discriminatorOptions);
const schemaName = `${method}-${endpoint}-${status}-${contentType}-response`;
return schemaValidation(value, responseSchema, 'response', schemaName);
};
Expand Down Expand Up @@ -185,7 +195,7 @@ const validate = (openApiDef, userOptions = {}) => {
let requestBodySchema = {
...openApiDef.paths[endpoint][method].requestBody.content[contentType].schema,
};
requestBodySchema = formatComponents(requestBodySchema);
requestBodySchema = formatComponents(requestBodySchema, discriminatorOptions);
const schemaName = `${method}-${endpoint}-${contentType}-request`;
return schemaValidation(value, requestBodySchema, 'request', schemaName);
};
Expand All @@ -196,7 +206,7 @@ const validate = (openApiDef, userOptions = {}) => {
const paramEndpoint = endpointValidation.params(openApiDef, endpoint, method, key, type);
configError(paramEndpoint, errorHandler);
let parametersSchema = paramEndpoint.parameter.schema;
parametersSchema = formatComponents(parametersSchema);
parametersSchema = formatComponents(parametersSchema, discriminatorOptions);
const sanitizeValue = sanitizeValueSchema(value, parametersSchema);
const schemaName = `${method}-${endpoint}-${key}-${type}-param`;
return schemaValidation(sanitizeValue, parametersSchema, type, schemaName);
Expand Down
Loading