Skip to content

Commit 26667be

Browse files
mxstbrmeganesu
andauthoredOct 16, 2020
docs: document pluginOptionsSchema (#27337)
* Add "How to validate options" to "Configuring Plugin Usage with Plugin Options" doc * Improve API docs for pluginOptionsSchema * Update docs based on suggestions * Update docs/docs/configuring-usage-with-plugin-options.md Co-authored-by: Megan Sullivan <megan@gatsbyjs.com> Co-authored-by: Megan Sullivan <megan@gatsbyjs.com>
1 parent 8330683 commit 26667be

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
 

‎docs/docs/configuring-usage-with-plugin-options.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ For example, `gatsby-plugin-console-log` can access the `message` in order to lo
2828
```javascript:title=plugins/gatsby-plugin-console-log/gatsby-node.js
2929
exports.onPreInit = (_, pluginOptions) => {
3030
console.log(
31-
`logging: "${pluginOptions.message || `default message`}" to the console` // highlight-line
31+
`logging: "${pluginOptions.message}" to the console` // highlight-line
3232
)
3333
}
3434
```
@@ -54,6 +54,24 @@ The following table lists possible options values and an example plugin that mak
5454

5555
**Note**: Themes (which are a type of plugin) are able to receive options from a site's `gatsby-config` to be used in its `gatsby-config` in order to allow themes to be composed together. This is done by exporting the `gatsby-config` as a function instead of an object. You can see an example of this in the [`gatsby-theme-blog`](https://github.com/gatsbyjs/themes/tree/master/packages/gatsby-theme-blog) and [`gatsby-theme-blog-core`](https://github.com/gatsbyjs/themes/tree/master/packages/gatsby-theme-blog-core) repositories. Plugins are not capable of this functionality.
5656

57+
## How to validate options
58+
59+
In order to make configuration easier for users, plugins can optionally define a [Joi](https://joi.dev) schema to enforce data types for each option using the [`pluginOptionsSchema` API](/docs/node-apis/#pluginOptionsSchema) in `gatsby-node.js`. When users of the plugin pass in configuration options, Gatsby will validate that the options match the schema.
60+
61+
For example, here is what the schema for `gatsby-plugin-console-log` looks like:
62+
63+
```javascript:title=plugins/gatsby-plugin-console-log/gatsby-node.js
64+
exports.pluginOptionsSchema = ({ Joi }) => {
65+
return Joi.object({
66+
optionA: Joi.boolean().required(),
67+
message: Joi.string().required().default(`default message`),
68+
optionB: Joi.boolean(),
69+
})
70+
}
71+
```
72+
73+
This ensures users pass a boolean to `optionA` and `optionB`, and a string to `message`. If they pass options that do not match the schema, the validation will show an error when they run `gatsby develop`. It also defaults the `message` option to "default message" in case users do not pass their own.
74+
5775
## Additional resources
5876

5977
- [Example Gatsby site using plugin options with a local plugin](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-plugin-options)

‎packages/gatsby/src/utils/api-node-docs.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,16 @@ export const onPreExtractQueries = true
430430
export const onCreateDevServer = true
431431

432432
/**
433-
* Called during `gatsby develop` bootstrap to get and validate a plugins options schema
434-
* @param {Joi} $0.Joi The instance of Joi to define the schema with
435-
*
433+
* Run during the bootstrap phase. Plugins can use this to define a schema for their options using
434+
* [Joi](https://joi.dev) to validate the options users pass to the plugin.
435+
* @param {object} $0
436+
* @param {Joi} $0.Joi The instance of [Joi](https://joi.dev) to define the schema
437+
* @example
438+
* exports.pluginOptionsSchema = ({ Joi }) => {
439+
* return Joi.object({
440+
* // Validate that the anonymize option is defined by the user and is a boolean
441+
* anonymize: Joi.boolean().required(),
442+
* })
443+
* }
436444
*/
437445
export const pluginOptionsSchema = true

0 commit comments

Comments
 (0)
Please sign in to comment.