Skip to content
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
3 changes: 3 additions & 0 deletions packages/openapi-response-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Ajv, {
Format,
ErrorObject,
ValidateFunction,
Options,
} from 'ajv';
import { IJsonSchema, OpenAPIV2, OpenAPIV3 } from 'openapi-types';

Expand Down Expand Up @@ -37,6 +38,7 @@ export interface OpenAPIResponseValidatorArgs {
openAPIResponseValidatorValidationError: OpenAPIResponseValidatorError,
ajvError: ErrorObject
): any;
ajvOptions?: Options;
}

export interface OpenAPIResponseValidatorError {
Expand Down Expand Up @@ -82,6 +84,7 @@ export default class OpenAPIResponseValidator
strict: false,
// @ts-ignore TODO get Ajv updated to account for logger
logger: false,
...(args.ajvOptions || {}),
});

this.errorMapper = errorTransformer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
constructorArgs: {
ajvOptions: {
discriminator: true
},
responses: {
200: {
schema: {
oneOf: [
{
type: 'object',
properties: {
foo: {
type: 'string',
},
my_type: {
type: 'string',
enum: ['foo']
}
},
required: ['foo', 'my_type'],
},
{

type: 'object',
properties: {
bar: {
type: 'string',
},
my_type: {
type: 'string',
enum: ['bar']
}
},
required: ['bar', 'my_type'],
}
],
discriminator: {
propertyName: 'my_type'
}
}
}
},
},
inputStatusCode: 200,
inputResponseBody: {
my_type: 'foo',
},
expectedValidationError: {
message: 'The response was not valid.',
errors:[
{
errorCode: 'required.openapi.responseValidation',
message: "must have required property 'foo'",
path: 'response'
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module.exports = {
constructorArgs: {
responses: {
200: {
schema: {
oneOf: [
{
type: 'object',
properties: {
foo: {
type: 'string',
},
my_type: {
type: 'string',
enum: ['foo']
}
},
required: ['foo', 'my_type'],
},
{

type: 'object',
properties: {
bar: {
type: 'string',
},
my_type: {
type: 'string',
enum: ['bar']
}
},
required: ['bar', 'my_type'],
}
],
discriminator: {
propertyName: 'my_type'
}
}
}
}
},
inputStatusCode: 200,
inputResponseBody: {
my_type: 'foo',
},
expectedValidationError: {
message: 'The response was not valid.',
errors:[
{
errorCode: 'required.openapi.responseValidation',
message: "must have required property 'foo'",
path: 'response'
},
{
errorCode: 'required.openapi.responseValidation',
message: "must have required property 'bar'",
path: 'response'
},
{
errorCode: 'enum.openapi.responseValidation',
message: 'must be equal to one of the allowed values',
path: 'my_type'
},
{
errorCode: 'oneOf.openapi.responseValidation',
message: 'must match exactly one schema in oneOf',
path: 'response'
}
]
}
}