-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin options validation (#63)
* feat: add plugin options validation Note: plugin options validation requires gatsby v2.25.0 or later. * test: add plugin options validation tests
- Loading branch information
Showing
9 changed files
with
8,713 additions
and
370 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
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,4 +1,5 @@ | ||
gatsby-node.js | ||
gatsby-ssr.js | ||
!src/gatsby-ssr.js | ||
theme-hydration-script-tag.js | ||
!src/theme-hydration-script-tag.js | ||
!__tests__/** | ||
!src/** |
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,3 +1,8 @@ | ||
module.exports = { | ||
extends: ['plugin:wkovacs64/react', 'prettier', 'prettier/react'], | ||
extends: [ | ||
'plugin:wkovacs64/react', | ||
'plugin:wkovacs64/jest', | ||
'prettier', | ||
'prettier/react', | ||
], | ||
}; |
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,5 +1,6 @@ | ||
package.json | ||
gatsby-node.js | ||
gatsby-ssr.js | ||
!src/gatsby-ssr.js | ||
theme-hydration-script-tag.js | ||
!src/theme-hydration-script-tag.js | ||
!__tests__/** | ||
!src/** |
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,67 @@ | ||
import { testPluginOptionsSchema } from 'gatsby-plugin-utils'; | ||
import { pluginOptionsSchema } from '../gatsby-node'; | ||
|
||
describe('plugin options schema', () => { | ||
it('accepts valid options', async () => { | ||
const options = { | ||
classNameDark: 'dark', | ||
classNameLight: 'light', | ||
storageKey: 'theme', | ||
minify: false, | ||
}; | ||
|
||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
options, | ||
); | ||
|
||
expect(isValid).toBe(true); | ||
expect(errors).toEqual([]); | ||
}); | ||
|
||
it('rejects extra options', async () => { | ||
const options = { | ||
classNameDark: 'dark', | ||
classNameLight: 'light', | ||
storageKey: 'theme', | ||
minify: false, | ||
tinyOrPickle: 'pickle', | ||
}; | ||
|
||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
options, | ||
); | ||
|
||
expect(isValid).toBe(false); | ||
expect(errors).toMatchInlineSnapshot(` | ||
Array [ | ||
"\\"tinyOrPickle\\" is not allowed", | ||
] | ||
`); | ||
}); | ||
|
||
it('rejects invalid option types', async () => { | ||
const options = { | ||
classNameDark: 1, | ||
classNameLight: 2, | ||
storageKey: 3, | ||
minify: 4, | ||
}; | ||
|
||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
options, | ||
); | ||
|
||
expect(isValid).toBe(false); | ||
expect(errors).toMatchInlineSnapshot(` | ||
Array [ | ||
"\\"classNameDark\\" must be a string", | ||
"\\"classNameLight\\" must be a string", | ||
"\\"storageKey\\" must be a string", | ||
"\\"minify\\" must be a boolean", | ||
] | ||
`); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
exports.pluginOptionsSchema = ({ Joi }) => { | ||
return Joi.object({ | ||
classNameDark: Joi.string() | ||
.default('dark-mode') | ||
.description('CSS class name applied in dark mode'), | ||
classNameLight: Joi.string() | ||
.default('light-mode') | ||
.description('CSS class name applied in light mode'), | ||
storageKey: Joi.string() | ||
.default('darkMode') | ||
.description('localStorage key used to persist mode'), | ||
minify: Joi.boolean() | ||
.default(true) | ||
.description('toggle minification of the injected script'), | ||
}); | ||
}; |
Oops, something went wrong.