Skip to content

Commit

Permalink
Update validatePaths type (#119)
Browse files Browse the repository at this point in the history
* update validatePaths type

* remove space

* fix typos

* PR feedback
  • Loading branch information
eviemayy committed Feb 19, 2022
1 parent 17a18cd commit a2bf304
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-walls-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"koa-oas3": minor
---

update validatePaths type to accept string array or regex array
64 changes: 64 additions & 0 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,67 @@ describe('Koa Oas3 with ChowOptions', () => {
});

})

describe('Pass Regex Array as a validatePaths option', () => {
let mw: koa.Middleware;
const pathParam = 345;

beforeEach(async () => {
mw = await oas({
file: path.resolve('./__tests__/fixtures/pet-store.json'),
endpoint: '/openapi',
uiEndpoint: '/openapi.html',
validatePaths: [new RegExp(`pets(?!/${pathParam})`)],
validationOptions: { requestBodyAjvOptions: { allErrors: true } } as ChowOptions
});
})

test('should NOT validate paths specified that do not validate against the RegExp pattern', async () => {
const ctx = createContext({
url: `/pets/${pathParam}`,
headers: {
'accept': 'application/json'
},
method: 'GET'
});

mw = await oas({
file: path.resolve('./__tests__/fixtures/pet-store.json'),
endpoint: '/openapi',
uiEndpoint: '/openapi.html',
validatePaths: [new RegExp(`pets(?!/${pathParam})`)],
validationOptions: { requestBodyAjvOptions: { allErrors: true } } as ChowOptions
});

const next = jest.fn();
await mw(ctx, next);

expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith();
})

test('should validate paths specified in RegExp pattern', async () => {
const ctx = createContext({
url: `/pets/`,
headers: {
'accept': 'application/json'
},
method: 'GET'
});

mw = await oas({
file: path.resolve('./__tests__/fixtures/pet-store.json'),
endpoint: '/openapi',
uiEndpoint: '/openapi.html',
validatePaths: [new RegExp(`pets(?!/${pathParam})`)],
validationOptions: { requestBodyAjvOptions: { allErrors: true } } as ChowOptions
});

const next = jest.fn();
await mw(ctx, next);

expect(next).toHaveBeenCalledTimes(1);
expect(next).not.toHaveBeenCalledWith();
})

})
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Config {
* Whitelist paths for request validation
* default: ['/']
*/
validatePaths: string[];
validatePaths: Array<string> | Array<RegExp>;
/**
* Optional base path to swagger ui bundle
*/
Expand Down
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function oas(cfg: Partial<Config>): Promise<koa.Middleware> {
return;
}

if (!config.validatePaths.some(path => ctx.path.startsWith(path))) {
if (skipValidation(config.validatePaths, ctx)) {
// Skip validation if no path matches
return next();
}
Expand Down Expand Up @@ -151,3 +151,14 @@ async function compileOas(config: Config) {
doc: openApiObject,
};
}

function skipValidation(validatePaths: Array<string> | Array<RegExp>, ctx: koa.Context) {
let dontValidate = !validatePaths.some((path: string | RegExp) => {
if (path instanceof RegExp) {
return path.test(ctx.path);
} else {
return ctx.path.startsWith(path as string);
}
});
return dontValidate;
}

0 comments on commit a2bf304

Please sign in to comment.