-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): plugin option validation (#27242)
* Add pluginOptionsSchema to google analytics for testing * Add structured error for plugin option validation errors * Validate gatsbyNode.pluginOptionsSchema when defined * Add first test, fix some bugs discovered based on it * process.exit on invalid plugin options * Much nicer structured error logging for invalid plugin options * Move TODO comment to correct place * Remove unused import * Add new API to gatsby-node API tests * Warn on invalid plugin schema * Gate plugin option validation behind feature flag for now * less formal wording * Clean up based on code review * Type GatsbyNode.pluginOptionsSchema * Properly type Joi including our .dotenv() extension * Move custom joi + types to gatsby-plugin-utils * Disable the .dotenv extension for now
- Loading branch information
Showing
15 changed files
with
2,554 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
process.env.GATSBY_RECIPES_NO_COLOR = "true" | ||
process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION = "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 39 additions & 7 deletions
46
packages/gatsby-plugin-google-analytics/src/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
exports.onPreInit = ({ reporter }, options) => { | ||
if (!options.trackingId) { | ||
reporter.warn( | ||
`The Google Analytics plugin requires a tracking ID. Did you mean to add it?` | ||
) | ||
} | ||
} | ||
exports.pluginOptionsSchema = ({ Joi }) => | ||
Joi.object({ | ||
trackingId: Joi.string() | ||
.required() | ||
.description( | ||
`The property ID; the tracking code won't be generated without it` | ||
), | ||
head: Joi.boolean() | ||
.default(false) | ||
.description( | ||
`Defines where to place the tracking script - \`true\` in the head and \`false\` in the body` | ||
), | ||
anonymize: Joi.boolean().default(false), | ||
respectDNT: Joi.boolean().default(false), | ||
exclude: Joi.array() | ||
.items(Joi.string()) | ||
.default([]) | ||
.description(`Avoids sending pageview hits from custom paths`), | ||
pageTransitionDelay: Joi.number() | ||
.default(0) | ||
.description( | ||
`Delays sending pageview hits on route update (in milliseconds)` | ||
), | ||
optimizeId: Joi.string().description( | ||
`Enables Google Optimize using your container Id` | ||
), | ||
experimentId: Joi.string().description( | ||
`Enables Google Optimize Experiment ID` | ||
), | ||
variationId: Joi.string().description( | ||
`Set Variation ID. 0 for original 1,2,3....` | ||
), | ||
defer: Joi.boolean().description( | ||
`Defers execution of google analytics script after page load` | ||
), | ||
sampleRate: Joi.number(), | ||
siteSpeedSampleRate: Joi.number(), | ||
cookieDomain: Joi.string(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./validate" | ||
export * from "./joi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import joi from "joi" | ||
import { PluginOptionsSchemaJoi } from "./utils/plugin-options-schema-joi-type" | ||
|
||
export * from "./utils/plugin-options-schema-joi-type" | ||
export const Joi: PluginOptionsSchemaJoi = joi.extend({ | ||
// This tells Joi to extend _all_ types with .dotenv(), see | ||
// https://github.com/sideway/joi/commit/03adf22eb1f06c47d1583617093edee3a96b3873 | ||
// @ts-ignore Joi types weren't updated with that commit, PR: https://github.com/sideway/joi/pull/2477 | ||
type: /^s/, | ||
rules: { | ||
// NOTE(@mxstbr): Disabled until we decide on the necessity for this API. | ||
// dotenv: { | ||
// args: [`name`], | ||
// validate(value, helpers, args): void { | ||
// if (!args.name) { | ||
// return helpers.error( | ||
// `any.dotenv requires the environment variable name` | ||
// ) | ||
// } | ||
// return value | ||
// }, | ||
// method(name): Schema { | ||
// return this.$_addRule({ name: `dotenv`, args: { name } }) | ||
// }, | ||
// }, | ||
}, | ||
}) |
Oops, something went wrong.