Skip to content

Commit

Permalink
set default behavior to validate spec and type coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 21, 2019
1 parent a2c9d60 commit 107513c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions .nyc_output/09163956-201a-4f50-8e8e-30cff1556081.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions .nyc_output/45a15900-548a-4456-8eb9-2cd37f17d6b5.json

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ see [app.js](example/app.js) for a complete example.
```javascript
new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
enableObjectCoercion: true, // should be default
}).install(app);
```

Expand All @@ -32,13 +31,21 @@ new OpenApiMiddleware({
```javascript
new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validateApiDoc: true, // default
enableObjectCoercion: true, // should be default
errorTransform: v => ({ // optional error transform
statusCode: v.status, // the http status to return
error: { // the custom error object
code: v.status,
message: v.errors[0].message,
// default is true
// validates the openapi spec, throws if invalid
validateApiDoc: true,
// default is true
// attempts to coerce a value's type to that defined in the openapi spec
enableObjectCoercion: true,
// default is undefined
// provide a custom error transform to customize how errors are shaped
errorTransform: validationResult => ({
// the http status code to return
statusCode: validationResult.status,
// the custom error object to return
error: {
code: validationResult.status,
message: validationResult.errors[0].message,
},
}),
}).install(app);
Expand Down
4 changes: 2 additions & 2 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ app.use(express.static(path.join(__dirname, 'public')));

new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validateApiDoc: true, // default
enableObjectCoercion: true, // will be default
validateApiDoc: true, // the default
enableObjectCoercion: true, // the default
}).install(app);

app.get('/v1/pets', function(req, res, next) {
Expand Down
7 changes: 5 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ export function OpenApiMiddleware(opts: OpenApiMiddlewareOpts) {
if (!apiContents)
throw new Error(`spec could not be read at ${opts.apiSpecPath}`);

opts.enableObjectCoercion = opts.enableObjectCoercion || true;
opts.name = opts.name || 'express-middleware-openapi';

const apiDoc = handleYaml(apiContents);
const framework = createFramework({ ...opts, apiDoc });
this.apiDoc = framework.apiDoc;

this.opts = opts;
this.opts.name = this.opts.name || 'express-middleware-openapi';
this.apiDoc = framework.apiDoc;
this.routes = buildRoutes(framework);
this.routeMap = this.routes.reduce((a, r) => {
const routeMethod = a[r.expressRoute];
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "express-middleware-openapi",
"version": "0.1.10-alpha",
"version": "0.1.11-alpha",
"description": "",
"main": "dist/index.js",
"scripts": {
"compile": "tsc",
"test": "nyc --reporter=text --reporter=lcov mocha -r source-map-support/register -r ts-node/register --recursive test/**/*.spec.ts",
"test": "npm run compile && nyc --reporter=text --reporter=lcov mocha -r source-map-support/register -r ts-node/register --recursive test/**/*.spec.ts",
"coveralls": "cat ./coverage/lcov.info | coveralls -v"
},
"keywords": [],
Expand Down
4 changes: 2 additions & 2 deletions test/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ app.use(express.static(path.join(__dirname, 'public')));

new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validateApiDoc: true, // is the default
enableObjectCoercion: true, // should be default
// validateApiDoc: true, // the default
// enableObjectCoercion: true, // the default
errorTransformer: (a, b) => {
console.log('---error trans---', a, b);

Expand Down

0 comments on commit 107513c

Please sign in to comment.