From a87c8cfc244e8ca71c859087b4cf51adbb84e41e Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 11:24:28 +0530 Subject: [PATCH 01/24] validator compiler added --- index.js | 21 ++++++++++++++++++--- lib/validatorCompiler.js | 34 ++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 lib/validatorCompiler.js diff --git a/index.js b/index.js index 04348477..48fa04d9 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,16 @@ 'use strict' const fp = require('fastify-plugin') +const validatorCompiler = require('./lib/validatorCompiler') -function fastifySwagger (fastify, opts, next) { +function fastifySwagger(fastify, opts, next) { + + // enabling custom or validator complier form opts object + const customCompiler = opts.customCompiler || null; + if(customCompiler && customCompiler !== null && typeof customCompiler !== typeof undefined){ + fastify.setValidatorCompiler(validatorCompiler) + } + // by default the mode is dynamic, as plugin initially was developed opts.mode = opts.mode || 'dynamic' @@ -25,7 +33,14 @@ function fastifySwagger (fastify, opts, next) { fastify.decorate('swaggerCSP', require('./static/csp.json')) } -module.exports = fp(fastifySwagger, { +const plugin = fp(fastifySwagger, { fastify: '>=3.x', - name: 'fastify-swagger' + name: 'fastify-swagger', }) + +module.exports.default = plugin; + +module.exports = { + fastifySwagger: plugin, + validatorCompiler +} \ No newline at end of file diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js new file mode 100644 index 00000000..fa5b309a --- /dev/null +++ b/lib/validatorCompiler.js @@ -0,0 +1,34 @@ +const Ajv = require('ajv'); + +const validatorCompiler = () => { + const ajv = new Ajv({ + removeAdditional: true, // remove additional properties + useDefaults: true, // replace missing properties and items with the values from corresponding default keyword + coerceTypes: true, // change data type of data to match type keyword + nullable: true // support keyword "nullable" from Open API 3 specification. + }) + + ajv.addFormat('binary', { + type: 'string', + validate: () => true + }) + ajv.addFormat('byte', { + type: 'string', + validate: () => true + }) + ajv.addFormat('int32', { + type: 'number', + validate: () => true + }) + ajv.addFormat('int64', { + type: 'number', + validate: () => true + }) + + return function (schema) { + return ajv.compile(schema) + } + +} + +module.exports = validatorCompiler; \ No newline at end of file diff --git a/package.json b/package.json index 14c47b46..f7a0946c 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "tsd": "^0.17.0" }, "dependencies": { + "ajv": "^6.12.6", "fastify-plugin": "^3.0.0", "fastify-static": "^4.0.0", "js-yaml": "^4.0.0", From af31ea5c220d38e7c8021de3bb94b2986a0830af Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 12:56:00 +0530 Subject: [PATCH 02/24] docs added --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/README.md b/README.md index 3af31062..0a20a622 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,51 @@ fastify.ready(err => { fastify.swagger() }) ``` + + +## OpenAPI spec (specification) + +There are three way to enable open api specification + +1) ### Project Level + +```js +const fastify = require('fastify')() + +fastify.register(require('fastify-swagger'), { + customCompiler: true +}) +``` + +2) ### Instance Level + +```js +const{ fastifySwagger, validatorCompiler } = require("fastify-swagger"); + +fastify.setValidatorCompiler(validatorCompiler) +``` + +2) ### Route Level + +```js +const{ fastifySwagger, validatorCompiler } = require("fastify-swagger"); + +fastify.get( + '/', + { + schema: { + body: { + type: 'object', + properties: { file: { type: 'string', format: 'binary' } } + } + }, + validatorCompiler + }, + ( ) => {} +) +``` + + ## API @@ -207,6 +252,7 @@ An example of using `fastify-swagger` with `static` mode enabled can be found [h | Option | Default | Description | | ------------------ | ---------------- | ------------------------------------------------------------------------------------------------------------------------- | | exposeRoute | false | Exposes documentation route. | + | customCompiler | false | if it's `true` means fastify will support swagger open api specs (binary, byte, int32, int64) as a schema | | hiddenTag | X-HIDDEN | Tag to control hiding of routes. | | hideUntagged | false | If `true` remove routes without tags from resulting Swagger/OpenAPI schema file. | | initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). | @@ -408,6 +454,8 @@ Please specify `type: 'null'` for the response otherwise Fastify itself will fai } ``` + + #### OpenAPI Parameter Options @@ -637,6 +685,37 @@ You can integration this plugin with ```fastify-helmet``` with some little work. }) ``` + + +### upload File Schema + +1) Enable open api specification (`docs available in upper`) + +2) FileUpload Schema + +```javascript + +async function routes(fastify, opts, next){ + fastify.post("/upload", { + schema: { + type: "object", + body: { + type: "object", + properties: { + file: { + type: "file", + format: "binary" + } + } + } + + }}, ()=>{}) +} + +``` + + + ### Development In order to start development run: From 213edabed19b5ad653b42bc8f12ea6e484a1919b Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 13:13:33 +0530 Subject: [PATCH 03/24] code standard fix --- index.js | 15 +++++------ lib/validatorCompiler.js | 55 ++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 48fa04d9..aa048e41 100644 --- a/index.js +++ b/index.js @@ -3,14 +3,13 @@ const fp = require('fastify-plugin') const validatorCompiler = require('./lib/validatorCompiler') -function fastifySwagger(fastify, opts, next) { - +function fastifySwagger (fastify, opts, next) { // enabling custom or validator complier form opts object - const customCompiler = opts.customCompiler || null; - if(customCompiler && customCompiler !== null && typeof customCompiler !== typeof undefined){ + const customCompiler = opts.customCompiler || null + if (customCompiler && customCompiler !== null && typeof customCompiler !== typeof undefined) { fastify.setValidatorCompiler(validatorCompiler) } - + // by default the mode is dynamic, as plugin initially was developed opts.mode = opts.mode || 'dynamic' @@ -35,12 +34,12 @@ function fastifySwagger(fastify, opts, next) { const plugin = fp(fastifySwagger, { fastify: '>=3.x', - name: 'fastify-swagger', + name: 'fastify-swagger' }) -module.exports.default = plugin; +module.exports.default = plugin module.exports = { fastifySwagger: plugin, validatorCompiler -} \ No newline at end of file +} diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index fa5b309a..d0ccd0c0 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -1,34 +1,33 @@ -const Ajv = require('ajv'); +const Ajv = require('ajv') const validatorCompiler = () => { - const ajv = new Ajv({ - removeAdditional: true, // remove additional properties - useDefaults: true, // replace missing properties and items with the values from corresponding default keyword - coerceTypes: true, // change data type of data to match type keyword - nullable: true // support keyword "nullable" from Open API 3 specification. - }) + const ajv = new Ajv({ + removeAdditional: true, // remove additional properties + useDefaults: true, // replace missing properties and items with the values from corresponding default keyword + coerceTypes: true, // change data type of data to match type keyword + nullable: true // support keyword "nullable" from Open API 3 specification. + }) - ajv.addFormat('binary', { - type: 'string', - validate: () => true - }) - ajv.addFormat('byte', { - type: 'string', - validate: () => true - }) - ajv.addFormat('int32', { - type: 'number', - validate: () => true - }) - ajv.addFormat('int64', { - type: 'number', - validate: () => true - }) - - return function (schema) { - return ajv.compile(schema) - } + ajv.addFormat('binary', { + type: 'string', + validate: () => true + }) + ajv.addFormat('byte', { + type: 'string', + validate: () => true + }) + ajv.addFormat('int32', { + type: 'number', + validate: () => true + }) + ajv.addFormat('int64', { + type: 'number', + validate: () => true + }) + return function (schema) { + return ajv.compile(schema) + } } -module.exports = validatorCompiler; \ No newline at end of file +module.exports = validatorCompiler From aa9c29e8e775dbeddd0879d038d8c993947706bb Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 13:37:44 +0530 Subject: [PATCH 04/24] fixed test issue --- examples/options.js | 1 + test/csp.js | 2 +- test/decorator.js | 2 +- test/esm/esm.mjs | 4 ++-- test/integration.js | 2 +- test/mode/static.js | 2 +- test/route.js | 2 +- test/spec/openapi/option.js | 2 +- test/spec/openapi/refs.js | 2 +- test/spec/openapi/route.js | 2 +- test/spec/openapi/schema.js | 2 +- test/spec/swagger/option.js | 2 +- test/spec/swagger/refs.js | 2 +- test/spec/swagger/route.js | 2 +- test/spec/swagger/schema.js | 2 +- test/transform.js | 2 +- 16 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/options.js b/examples/options.js index ea31100a..3274f864 100644 --- a/examples/options.js +++ b/examples/options.js @@ -9,6 +9,7 @@ const swaggerOption = { schemes: ['http'], consumes: ['application/json'], produces: ['application/json'], + customCompiler: false, tags: [ { name: 'tag' } ], diff --git a/test/csp.js b/test/csp.js index 445a6e35..21fd5c07 100644 --- a/test/csp.js +++ b/test/csp.js @@ -4,7 +4,7 @@ const t = require('tap') const test = t.test const Fastify = require('fastify') const fastifyHelmet = require('fastify-helmet') -const fastifySwagger = require('../index') +const { fastifySwagger } = require('../index') const { schemaQuerystring, schemaBody, diff --git a/test/decorator.js b/test/decorator.js index 20bd9681..33345465 100644 --- a/test/decorator.js +++ b/test/decorator.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -const fastifySwagger = require('../index') +const { fastifySwagger } = require('../index') test('fastify.swagger should exist', t => { t.plan(2) diff --git a/test/esm/esm.mjs b/test/esm/esm.mjs index a2d7c450..de782bdf 100644 --- a/test/esm/esm.mjs +++ b/test/esm/esm.mjs @@ -1,11 +1,11 @@ import t from 'tap' import Fastify from 'fastify' -import swaggerDefault from '../../index.js' +import { fastifySwagger } from '../../index.js' t.test('esm support', async t => { const fastify = Fastify() - fastify.register(swaggerDefault) + fastify.register(fastifySwagger) await fastify.ready() diff --git a/test/integration.js b/test/integration.js index acabd6a8..bd2d9911 100644 --- a/test/integration.js +++ b/test/integration.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -const fastifySwagger = require('../index') +const { fastifySwagger } = require('../index') const fastifyHelmet = require('fastify-helmet') const swaggerCSP = require('../static/csp.json') test('fastify will response swagger csp', t => { diff --git a/test/mode/static.js b/test/mode/static.js index 921ddeb7..5d15aa10 100644 --- a/test/mode/static.js +++ b/test/mode/static.js @@ -3,7 +3,7 @@ const path = require('path') const { test } = require('tap') const Fastify = require('fastify') -const fastifySwagger = require('../../index') +const { fastifySwagger } = require('../../index') const fastifySwaggerDynamic = require('../../lib/mode/dynamic') const yaml = require('js-yaml') const Swagger = require('swagger-parser') diff --git a/test/route.js b/test/route.js index 3064d985..51ae717f 100644 --- a/test/route.js +++ b/test/route.js @@ -5,7 +5,7 @@ const test = t.test const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const fastifySwagger = require('../index') +const { fastifySwagger } = require('../index') const { schemaQuerystring, schemaBody, diff --git a/test/spec/openapi/option.js b/test/spec/openapi/option.js index 32bec212..3315987a 100644 --- a/test/spec/openapi/option.js +++ b/test/spec/openapi/option.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') const { readPackageJson } = require('../../../lib/util/common') const { openapiOption } = require('../../../examples/options') diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index 7ed5cff6..3433e1f4 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') const { openapiOption } = require('../../../examples/options') test('support $ref schema', t => { diff --git a/test/spec/openapi/route.js b/test/spec/openapi/route.js index ec901662..a28bd3af 100644 --- a/test/spec/openapi/route.js +++ b/test/spec/openapi/route.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') const { openapiOption, schemaBody, diff --git a/test/spec/openapi/schema.js b/test/spec/openapi/schema.js index 719f27bb..9f6a2def 100644 --- a/test/spec/openapi/schema.js +++ b/test/spec/openapi/schema.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') const { openapiOption, schemaAllOf diff --git a/test/spec/swagger/option.js b/test/spec/swagger/option.js index fdf21e30..ee8717fe 100644 --- a/test/spec/swagger/option.js +++ b/test/spec/swagger/option.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') const { readPackageJson } = require('../../../lib/util/common') const { swaggerOption } = require('../../../examples/options') diff --git a/test/spec/swagger/refs.js b/test/spec/swagger/refs.js index 9acc349d..1e28b989 100644 --- a/test/spec/swagger/refs.js +++ b/test/spec/swagger/refs.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') test('support $ref schema', async t => { t.plan(1) diff --git a/test/spec/swagger/route.js b/test/spec/swagger/route.js index 1e15b1c7..a015c488 100644 --- a/test/spec/swagger/route.js +++ b/test/spec/swagger/route.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') const { swaggerOption, schemaBody, diff --git a/test/spec/swagger/schema.js b/test/spec/swagger/schema.js index 11f9e324..9c44686f 100644 --- a/test/spec/swagger/schema.js +++ b/test/spec/swagger/schema.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const fastifySwagger = require('../../../index') +const { fastifySwagger } = require('../../../index') test('support file in json schema', async t => { const opts7 = { diff --git a/test/transform.js b/test/transform.js index 9407c74c..399ebebc 100644 --- a/test/transform.js +++ b/test/transform.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -const fastifySwagger = require('../index') +const { fastifySwagger } = require('../index') const Joi = require('joi') const Convert = require('joi-to-json') From ad5a183a839a194d1bdd3995ca14cf5cec33a048 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 16:02:25 +0530 Subject: [PATCH 05/24] test added for validatorCompiler --- examples/options.js | 2 +- index.js | 4 ++++ package.json | 1 + test/validatorCompiler.js | 48 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/validatorCompiler.js diff --git a/examples/options.js b/examples/options.js index 3274f864..5d8c3c0c 100644 --- a/examples/options.js +++ b/examples/options.js @@ -9,7 +9,6 @@ const swaggerOption = { schemes: ['http'], consumes: ['application/json'], produces: ['application/json'], - customCompiler: false, tags: [ { name: 'tag' } ], @@ -31,6 +30,7 @@ const swaggerOption = { } const openapiOption = { + customCompiler: true, openapi: { info: { title: 'Test swagger', diff --git a/index.js b/index.js index aa048e41..8edcb9cc 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,11 @@ const validatorCompiler = require('./lib/validatorCompiler') function fastifySwagger (fastify, opts, next) { // enabling custom or validator complier form opts object const customCompiler = opts.customCompiler || null + console.log(customCompiler) + if (customCompiler && customCompiler !== null && typeof customCompiler !== typeof undefined) { + console.log('custom com') + fastify.setValidatorCompiler(validatorCompiler) } diff --git a/package.json b/package.json index f7a0946c..fb287ef9 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "fastify-static": "^4.0.0", "js-yaml": "^4.0.0", "json-schema-resolver": "^1.2.0", + "openapi-schema-validator": "^9.2.0", "openapi-types": "^9.1.0" }, "standard": { diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js new file mode 100644 index 00000000..15de0f32 --- /dev/null +++ b/test/validatorCompiler.js @@ -0,0 +1,48 @@ +const { + test +} = require('tap') +const Fastify = require('fastify') +// const Swagger = require('openapi-schema-validator') +const { + openapiOption +} = require('../examples/options') + +const { + validatorCompiler, + fastifySwagger +} = require('../index') +test('validator compiler is function', t => { + t.type(validatorCompiler, 'function') + t.ok('passed validator compiler is function') + t.end() +}) + +test('validator compiler working', t => { + const fastify = Fastify() + fastify.register(fastifySwagger, openapiOption) + fastify.post('/', { + schema: { + type: 'object', + consumes: ['multipart/form-data'], + body: { + type: 'object', + properties: { + upload: { + type: 'file', + format: 'binary' + } + } + } + } + + }, () => {}) + + fastify.ready(err => { + if (err) { + t.error(err) + } else { + t.ok('passed validator compiler working') + t.end() + } + }) +}) From 7842391e01e8654c3facd782872272d3cd97df28 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 16:05:38 +0530 Subject: [PATCH 06/24] unused package removed --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index fb287ef9..f7a0946c 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "fastify-static": "^4.0.0", "js-yaml": "^4.0.0", "json-schema-resolver": "^1.2.0", - "openapi-schema-validator": "^9.2.0", "openapi-types": "^9.1.0" }, "standard": { From b673acd200abdbd23907995703846aad7ac51820 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 16:08:16 +0530 Subject: [PATCH 07/24] contributor name added --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index f7a0946c..03676fde 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,10 @@ { "name": "Matteo Collina", "email": "hello@matteocollina.com" + }, + { + "name": "Aditya panther", + "email": "raj68518@gmail.com" } ], "license": "MIT", From f24b804c603aa881ea2e1a80b9ef612048ddd6e4 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 19:04:50 +0530 Subject: [PATCH 08/24] removed contributor name --- package.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/package.json b/package.json index 03676fde..f7a0946c 100644 --- a/package.json +++ b/package.json @@ -27,10 +27,6 @@ { "name": "Matteo Collina", "email": "hello@matteocollina.com" - }, - { - "name": "Aditya panther", - "email": "raj68518@gmail.com" } ], "license": "MIT", From 27cadd30d9287b35b93d71b10ce2c1c68ee70262 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 19:38:01 +0530 Subject: [PATCH 09/24] return as a plugin issue fixed --- index.js | 7 ++----- lib/validatorCompiler.js | 2 ++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8edcb9cc..7aa03491 100644 --- a/index.js +++ b/index.js @@ -42,8 +42,5 @@ const plugin = fp(fastifySwagger, { }) module.exports.default = plugin - -module.exports = { - fastifySwagger: plugin, - validatorCompiler -} +module.exports.fastifySwagger = plugin +module.exports.validatorCompiler = validatorCompiler diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index d0ccd0c0..e75a17d8 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -26,6 +26,8 @@ const validatorCompiler = () => { }) return function (schema) { + console.log(schema) + return ajv.compile(schema) } } From 5eb452b7390bb287d327093438cb4d46e7de4990 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 19:47:28 +0530 Subject: [PATCH 10/24] restored test --- test/csp.js | 2 +- test/decorator.js | 2 +- test/esm/esm.mjs | 2 +- test/integration.js | 2 +- test/mode/static.js | 2 +- test/route.js | 2 +- test/spec/openapi/option.js | 2 +- test/spec/openapi/refs.js | 2 +- test/spec/openapi/route.js | 2 +- test/spec/openapi/schema.js | 2 +- test/spec/swagger/option.js | 2 +- test/spec/swagger/refs.js | 2 +- test/spec/swagger/route.js | 2 +- test/spec/swagger/schema.js | 2 +- test/transform.js | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test/csp.js b/test/csp.js index 21fd5c07..445a6e35 100644 --- a/test/csp.js +++ b/test/csp.js @@ -4,7 +4,7 @@ const t = require('tap') const test = t.test const Fastify = require('fastify') const fastifyHelmet = require('fastify-helmet') -const { fastifySwagger } = require('../index') +const fastifySwagger = require('../index') const { schemaQuerystring, schemaBody, diff --git a/test/decorator.js b/test/decorator.js index 33345465..20bd9681 100644 --- a/test/decorator.js +++ b/test/decorator.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -const { fastifySwagger } = require('../index') +const fastifySwagger = require('../index') test('fastify.swagger should exist', t => { t.plan(2) diff --git a/test/esm/esm.mjs b/test/esm/esm.mjs index de782bdf..820a0719 100644 --- a/test/esm/esm.mjs +++ b/test/esm/esm.mjs @@ -1,6 +1,6 @@ import t from 'tap' import Fastify from 'fastify' -import { fastifySwagger } from '../../index.js' +import fastifySwagger from '../../index.js' t.test('esm support', async t => { const fastify = Fastify() diff --git a/test/integration.js b/test/integration.js index bd2d9911..acabd6a8 100644 --- a/test/integration.js +++ b/test/integration.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -const { fastifySwagger } = require('../index') +const fastifySwagger = require('../index') const fastifyHelmet = require('fastify-helmet') const swaggerCSP = require('../static/csp.json') test('fastify will response swagger csp', t => { diff --git a/test/mode/static.js b/test/mode/static.js index 5d15aa10..921ddeb7 100644 --- a/test/mode/static.js +++ b/test/mode/static.js @@ -3,7 +3,7 @@ const path = require('path') const { test } = require('tap') const Fastify = require('fastify') -const { fastifySwagger } = require('../../index') +const fastifySwagger = require('../../index') const fastifySwaggerDynamic = require('../../lib/mode/dynamic') const yaml = require('js-yaml') const Swagger = require('swagger-parser') diff --git a/test/route.js b/test/route.js index 51ae717f..3064d985 100644 --- a/test/route.js +++ b/test/route.js @@ -5,7 +5,7 @@ const test = t.test const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const { fastifySwagger } = require('../index') +const fastifySwagger = require('../index') const { schemaQuerystring, schemaBody, diff --git a/test/spec/openapi/option.js b/test/spec/openapi/option.js index 3315987a..32bec212 100644 --- a/test/spec/openapi/option.js +++ b/test/spec/openapi/option.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') const { readPackageJson } = require('../../../lib/util/common') const { openapiOption } = require('../../../examples/options') diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index 3433e1f4..7ed5cff6 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') const { openapiOption } = require('../../../examples/options') test('support $ref schema', t => { diff --git a/test/spec/openapi/route.js b/test/spec/openapi/route.js index a28bd3af..ec901662 100644 --- a/test/spec/openapi/route.js +++ b/test/spec/openapi/route.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') const { openapiOption, schemaBody, diff --git a/test/spec/openapi/schema.js b/test/spec/openapi/schema.js index 9f6a2def..719f27bb 100644 --- a/test/spec/openapi/schema.js +++ b/test/spec/openapi/schema.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') const { openapiOption, schemaAllOf diff --git a/test/spec/swagger/option.js b/test/spec/swagger/option.js index ee8717fe..fdf21e30 100644 --- a/test/spec/swagger/option.js +++ b/test/spec/swagger/option.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') const { readPackageJson } = require('../../../lib/util/common') const { swaggerOption } = require('../../../examples/options') diff --git a/test/spec/swagger/refs.js b/test/spec/swagger/refs.js index 1e28b989..9acc349d 100644 --- a/test/spec/swagger/refs.js +++ b/test/spec/swagger/refs.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') test('support $ref schema', async t => { t.plan(1) diff --git a/test/spec/swagger/route.js b/test/spec/swagger/route.js index a015c488..1e15b1c7 100644 --- a/test/spec/swagger/route.js +++ b/test/spec/swagger/route.js @@ -4,7 +4,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') const yaml = require('js-yaml') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') const { swaggerOption, schemaBody, diff --git a/test/spec/swagger/schema.js b/test/spec/swagger/schema.js index 9c44686f..11f9e324 100644 --- a/test/spec/swagger/schema.js +++ b/test/spec/swagger/schema.js @@ -3,7 +3,7 @@ const { test } = require('tap') const Fastify = require('fastify') const Swagger = require('swagger-parser') -const { fastifySwagger } = require('../../../index') +const fastifySwagger = require('../../../index') test('support file in json schema', async t => { const opts7 = { diff --git a/test/transform.js b/test/transform.js index 399ebebc..9407c74c 100644 --- a/test/transform.js +++ b/test/transform.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -const { fastifySwagger } = require('../index') +const fastifySwagger = require('../index') const Joi = require('joi') const Convert = require('joi-to-json') From 507c38201bd943efdc052c980dea2e1abaede67d Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 21:08:30 +0530 Subject: [PATCH 11/24] validation added --- lib/validatorCompiler.js | 60 ++++++++++++++++++++++++++++++++++------ package.json | 1 + 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index e75a17d8..a0aba2d5 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -1,34 +1,76 @@ const Ajv = require('ajv') +const ajvOpenApi = require('ajv-openapi') + +const openAjv = (compileObj, data) => { + const ajvOptions = { + schemaId: 'auto', + format: 'full', + coerceTypes: true, + unknownFormats: 'ignore', + useDefaults: true, + nullable: true + } + + const openApiOptions = { + useDraft04: true + } + + const ajv = ajvOpenApi( + new Ajv(ajvOptions), + openApiOptions + ) + return ajv.compile(compileObj)(data) +} + +const binaryValidation = async (data) => { + return openAjv({ format: 'binary', type: 'string' }, data) +} + +const byteValidation = async (data) => { + return openAjv({ format: 'byte', type: 'string' }, data) +} + +const int64bitValidation = async (data) => { + return openAjv({ format: 'int64', type: 'integer' }, data) +} + +const int32bitValidation = async (data) => { + return openAjv({ format: 'int32', type: 'integer' }, data) +} const validatorCompiler = () => { const ajv = new Ajv({ - removeAdditional: true, // remove additional properties - useDefaults: true, // replace missing properties and items with the values from corresponding default keyword - coerceTypes: true, // change data type of data to match type keyword - nullable: true // support keyword "nullable" from Open API 3 specification. + removeAdditional: true, + useDefaults: true, + coerceTypes: true, + nullable: true }) ajv.addFormat('binary', { type: 'string', - validate: () => true + validate: binaryValidation }) ajv.addFormat('byte', { type: 'string', - validate: () => true + validate: byteValidation }) ajv.addFormat('int32', { type: 'number', - validate: () => true + validate: int32bitValidation }) ajv.addFormat('int64', { type: 'number', - validate: () => true + validate: int64bitValidation }) return function (schema) { console.log(schema) + const d = ajv.compile(schema) + console.log('dom') + + console.log(d) - return ajv.compile(schema) + return d } } diff --git a/package.json b/package.json index f7a0946c..b2903f2f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ }, "dependencies": { "ajv": "^6.12.6", + "ajv-openapi": "^2.0.0", "fastify-plugin": "^3.0.0", "fastify-static": "^4.0.0", "js-yaml": "^4.0.0", From 6d8489dbe4d8ac1c5eb7f552dc5df15f51380adf Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 21:12:13 +0530 Subject: [PATCH 12/24] async removed from functions --- lib/validatorCompiler.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index a0aba2d5..de065c21 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -22,19 +22,19 @@ const openAjv = (compileObj, data) => { return ajv.compile(compileObj)(data) } -const binaryValidation = async (data) => { +const binaryValidation = (data) => { return openAjv({ format: 'binary', type: 'string' }, data) } -const byteValidation = async (data) => { +const byteValidation = (data) => { return openAjv({ format: 'byte', type: 'string' }, data) } -const int64bitValidation = async (data) => { +const int64bitValidation = (data) => { return openAjv({ format: 'int64', type: 'integer' }, data) } -const int32bitValidation = async (data) => { +const int32bitValidation = (data) => { return openAjv({ format: 'int32', type: 'integer' }, data) } From 6238ed214dd9939e7eca4b833c4102a73a46f1de Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 29 Aug 2021 21:16:35 +0530 Subject: [PATCH 13/24] log removed --- lib/validatorCompiler.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index de065c21..27f36a7e 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -64,13 +64,7 @@ const validatorCompiler = () => { }) return function (schema) { - console.log(schema) - const d = ajv.compile(schema) - console.log('dom') - - console.log(d) - - return d + return ajv.compile(schema) } } From 8992baa3408d5520b6322890575abc16d45d35fc Mon Sep 17 00:00:00 2001 From: Aditya panther Date: Sat, 4 Sep 2021 14:33:25 +0530 Subject: [PATCH 14/24] Update lib/validatorCompiler.js Co-authored-by: Manuel Spigolon --- lib/validatorCompiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index 27f36a7e..01aadc68 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -1,7 +1,7 @@ const Ajv = require('ajv') const ajvOpenApi = require('ajv-openapi') -const openAjv = (compileObj, data) => { +function openAjv (compileObj, data) { const ajvOptions = { schemaId: 'auto', format: 'full', From c93cd83cb6a1098bf24b3e88f41fa9d3543df5ea Mon Sep 17 00:00:00 2001 From: Aditya panther Date: Sat, 4 Sep 2021 14:33:34 +0530 Subject: [PATCH 15/24] Update lib/validatorCompiler.js Co-authored-by: Manuel Spigolon --- lib/validatorCompiler.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index 01aadc68..acef8ad9 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -1,3 +1,5 @@ +'use strict' + const Ajv = require('ajv') const ajvOpenApi = require('ajv-openapi') From a6a2fb977091729fa38f8116bf0a2c9c58135512 Mon Sep 17 00:00:00 2001 From: Aditya panther Date: Sat, 4 Sep 2021 14:34:14 +0530 Subject: [PATCH 16/24] Update index.js Co-authored-by: Frazer Smith --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7aa03491..995224d2 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ function fastifySwagger (fastify, opts, next) { const customCompiler = opts.customCompiler || null console.log(customCompiler) - if (customCompiler && customCompiler !== null && typeof customCompiler !== typeof undefined) { + if (customCompiler) { console.log('custom com') fastify.setValidatorCompiler(validatorCompiler) From 175d8fa6544b4f34a51cac796b391a52c2d7c44b Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sat, 4 Sep 2021 17:04:02 +0530 Subject: [PATCH 17/24] custom validator added and other issue fixed --- index.js | 9 ++--- lib/validatorCompiler.js | 71 +++++++++++++++++++++++++-------------- package.json | 2 +- test/validatorCompiler.js | 68 +++++++++++++++++++++++++++++++++---- 4 files changed, 110 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 995224d2..577c2974 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,12 @@ 'use strict' const fp = require('fastify-plugin') -const validatorCompiler = require('./lib/validatorCompiler') +const { validatorCompiler } = require('./lib/validatorCompiler') function fastifySwagger (fastify, opts, next) { // enabling custom or validator complier form opts object const customCompiler = opts.customCompiler || null - console.log(customCompiler) - if (customCompiler) { - console.log('custom com') - fastify.setValidatorCompiler(validatorCompiler) } @@ -41,6 +37,5 @@ const plugin = fp(fastifySwagger, { name: 'fastify-swagger' }) -module.exports.default = plugin -module.exports.fastifySwagger = plugin +module.exports = plugin module.exports.validatorCompiler = validatorCompiler diff --git a/lib/validatorCompiler.js b/lib/validatorCompiler.js index acef8ad9..dc69fa60 100644 --- a/lib/validatorCompiler.js +++ b/lib/validatorCompiler.js @@ -1,46 +1,61 @@ 'use strict' const Ajv = require('ajv') -const ajvOpenApi = require('ajv-openapi') +const Decimal = require('decimal.js') -function openAjv (compileObj, data) { - const ajvOptions = { - schemaId: 'auto', - format: 'full', - coerceTypes: true, - unknownFormats: 'ignore', - useDefaults: true, - nullable: true - } +const range = { - const openApiOptions = { - useDraft04: true + int64Bit: { + min: new Decimal('-9223372036854775808'), + max: new Decimal('9223372036854775807') + }, + int32Bit: { + min: new Decimal('-2147483648'), + max: new Decimal('2147483647') + }, + bytes: { + min: new Decimal('-128'), + max: new Decimal('127') } - const ajv = ajvOpenApi( - new Ajv(ajvOptions), - openApiOptions - ) - return ajv.compile(compileObj)(data) } const binaryValidation = (data) => { - return openAjv({ format: 'binary', type: 'string' }, data) + const binaryRegex = /^[0-1]{1,}$/g + return binaryRegex.test(data) } const byteValidation = (data) => { - return openAjv({ format: 'byte', type: 'string' }, data) + const notBase64 = /[^A-Z0-9+/=]/i + + const len = data.length + if (!len || len % 4 !== 0 || notBase64.test(data)) { + return false + } + + const firstPaddingChar = data.indexOf('=') + return firstPaddingChar === -1 || + firstPaddingChar === len - 1 || + (firstPaddingChar === len - 2 && data[len - 1] === '=') } const int64bitValidation = (data) => { - return openAjv({ format: 'int64', type: 'integer' }, data) + return ( + Number.isInteger(+data) && + range.int64Bit.max.greaterThanOrEqualTo(data) && + range.int64Bit.min.lessThanOrEqualTo(data) + ) } const int32bitValidation = (data) => { - return openAjv({ format: 'int32', type: 'integer' }, data) + return ( + Number.isInteger(+data) && + range.int32Bit.max.greaterThanOrEqualTo(data) && + range.int32Bit.min.lessThanOrEqualTo(data) + ) } -const validatorCompiler = () => { +const validatorCompiler = (schema) => { const ajv = new Ajv({ removeAdditional: true, useDefaults: true, @@ -65,9 +80,13 @@ const validatorCompiler = () => { validate: int64bitValidation }) - return function (schema) { - return ajv.compile(schema) - } + return ajv.compile(schema) } -module.exports = validatorCompiler +module.exports = { + validatorCompiler, + int32bitValidation, + int64bitValidation, + binaryValidation, + byteValidation +} diff --git a/package.json b/package.json index b2903f2f..f5192a65 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ }, "dependencies": { "ajv": "^6.12.6", - "ajv-openapi": "^2.0.0", + "decimal.js": "^3.0.0", "fastify-plugin": "^3.0.0", "fastify-static": "^4.0.0", "js-yaml": "^4.0.0", diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js index 15de0f32..8b817516 100644 --- a/test/validatorCompiler.js +++ b/test/validatorCompiler.js @@ -11,6 +11,9 @@ const { validatorCompiler, fastifySwagger } = require('../index') + +const { binaryValidation, byteValidation, int32bitValidation, int64bitValidation } = require('../lib/validatorCompiler') + test('validator compiler is function', t => { t.type(validatorCompiler, 'function') t.ok('passed validator compiler is function') @@ -38,11 +41,64 @@ test('validator compiler working', t => { }, () => {}) fastify.ready(err => { - if (err) { - t.error(err) - } else { - t.ok('passed validator compiler working') - t.end() - } + t.error(err) + t.end() }) }) + +test('binary validation', t => { + const data = binaryValidation('0100110001') + if (data) { + t.pass() + } else { + t.fail() + } + t.end() +}) + +test('byte validation', t => { + const byteData = 'MTIz' + const data1 = byteValidation(byteData) + const data2 = byteValidation('abc') + + if (data1) { + t.pass() + } else { + t.fail() + } + + if (!data2) { + t.pass() + } else { + t.fail() + } + + const data3 = byteValidation('QCPvv6VAI++/pQ==') + + if (data3) { + t.pass() + } else { + t.fail() + } + t.end() +}) + +test('int 32 bit validation', t => { + const data = int32bitValidation('123') + if (data) { + t.pass() + } else { + t.fail() + } + t.end() +}) + +test('int 64 bit validation', t => { + const data = int64bitValidation('512') + if (data) { + t.pass() + } else { + t.fail() + } + t.end() +}) From 97327d6e98e3ad72b7d7a797901a64469774cd91 Mon Sep 17 00:00:00 2001 From: raj68518 Date: Sun, 5 Sep 2021 20:36:17 +0530 Subject: [PATCH 18/24] docs fixed and comments removed --- README.md | 6 ++-- test/validatorCompiler.js | 65 +++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 0a20a622..165a1f67 100644 --- a/README.md +++ b/README.md @@ -123,8 +123,8 @@ fastify.ready(err => { }) ``` - -## OpenAPI spec (specification) + +## OpenAPI validation formats There are three way to enable open api specification @@ -689,7 +689,7 @@ You can integration this plugin with ```fastify-helmet``` with some little work. ### upload File Schema -1) Enable open api specification (`docs available in upper`) +1) Enable open api validation formats (`docs available in upper`) 2) FileUpload Schema diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js index 8b817516..f122fbd4 100644 --- a/test/validatorCompiler.js +++ b/test/validatorCompiler.js @@ -2,7 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') -// const Swagger = require('openapi-schema-validator') +// const Swagger = require('swagger-parser') const { openapiOption } = require('../examples/options') @@ -20,32 +20,6 @@ test('validator compiler is function', t => { t.end() }) -test('validator compiler working', t => { - const fastify = Fastify() - fastify.register(fastifySwagger, openapiOption) - fastify.post('/', { - schema: { - type: 'object', - consumes: ['multipart/form-data'], - body: { - type: 'object', - properties: { - upload: { - type: 'file', - format: 'binary' - } - } - } - } - - }, () => {}) - - fastify.ready(err => { - t.error(err) - t.end() - }) -}) - test('binary validation', t => { const data = binaryValidation('0100110001') if (data) { @@ -102,3 +76,40 @@ test('int 64 bit validation', t => { } t.end() }) + +test('validator compiler working', t => { + const fastify = Fastify() + fastify.register(fastifySwagger, openapiOption) + fastify.post('/', { + schema: { + type: 'object', + consumes: ['multipart/form-data'], + body: { + type: 'object', + properties: { + upload: { + type: 'file', + format: 'binary' + } + } + } + } + + }, () => {}) + + fastify.ready(err => { + t.error(err) + t.end() + }) + + // fastify.inject({ + // method: 'POST', + // url: '/' + // }, (err, res) => { + // t.error(err) + // t.equal(res.statusCode, 200) + // t.ok() + // // const payload = JSON.parse(res.payload) + // }) + // t.end() +}) From 2ec033b9fb18d90b5ad9e53b8085dd701036475c Mon Sep 17 00:00:00 2001 From: Aditya panther Date: Tue, 7 Sep 2021 15:07:26 +0530 Subject: [PATCH 19/24] Update README.md Co-authored-by: Frazer Smith --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 165a1f67..1cf97e9a 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ An example of using `fastify-swagger` with `static` mode enabled can be found [h | Option | Default | Description | | ------------------ | ---------------- | ------------------------------------------------------------------------------------------------------------------------- | | exposeRoute | false | Exposes documentation route. | - | customCompiler | false | if it's `true` means fastify will support swagger open api specs (binary, byte, int32, int64) as a schema | + | customCompiler | false | If `true` enable OpenAPI validation formats (binary, byte, int32, int64) as a schema | | hiddenTag | X-HIDDEN | Tag to control hiding of routes. | | hideUntagged | false | If `true` remove routes without tags from resulting Swagger/OpenAPI schema file. | | initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). | From 825585dc9d7a2b62690d46ea77a2ee7341a705ee Mon Sep 17 00:00:00 2001 From: Aditya panther Date: Tue, 7 Sep 2021 15:07:42 +0530 Subject: [PATCH 20/24] Update README.md Co-authored-by: Frazer Smith --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1cf97e9a..374498a0 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ fastify.ready(err => { ## OpenAPI validation formats -There are three way to enable open api specification +There are three way to enable the use of OpenAPI validation formats: 1) ### Project Level From faf106e4e16d63d33f13d498fe547a7c68fc1192 Mon Sep 17 00:00:00 2001 From: adityapanther Date: Sun, 21 Nov 2021 12:40:10 +0530 Subject: [PATCH 21/24] comment removed --- test/validatorCompiler.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js index f122fbd4..00c07f1c 100644 --- a/test/validatorCompiler.js +++ b/test/validatorCompiler.js @@ -2,7 +2,6 @@ const { test } = require('tap') const Fastify = require('fastify') -// const Swagger = require('swagger-parser') const { openapiOption } = require('../examples/options') @@ -101,15 +100,4 @@ test('validator compiler working', t => { t.error(err) t.end() }) - - // fastify.inject({ - // method: 'POST', - // url: '/' - // }, (err, res) => { - // t.error(err) - // t.equal(res.statusCode, 200) - // t.ok() - // // const payload = JSON.parse(res.payload) - // }) - // t.end() }) From 0a713760e8dd4fa84100a8ba043ccebf15abcf07 Mon Sep 17 00:00:00 2001 From: adityapanther Date: Sun, 21 Nov 2021 13:27:30 +0530 Subject: [PATCH 22/24] fastify inject added --- test/validatorCompiler.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js index 00c07f1c..e2c902c2 100644 --- a/test/validatorCompiler.js +++ b/test/validatorCompiler.js @@ -82,10 +82,13 @@ test('validator compiler working', t => { fastify.post('/', { schema: { type: 'object', - consumes: ['multipart/form-data'], + consumes: ['multipart/form-data', 'application/json'], body: { type: 'object', properties: { + fileName: { + type: 'string' + }, upload: { type: 'file', format: 'binary' @@ -94,10 +97,32 @@ test('validator compiler working', t => { } } - }, () => {}) + }, (req, res) => { + return { + status: 'success', + code: 200, + msg: 'operation completed successfully' + } + }) - fastify.ready(err => { - t.error(err) - t.end() + fastify.ready(() => { + fastify.inject({ + method: 'POST', + url: '/', + headers: { + 'content-type': 'application/json' + }, + payload: { + fileName: 'aditya_picture' + } + }, (err, res) => { + console.log('res..............') + const data = res.json() + console.log() + t.error(err) + t.equal(data.code, 200) + t.same(data, { status: 'success', code: 200, msg: 'operation completed successfully' }) + t.end() + }) }) }) From 291a64691f85142b9539e2e6a313fd3d8213af7f Mon Sep 17 00:00:00 2001 From: adityapanther Date: Sun, 21 Nov 2021 13:47:23 +0530 Subject: [PATCH 23/24] log removed --- test/validatorCompiler.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js index e2c902c2..e4589f41 100644 --- a/test/validatorCompiler.js +++ b/test/validatorCompiler.js @@ -116,9 +116,7 @@ test('validator compiler working', t => { fileName: 'aditya_picture' } }, (err, res) => { - console.log('res..............') const data = res.json() - console.log() t.error(err) t.equal(data.code, 200) t.same(data, { status: 'success', code: 200, msg: 'operation completed successfully' }) From f0ee769c90477a96d1dcfe3c173230e4a1208652 Mon Sep 17 00:00:00 2001 From: adityapanther Date: Fri, 26 Nov 2021 12:09:57 +0530 Subject: [PATCH 24/24] strict mode added to test --- test/validatorCompiler.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/validatorCompiler.js b/test/validatorCompiler.js index e4589f41..5e7aae66 100644 --- a/test/validatorCompiler.js +++ b/test/validatorCompiler.js @@ -1,3 +1,5 @@ +'use strict' + const { test } = require('tap')