From 625319328fbefb49a016651f910e65e6947f0eac Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 08:49:04 +0200 Subject: [PATCH 1/8] Test passing for wrong cases --- packages/gatsby-plugin-netlify/package.json | 3 +- .../src/__tests__/gatsby-node.js | 48 +++++++++++++++++++ .../gatsby-plugin-netlify/src/gatsby-node.js | 12 +++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js diff --git a/packages/gatsby-plugin-netlify/package.json b/packages/gatsby-plugin-netlify/package.json index 686312f33191e..51215af6c172d 100644 --- a/packages/gatsby-plugin-netlify/package.json +++ b/packages/gatsby-plugin-netlify/package.json @@ -23,7 +23,8 @@ "@babel/cli": "^7.11.6", "@babel/core": "^7.11.6", "babel-preset-gatsby-package": "^0.5.3", - "cross-env": "^7.0.2" + "cross-env": "^7.0.2", + "gatsby-plugin-utils": "^0.2.27" }, "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-netlify#readme", "keywords": [ diff --git a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js new file mode 100644 index 0000000000000..219e4eaca693c --- /dev/null +++ b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js @@ -0,0 +1,48 @@ +import { testPluginOptionsSchema } from "gatsby-plugin-utils" +import { pluginOptionsSchema } from "../gatsby-node" + +// options: { +// headers: {}, // option to add more headers. `Link` headers are transformed by the below criteria +// allPageHeaders: [], // option to add headers for all pages. `Link` headers are transformed by the below criteria +// mergeSecurityHeaders: true, // boolean to turn off the default security headers +// mergeLinkHeaders: true, // boolean to turn off the default gatsby js headers +// mergeCachingHeaders: true, // boolean to turn off the default caching headers +// transformHeaders: (headers, path) => headers, // optional transform for manipulating headers under each path (e.g.sorting), etc. +// generateMatchPathRewrites: true, // boolean to turn off automatic creation of redirect rules for client only paths +// }, + +describe(`gatsby-node.js`, () => { + it(`should provide meaningful errors when fields are invalid`, () => { + const expectedErrors = [ + `"headers" must be of type object`, + `"allPageHeaders" must be an array`, + `"mergeSecurityHeaders" must be a boolean`, + `"mergeLinkHeaders" must be a boolean`, + `"mergeCachingHeaders" must be a boolean`, + `"transformHeaders" must be of type function`, + `"generateMatchPathRewrites" must be a boolean`, + ] + + const { errors } = testPluginOptionsSchema(pluginOptionsSchema, { + headers: `this should be an object`, + allPageHeaders: `this should be an array`, + mergeSecurityHeaders: `this should be a boolean`, + mergeLinkHeaders: `this should be a boolean`, + mergeCachingHeaders: `this should be a boolean`, + transformHeaders: `this should be a function`, + generateMatchPathRewrites: `this should be a boolean`, + }) + + expect(errors).toEqual(expectedErrors) + }) + + it(`should validate the schema`, () => { + const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, { + headers: { + Authorization: `Bearer: Some-Magic-Token`, + }, + }) + + expect(isValid).toBe(true) + }) +}) diff --git a/packages/gatsby-plugin-netlify/src/gatsby-node.js b/packages/gatsby-plugin-netlify/src/gatsby-node.js index 0aa878760a500..1546301481672 100644 --- a/packages/gatsby-plugin-netlify/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/gatsby-node.js @@ -52,3 +52,15 @@ exports.onPostBuild = async ( createRedirects(pluginData, redirects, rewrites), ]) } + +exports.pluginOptionsSchema = function ({ Joi }) { + return Joi.object({ + headers: Joi.object(), + allPageHeaders: Joi.array(), + mergeSecurityHeaders: Joi.boolean(), + mergeLinkHeaders: Joi.boolean(), + mergeCachingHeaders: Joi.boolean(), + transformHeaders: Joi.function(), + generateMatchPathRewrites: Joi.boolean(), + }) +} From ed8693e7daad67a5296d8c82da2fcfa68ab1787d Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 09:24:42 +0200 Subject: [PATCH 2/8] Adding test to verify basic value of schema validations --- .../src/__tests__/gatsby-node.js | 13 ++++++++++--- packages/gatsby-plugin-netlify/src/gatsby-node.js | 12 +++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js index 219e4eaca693c..663dc5d89143f 100644 --- a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js @@ -19,7 +19,7 @@ describe(`gatsby-node.js`, () => { `"mergeSecurityHeaders" must be a boolean`, `"mergeLinkHeaders" must be a boolean`, `"mergeCachingHeaders" must be a boolean`, - `"transformHeaders" must be of type function`, + `"transformHeaders" must have an arity lesser or equal to 2`, `"generateMatchPathRewrites" must be a boolean`, ] @@ -29,7 +29,7 @@ describe(`gatsby-node.js`, () => { mergeSecurityHeaders: `this should be a boolean`, mergeLinkHeaders: `this should be a boolean`, mergeCachingHeaders: `this should be a boolean`, - transformHeaders: `this should be a function`, + transformHeaders: (too, much, args) => ``, generateMatchPathRewrites: `this should be a boolean`, }) @@ -39,8 +39,15 @@ describe(`gatsby-node.js`, () => { it(`should validate the schema`, () => { const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, { headers: { - Authorization: `Bearer: Some-Magic-Token`, + Authorization: [`Bearer: Some-Magic-Token`], + otherHeader: [`some`, `great`, `headers`], }, + allPageHeaders: [`First header`, `Second header`], + mergeSecurityHeaders: true, + mergeLinkHeaders: false, + mergeCachingHeaders: true, + transformHeaders: () => null, + generateMatchPathRewrites: false, }) expect(isValid).toBe(true) diff --git a/packages/gatsby-plugin-netlify/src/gatsby-node.js b/packages/gatsby-plugin-netlify/src/gatsby-node.js index 1546301481672..b87bd782a18f3 100644 --- a/packages/gatsby-plugin-netlify/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/gatsby-node.js @@ -54,13 +54,19 @@ exports.onPostBuild = async ( } exports.pluginOptionsSchema = function ({ Joi }) { + // headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers + const headersSchema = Joi.object().pattern( + /^/, + Joi.array().items(Joi.string()) + ) + return Joi.object({ - headers: Joi.object(), - allPageHeaders: Joi.array(), + headers: headersSchema, + allPageHeaders: Joi.array().items(Joi.string()), mergeSecurityHeaders: Joi.boolean(), mergeLinkHeaders: Joi.boolean(), mergeCachingHeaders: Joi.boolean(), - transformHeaders: Joi.function(), + transformHeaders: Joi.function().maxArity(2), // This should return a "headersSchema" type defined at line 58 generateMatchPathRewrites: Joi.boolean(), }) } From 98f24d4934fce6a39412c95f1d82dc22cca0ac1d Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 09:25:09 +0200 Subject: [PATCH 3/8] Typo --- packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js index 663dc5d89143f..edfc523334614 100644 --- a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js @@ -29,7 +29,7 @@ describe(`gatsby-node.js`, () => { mergeSecurityHeaders: `this should be a boolean`, mergeLinkHeaders: `this should be a boolean`, mergeCachingHeaders: `this should be a boolean`, - transformHeaders: (too, much, args) => ``, + transformHeaders: (too, many, args) => ``, generateMatchPathRewrites: `this should be a boolean`, }) From eb1bb982fdc53f995201ea0345b5ee080cc0196d Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 09:51:25 +0200 Subject: [PATCH 4/8] Removing comments --- .../gatsby-plugin-netlify/src/__tests__/gatsby-node.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js index edfc523334614..1ecaaea492ace 100644 --- a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js @@ -1,16 +1,6 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils" import { pluginOptionsSchema } from "../gatsby-node" -// options: { -// headers: {}, // option to add more headers. `Link` headers are transformed by the below criteria -// allPageHeaders: [], // option to add headers for all pages. `Link` headers are transformed by the below criteria -// mergeSecurityHeaders: true, // boolean to turn off the default security headers -// mergeLinkHeaders: true, // boolean to turn off the default gatsby js headers -// mergeCachingHeaders: true, // boolean to turn off the default caching headers -// transformHeaders: (headers, path) => headers, // optional transform for manipulating headers under each path (e.g.sorting), etc. -// generateMatchPathRewrites: true, // boolean to turn off automatic creation of redirect rules for client only paths -// }, - describe(`gatsby-node.js`, () => { it(`should provide meaningful errors when fields are invalid`, () => { const expectedErrors = [ From 39aced412e9582911334b774fade19ed9b0ab058 Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 10:17:07 +0200 Subject: [PATCH 5/8] Adding env variables check --- packages/gatsby-plugin-netlify/src/gatsby-node.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-netlify/src/gatsby-node.js b/packages/gatsby-plugin-netlify/src/gatsby-node.js index b87bd782a18f3..3fc86af8e5dea 100644 --- a/packages/gatsby-plugin-netlify/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/gatsby-node.js @@ -53,7 +53,7 @@ exports.onPostBuild = async ( ]) } -exports.pluginOptionsSchema = function ({ Joi }) { +const pluginOptionsSchema = function ({ Joi }) { // headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers const headersSchema = Joi.object().pattern( /^/, @@ -70,3 +70,7 @@ exports.pluginOptionsSchema = function ({ Joi }) { generateMatchPathRewrites: Joi.boolean(), }) } + +if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { + exports.pluginOptionsSchema = pluginOptionsSchema +} From c6afbf5a196de57cf5f1cab7487e3cffe60b9df2 Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 10:38:32 +0200 Subject: [PATCH 6/8] ADding description --- .../src/__tests__/gatsby-node.js | 4 +-- .../gatsby-plugin-netlify/src/gatsby-node.js | 33 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js index 1ecaaea492ace..8eb6b9a55da8a 100644 --- a/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js @@ -29,8 +29,8 @@ describe(`gatsby-node.js`, () => { it(`should validate the schema`, () => { const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, { headers: { - Authorization: [`Bearer: Some-Magic-Token`], - otherHeader: [`some`, `great`, `headers`], + "/some-page": [`Bearer: Some-Magic-Token`], + "/some-other-page": [`some`, `great`, `headers`], }, allPageHeaders: [`First header`, `Second header`], mergeSecurityHeaders: true, diff --git a/packages/gatsby-plugin-netlify/src/gatsby-node.js b/packages/gatsby-plugin-netlify/src/gatsby-node.js index 3fc86af8e5dea..41da07487e5d1 100644 --- a/packages/gatsby-plugin-netlify/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/gatsby-node.js @@ -55,19 +55,32 @@ exports.onPostBuild = async ( const pluginOptionsSchema = function ({ Joi }) { // headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers - const headersSchema = Joi.object().pattern( - /^/, - Joi.array().items(Joi.string()) - ) + const headersSchema = Joi.object() + .pattern(/^/, Joi.array().items(Joi.string())) + .description(`Add more Netlify headers to specific pages`) return Joi.object({ headers: headersSchema, - allPageHeaders: Joi.array().items(Joi.string()), - mergeSecurityHeaders: Joi.boolean(), - mergeLinkHeaders: Joi.boolean(), - mergeCachingHeaders: Joi.boolean(), - transformHeaders: Joi.function().maxArity(2), // This should return a "headersSchema" type defined at line 58 - generateMatchPathRewrites: Joi.boolean(), + allPageHeaders: Joi.array() + .items(Joi.string()) + .description(`Add more headers to all the pages`), + mergeSecurityHeaders: Joi.boolean().description( + `When true, turn off the default security headers` + ), + mergeLinkHeaders: Joi.boolean().description( + `When true, turn off the default gatsby js headers` + ), + mergeCachingHeaders: Joi.boolean().description( + `When true, turn off the default caching headers` + ), + transformHeaders: Joi.function() + .maxArity(2) + .description( + `Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array }` + ), + generateMatchPathRewrites: Joi.boolean().description( + `When true, turn off automatic creation of redirect rules for client only paths` + ), }) } From ee2ec9da75afaa51a93dc49ac564a16bbd25cc0e Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 10:41:01 +0200 Subject: [PATCH 7/8] Fixing descrpition --- packages/gatsby-plugin-netlify/src/gatsby-node.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-plugin-netlify/src/gatsby-node.js b/packages/gatsby-plugin-netlify/src/gatsby-node.js index 41da07487e5d1..b73131fd752e0 100644 --- a/packages/gatsby-plugin-netlify/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/gatsby-node.js @@ -65,13 +65,13 @@ const pluginOptionsSchema = function ({ Joi }) { .items(Joi.string()) .description(`Add more headers to all the pages`), mergeSecurityHeaders: Joi.boolean().description( - `When true, turn off the default security headers` + `When set to true, turns off the default security headers` ), mergeLinkHeaders: Joi.boolean().description( - `When true, turn off the default gatsby js headers` + `When set to true, turns off the default gatsby js headers` ), mergeCachingHeaders: Joi.boolean().description( - `When true, turn off the default caching headers` + `When set to true, turns off the default caching headers` ), transformHeaders: Joi.function() .maxArity(2) @@ -79,7 +79,7 @@ const pluginOptionsSchema = function ({ Joi }) { `Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array }` ), generateMatchPathRewrites: Joi.boolean().description( - `When true, turn off automatic creation of redirect rules for client only paths` + `When set to true, turns off automatic creation of redirect rules for client only paths` ), }) } From b3fae6bd0fca71a23b0be2e7349231219e611b38 Mon Sep 17 00:00:00 2001 From: mfrachet Date: Tue, 13 Oct 2020 13:49:13 +0200 Subject: [PATCH 8/8] Review comments --- packages/gatsby-plugin-netlify/src/gatsby-node.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-netlify/src/gatsby-node.js b/packages/gatsby-plugin-netlify/src/gatsby-node.js index b73131fd752e0..a4ec931517676 100644 --- a/packages/gatsby-plugin-netlify/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify/src/gatsby-node.js @@ -53,10 +53,11 @@ exports.onPostBuild = async ( ]) } +const MATH_ALL_KEYS = /^/ const pluginOptionsSchema = function ({ Joi }) { // headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers const headersSchema = Joi.object() - .pattern(/^/, Joi.array().items(Joi.string())) + .pattern(MATH_ALL_KEYS, Joi.array().items(Joi.string())) .description(`Add more Netlify headers to specific pages`) return Joi.object({