Skip to content

Commit

Permalink
add option to disable type coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Sep 8, 2019
1 parent 17dfc20 commit 9f786d9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Expand Up @@ -8,11 +8,13 @@ import { OpenAPIV3, OpenApiRequest } from './framework/types';
export interface OpenApiValidatorOpts {
apiSpecPath?: string;
apiSpec?: OpenAPIV3.Document | string;
coerceTypes?: boolean;
multerOpts?: {};
}

export class OpenApiValidator {
private context: OpenApiContext;
private coerceTypes: boolean;
private multerOpts: {};

constructor(options: OpenApiValidatorOpts) {
Expand All @@ -21,6 +23,7 @@ export class OpenApiValidator {
if (options.apiSpecPath && options.apiSpec)
throw ono('apiSpecPath or apiSpec required. not both.');

this.coerceTypes = options.coerceTypes == null ? true : options.coerceTypes;
this.multerOpts = options.multerOpts;

const openApiContext = new OpenApiContext({
Expand Down Expand Up @@ -51,7 +54,7 @@ export class OpenApiValidator {

const aoav = new middlewares.RequestValidator(this.context.apiDoc, {
nullable: true,
coerceTypes: true,
coerceTypes: this.coerceTypes,
removeAdditional: false,
useDefaults: true,
});
Expand Down
63 changes: 49 additions & 14 deletions test/nullable.spec.ts
Expand Up @@ -13,14 +13,12 @@ describe(packageJson.name, () => {
before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'nullable.yaml');
app = await createApp({ apiSpec }, 3005);
app = await createApp({ apiSpec, coerceTypes: false }, 3005);
basePath = app.basePath;

app.use(
`${basePath}`,
express
.Router()
.post(`/pets/nullable`, (req, res) => res.json(req.body)),
express.Router().post(`/pets/nullable`, (req, res) => res.json(req.body)),
);
});

Expand All @@ -39,14 +37,51 @@ describe(packageJson.name, () => {
expect(r.body.name).to.be.null;
}));

it('should fill null with default (name: nullable false/default)', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: null,
})
.expect(200)
.then(r => {
expect(r.body.name).to.equal('');
}));
it('should not fill an explicity null with default when coerceTypes is false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: null,
})
.expect(400));

it('should fill unspecified field with default when coerceTypes is false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: 'name',
})
.expect(200)
.then(r => {
expect(r.body.tag).to.equal('my default value');
}));

it('should fail if required and not provided (nullable true)', async () =>
request(app)
.post(`${basePath}/pets/nullable`)
.send({})
.expect(400)
.then(r => {
expect(r.body.errors[0].path).to.equal('.body.name');
}));

it('should fail if required and not provided (nullable false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({})
.expect(400)
.then(r => {
expect(r.body.errors[0].path).to.equal('.body.name');
}));

it('should fail if required and provided as null when nullable is false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: null,
})
.expect(400)
.then(r => {
expect(r.body.errors[0].path).to.equal('.body.name');
}));
});
1 change: 1 addition & 0 deletions test/resources/nullable.yaml
Expand Up @@ -100,6 +100,7 @@ components:
type: string
tag:
type: string
default: my default value


Pet:
Expand Down

0 comments on commit 9f786d9

Please sign in to comment.