From bf5ce43f7755c3d18dc6a15444748514772ffec4 Mon Sep 17 00:00:00 2001 From: Ivan Malkov Date: Wed, 17 Feb 2021 15:50:39 +0300 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Added=20feature=20to?= =?UTF-8?q?=20ignore=20routes=20for=20specific=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frameworks/fastify.js | 14 ++++++++++---- types/index.d.ts | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/frameworks/fastify.js b/src/frameworks/fastify.js index 83b4b5a..e26e177 100644 --- a/src/frameworks/fastify.js +++ b/src/frameworks/fastify.js @@ -9,22 +9,28 @@ function getValidator(validateRequest) { } catch (err) { throw new Error('Missing `uri-js` dependency. Please run "npm install uri-js" to use fastify plugin'); } - let skiplist = []; + let skiplist = { + get: [], + post: [], + put: [] + }; return (pluginOptions) => { if (pluginOptions && pluginOptions.skiplist) { - skiplist = pluginOptions.skiplist.map((regexStr) => new RegExp(regexStr)); + skiplist.get = pluginOptions.skiplist.get.map((regexStr) => new RegExp(regexStr)); + skiplist.post = pluginOptions.skiplist.post.map((regexStr) => new RegExp(regexStr)); + skiplist.put = pluginOptions.skiplist.put.map((regexStr) => new RegExp(regexStr)); } return fp(function (fastify, options, next) { - fastify.addHook('preValidation', validate); + fastify.addHook('preHandler', validate); next(); }); }; function validate(request, reply) { const requestOptions = _getParameters(request); - if (skiplist.some((skipListRegex) => { + if (skiplist[requestOptions.method.toLowerCase()].some((skipListRegex) => { return skipListRegex.test(requestOptions.path); })) { return Promise.resolve(); diff --git a/types/index.d.ts b/types/index.d.ts index c235bdb..d755a68 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -53,7 +53,11 @@ export interface format { } export interface FastifyPluginOptions { - skiplist?: Array; + skiplist?: { + get: Array, + post: Array + put: Array + }; } export interface ajvValidatorOptions { From ab262df33c98b38e5a1a8751a645057ac9972794 Mon Sep 17 00:00:00 2001 From: Ivan Malkov Date: Wed, 17 Feb 2021 16:03:34 +0300 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=F0=9F=90=9B=20added=20DELETE=20rout?= =?UTF-8?q?e=20to=20skiplist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frameworks/fastify.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/frameworks/fastify.js b/src/frameworks/fastify.js index e26e177..981cae9 100644 --- a/src/frameworks/fastify.js +++ b/src/frameworks/fastify.js @@ -12,7 +12,8 @@ function getValidator(validateRequest) { let skiplist = { get: [], post: [], - put: [] + put: [], + delete: [] }; return (pluginOptions) => { @@ -20,6 +21,7 @@ function getValidator(validateRequest) { skiplist.get = pluginOptions.skiplist.get.map((regexStr) => new RegExp(regexStr)); skiplist.post = pluginOptions.skiplist.post.map((regexStr) => new RegExp(regexStr)); skiplist.put = pluginOptions.skiplist.put.map((regexStr) => new RegExp(regexStr)); + skiplist.delete = pluginOptions.skiplist.delete.map((regexStr) => new RegExp(regexStr)); } return fp(function (fastify, options, next) { From 5313485fa42ef75cb34f7f44f3995d75410a3454 Mon Sep 17 00:00:00 2001 From: Ivan Malkov Date: Wed, 17 Feb 2021 17:29:35 +0300 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Added=20patch=20metho?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frameworks/fastify.js | 12 ++++++------ types/index.d.ts | 8 +++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/frameworks/fastify.js b/src/frameworks/fastify.js index 981cae9..33209a5 100644 --- a/src/frameworks/fastify.js +++ b/src/frameworks/fastify.js @@ -9,19 +9,19 @@ function getValidator(validateRequest) { } catch (err) { throw new Error('Missing `uri-js` dependency. Please run "npm install uri-js" to use fastify plugin'); } - let skiplist = { + const skiplist = { get: [], post: [], put: [], - delete: [] + delete: [], + patch: [] }; return (pluginOptions) => { if (pluginOptions && pluginOptions.skiplist) { - skiplist.get = pluginOptions.skiplist.get.map((regexStr) => new RegExp(regexStr)); - skiplist.post = pluginOptions.skiplist.post.map((regexStr) => new RegExp(regexStr)); - skiplist.put = pluginOptions.skiplist.put.map((regexStr) => new RegExp(regexStr)); - skiplist.delete = pluginOptions.skiplist.delete.map((regexStr) => new RegExp(regexStr)); + for (let key in pluginOptions.skiplist) { + skiplist[key] = pluginOptions.skiplist[key].map((regexStr) => new RegExp(regexStr)); + } } return fp(function (fastify, options, next) { diff --git a/types/index.d.ts b/types/index.d.ts index d755a68..d5925d0 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -54,9 +54,11 @@ export interface format { export interface FastifyPluginOptions { skiplist?: { - get: Array, - post: Array - put: Array + get?: Array, + post?: Array + put?: Array, + delete?: Array, + patch?: Array, }; } From f934a39de07d458a55b2a1c51008d74242a35c97 Mon Sep 17 00:00:00 2001 From: Ivan Malkov Date: Wed, 17 Feb 2021 18:37:45 +0300 Subject: [PATCH 4/6] =?UTF-8?q?test:=20=F0=9F=92=8D=20Added=20tests=20for?= =?UTF-8?q?=20new=20skiplist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frameworks/fastify.js | 2 +- test/fastify/fastify-test-skiplist.js | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/frameworks/fastify.js b/src/frameworks/fastify.js index 33209a5..06d3e1c 100644 --- a/src/frameworks/fastify.js +++ b/src/frameworks/fastify.js @@ -25,7 +25,7 @@ function getValidator(validateRequest) { } return fp(function (fastify, options, next) { - fastify.addHook('preHandler', validate); + fastify.addHook('preValidation', validate); next(); }); }; diff --git a/test/fastify/fastify-test-skiplist.js b/test/fastify/fastify-test-skiplist.js index 79cc67c..e07bdcc 100644 --- a/test/fastify/fastify-test-skiplist.js +++ b/test/fastify/fastify-test-skiplist.js @@ -17,7 +17,9 @@ describe('fastify plugin skiplist', () => { app = fastify({ logger: true }); app.register(inputValidation.validate({ - skiplist: ['^/pets$'] + skiplist: { + get: ['^/pets$'] + } })); app.setErrorHandler(async (err, req, reply) => { if (err instanceof inputValidation.InputValidationError) { @@ -31,6 +33,9 @@ describe('fastify plugin skiplist', () => { app.get('/pets', (req, reply) => { reply.status(204).send(); }); + app.post('/pets', (req, reply) => { + reply.status(201).send(); + }); await app.ready(); }); @@ -52,6 +57,21 @@ describe('fastify plugin skiplist', () => { expect(response.body).to.eql(''); }); + it('POST fails if only GET validation is ignored', async () => { + const response = await app.inject() + .headers({ + 'api-version': '1.0' + }) + .body({ + age: true + }) + .post('/pets'); + expect(response.statusCode).to.equal(400); + expect(response.json()).to.eql({ + more_info: "[{\"keyword\":\"required\",\"dataPath\":\"\",\"schemaPath\":\"#/required\",\"params\":{\"missingProperty\":\"name\"},\"message\":\"should have required property 'name'\"},{\"keyword\":\"type\",\"dataPath\":\".age\",\"schemaPath\":\"#/properties/age/type\",\"params\":{\"type\":\"integer\"},\"message\":\"should be integer\"},{\"keyword\":\"required\",\"dataPath\":\"\",\"schemaPath\":\"#/required\",\"params\":{\"missingProperty\":\"test\"},\"message\":\"should have required property 'test'\"}]" + }); + }); + it('Skips endpoint validation for request with multiple errors', async () => { const response = await app.inject().get('/pets'); expect(response.statusCode).to.equal(204); From f1b9188a244d1b9501679e9f5ab61cf9d24ba1bb Mon Sep 17 00:00:00 2001 From: Ivan Malkov Date: Mon, 22 Feb 2021 17:09:42 +0300 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20linting=20fix=20?= =?UTF-8?q?-=20replaced=20let=20with=20const?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frameworks/fastify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frameworks/fastify.js b/src/frameworks/fastify.js index 06d3e1c..5130948 100644 --- a/src/frameworks/fastify.js +++ b/src/frameworks/fastify.js @@ -19,7 +19,7 @@ function getValidator(validateRequest) { return (pluginOptions) => { if (pluginOptions && pluginOptions.skiplist) { - for (let key in pluginOptions.skiplist) { + for (const key in pluginOptions.skiplist) { skiplist[key] = pluginOptions.skiplist[key].map((regexStr) => new RegExp(regexStr)); } } From 8e6d4a826dcb7bd2ced475b393cd4d1796df71d6 Mon Sep 17 00:00:00 2001 From: Ivan Malkov Date: Tue, 23 Feb 2021 08:02:02 +0300 Subject: [PATCH 6/6] =?UTF-8?q?docs:=20=E2=9C=8F=EF=B8=8F=20updated=20docu?= =?UTF-8?q?mentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e403076..cadccb4 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,14 @@ This middleware function validates the request body, headers, path parameters an #### fastifyOptions -- `skiplist`: Endpoint paths for which validation should not be applied. An array of strings in RegExp format, e. g. `['^/pets$']` - +- `skiplist`: Endpoint paths with methods for which validation should not be applied. An object with array of strings in RegExp format for every method you want to specify, e. g.: + ```js + skipList: { + get: ['^/pets$', '^/ui$'], + post: ['^/cat$'], + delete: ['^/cat$'] + } + ``` ### openapi-validator-middleware.init(pathToSwaggerFile, options) Initialize the middleware using a swagger definition.