Skip to content

Commit

Permalink
add null value support to response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerbn committed Nov 6, 2019
1 parent 86eb3c4 commit c62a148
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class OpenApiValidator {
const responseValidator = new middlewares.ResponseValidator(
this.context.apiDoc,
{
nullable: true,
coerceTypes,
removeAdditional,
unknownFormats,
Expand Down
4 changes: 4 additions & 0 deletions test/resources/response.validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ components:
required:
- name
properties:
bought_at:
type: string
format: date-time
nullable: true
name:
type: string
nullable: true
Expand Down
24 changes: 23 additions & 1 deletion test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createApp } from './common/app';

const packageJson = require('../package.json');
const apiSpecPath = path.join('test', 'resources', 'response.validation.yaml');
const today = new Date();

describe(packageJson.name, () => {
let app = null;
Expand All @@ -25,8 +26,13 @@ describe(packageJson.name, () => {
});
app.get(`${app.basePath}/pets`, (req, res) => {
let json = {};
if ((req.query.mode = 'bad_type')) {
if ((req.query.mode == 'bad_type')) {
json = [{ id: 'bad_id', name: 'name', tag: 'tag' }];
} else if (req.query.mode == 'check_null') {
json = [
{ id: 1, name: 'name', tag: 'tag', bought_at: null },
{ id: 2, name: 'name', tag: 'tag', bought_at: today.toISOString() },
{ id: 3, name: 'name', tag: 'tag'}];
}
return res.json(json);
});
Expand Down Expand Up @@ -99,6 +105,22 @@ describe(packageJson.name, () => {
expect(e.code).to.equal(500);
}));

it('should pass if value is null', async () =>
request(app)
.get(`${app.basePath}/pets?mode=check_null`)
.expect(200)
.then((r: any) => {
expect(r.body)
.is.an('array')
.with.length(3);
expect(r.body[0].bought_at)
.equal(null);
expect(r.body[1].bought_at)
.equal(today.toISOString());
expect(r.body[2].bought_at)
.to.be.undefined;
}));

it('should pass if response is a list', async () =>
request(app)
.get(`${app.basePath}/users`)
Expand Down

0 comments on commit c62a148

Please sign in to comment.