Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 1, 2019
1 parent 3ef9a47 commit 7464364
Show file tree
Hide file tree
Showing 27 changed files with 69 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ValidateRequestOpts = {
};

export type ValidateResponseOpts = {
removeAdditional?: string | boolean;
removeAdditional?: 'failing' | boolean;
};

export interface OpenApiValidatorOpts {
Expand Down
6 changes: 3 additions & 3 deletions src/middlewares/ajv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export function createRequestAjv(

export function createResponseAjv(
openApiSpec: OpenAPIV3.Document,
options: any = {},
options: ajv.Options = {},
): Ajv.Ajv {
return createAjv(openApiSpec, options, false);
}

function createAjv(
openApiSpec: OpenAPIV3.Document,
options: any = {},
options: ajv.Options = {},
request = true,
): Ajv.Ajv {
const ajv = new Ajv({
Expand All @@ -36,7 +36,7 @@ function createAjv(
ajv.removeKeyword('propertyNames');
ajv.removeKeyword('contains');
ajv.removeKeyword('const');

if (request) {
ajv.removeKeyword('readOnly');
ajv.addKeyword('readOnly', {
Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OpenApiRequestHandler,
OpenApiRequestMetadata,
OpenAPIV3,
ValidationError,
} from '../framework/types';
const multer = require('multer');

Expand Down Expand Up @@ -67,7 +68,7 @@ function isMultipart(req: OpenApiRequest): boolean {
);
}

function error(req: OpenApiRequest, err: Error) {
function error(req: OpenApiRequest, err: Error): ValidationError {
if (err instanceof multer.MulterError) {
// TODO is special handling for MulterErrors needed
return validationError(500, req.path, err.message);
Expand Down
19 changes: 13 additions & 6 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { HandleFunction } from 'connect';
const TYPE_JSON = 'application/json';

export class RequestValidator {
private _middlewareCache;
private _middlewareCache: { string?: (string) => HandleFunction } = {};
private _apiDocs: OpenAPIV3.Document;
private ajv: Ajv;
private _requestOpts: ValidateRequestOpts = {};
Expand Down Expand Up @@ -84,7 +84,7 @@ export class RequestValidator {
): HandleFunction {
const parameters = this.parametersToSchema(path, pathSchema.parameters);

let usedSecuritySchema = [];
let usedSecuritySchema: OpenAPIV3.SecurityRequirementObject[] = [];
if (
pathSchema.hasOwnProperty('security') &&
pathSchema.security.length > 0
Expand Down Expand Up @@ -206,7 +206,11 @@ export class RequestValidator {
};
}

private rejectUnknownQueryParams(query, schema, whiteList = []) {
private rejectUnknownQueryParams(
query,
schema,
whiteList: string[] = [],
): void {
if (!schema.properties) return;
const knownQueryParams = new Set(Object.keys(schema.properties));
whiteList.forEach(item => knownQueryParams.add(item));
Expand All @@ -226,7 +230,7 @@ export class RequestValidator {
path: string,
contentType: ContentType,
requestBody: OpenAPIV3.RequestBodyObject,
) {
): object {
if (requestBody.content) {
let content = null;
for (const type of contentType.equivalents()) {
Expand Down Expand Up @@ -287,7 +291,10 @@ export class RequestValidator {
}
}

private getSecurityQueryParams(usedSecuritySchema, securitySchema) {
private getSecurityQueryParams(
usedSecuritySchema: OpenAPIV3.SecurityRequirementObject[],
securitySchema,
): string[] {
return usedSecuritySchema && securitySchema
? usedSecuritySchema
.filter(obj => Object.entries(obj).length !== 0)
Expand All @@ -300,7 +307,7 @@ export class RequestValidator {
: [];
}

private parametersToSchema(path, parameters = []) {
private parametersToSchema(path: string, parameters = []) {
const schema = { query: {}, headers: {}, params: {}, cookies: {} };
const reqFields = {
query: 'query',
Expand Down
9 changes: 5 additions & 4 deletions src/middlewares/openapi.response.validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ono from 'ono';
import * as Ajv from 'ajv';
import * as ajv from 'ajv';
import mung from './modded.express.mung';
import { createResponseAjv } from './ajv';
import {
Expand All @@ -13,11 +13,11 @@ import { OpenAPIV3 } from '../framework/types';
const TYPE_JSON = 'application/json';

export class ResponseValidator {
private ajv: Ajv.Ajv;
private ajv: ajv.Ajv;
private spec: OpenAPIV3.Document;
private validatorsCache = {};

constructor(openApiSpec: OpenAPIV3.Document, options: any = {}) {
constructor(openApiSpec: OpenAPIV3.Document, options: ajv.Options = {}) {
this.spec = openApiSpec;
this.ajv = createResponseAjv(openApiSpec, options);
(<any>mung).onError = (err, req, res, next) => {
Expand Down Expand Up @@ -45,7 +45,8 @@ export class ResponseValidator {
return this.buildValidators(responses);
}

const contentTypeKey = ContentType.from(req).equivalents()[0] || 'not_provided';
const contentTypeKey =
ContentType.from(req).equivalents()[0] || 'not_provided';
const key = `${req.method}-${req.originalUrl}-${contentTypeKey}`;

let validators = this.validatorsCache[key];
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/openapi.security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class AuthValidator {
this.scopes = scopes;
}

validate() {
public validate(): void {
this.validateApiKey();
this.validateHttp();
this.validateOauth2();
Expand Down Expand Up @@ -252,7 +252,7 @@ class AuthValidator {
}

class Util {
static isEmptyObject(o: Object) {
static isEmptyObject(o: {}) {
return (
typeof o === 'object' &&
Object.entries(o).length === 0 &&
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export class ContentType {
}
}
}
static from(req: Request): ContentType {
public static from(req: Request): ContentType {
return new ContentType(req.headers['content-type']);
}

equivalents(): string[] {
public equivalents(): string[] {
if (!this.withoutBoundary) return [];
if (this.charSet) {
return [this.mediaType, `${this.mediaType}; ${this.charSet}`];
Expand Down
3 changes: 1 addition & 2 deletions test/additional.props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
3 changes: 1 addition & 2 deletions test/coercion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
2 changes: 1 addition & 1 deletion test/common/app.common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as http from 'http';
import * as express from 'express';

export function startServer(app, port): Promise<http.Server> {
export function startServer(app, port: number): Promise<http.Server> {
return new Promise((resolve, reject) => {
const http = require('http');
const server = http.createServer(app);
Expand Down
8 changes: 4 additions & 4 deletions test/common/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { OpenApiValidatorOpts } from '../../src/framework/types';

export async function createApp(
opts?: OpenApiValidatorOpts,
port: number = 3000,
customRoutes: (app) => void = () => {},
useRoutes: boolean = true,
port = 3000,
customRoutes = app => {},
useRoutes = true,
) {
var app = express();
(<any>app).basePath = '/v1';
Expand All @@ -25,7 +25,7 @@ export async function createApp(
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

await new OpenApiValidator(opts).install(app)
await new OpenApiValidator(opts).install(app);

if (useRoutes) {
// register common routes
Expand Down
15 changes: 8 additions & 7 deletions test/common/myapp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const logger = require('morgan');
const http = require('http');
const OpenApiValidator = require('../../src').OpenApiValidator;
import * as express from 'express';
import * as path from 'path';
import * as cookieParser from 'cookie-parser';
import * as bodyParser from 'body-parser';
import * as logger from 'morgan';
import * as http from 'http';
import { OpenApiValidator } from '../../src';

const app = express();

app.use(bodyParser.urlencoded());
Expand Down
4 changes: 1 addition & 3 deletions test/nullable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
let basePath = null;

before(async () => {
// Set up the express app
Expand Down
4 changes: 1 addition & 3 deletions test/one.of.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
3 changes: 1 addition & 2 deletions test/openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let apps = [];
Expand Down
3 changes: 1 addition & 2 deletions test/path.level.parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
3 changes: 1 addition & 2 deletions test/query.params.allow.unknown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as path from 'path';
import * as express from 'express';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
3 changes: 1 addition & 2 deletions test/query.params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
3 changes: 1 addition & 2 deletions test/read.only.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
3 changes: 1 addition & 2 deletions test/request.bodies.ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
Expand Down
2 changes: 1 addition & 1 deletion test/response.validation.options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';

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

describe(packageJson.name, () => {
Expand Down
22 changes: 12 additions & 10 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';

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

Expand All @@ -31,8 +30,14 @@ describe(packageJson.name, () => {
} 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'}];
{
id: 2,
name: 'name',
tag: 'tag',
bought_at: today.toISOString(),
},
{ id: 3, name: 'name', tag: 'tag' },
];
}
return res.json(json);
});
Expand Down Expand Up @@ -113,12 +118,9 @@ describe(packageJson.name, () => {
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;
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 () =>
Expand Down
2 changes: 1 addition & 1 deletion test/response.validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as fs from 'fs';
import * as jsyaml from 'js-yaml';
import { expect } from 'chai';
import { ResponseValidator } from '../src/middlewares/openapi.response.validator';
import * as packageJson from '../package.json';

const packageJson = require('../package.json');
const apiSpecPath = path.join('test', 'resources', 'response.validation.yaml');
const apiSpec = jsyaml.safeLoad(fs.readFileSync(apiSpecPath, 'utf8'));

Expand Down
Loading

0 comments on commit 7464364

Please sign in to comment.