Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial options validation #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/gatsby-plugin-manifest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ manifest—https://developers.google.com/web/fundamentals/engage-and-retain/web-

For more information see the w3 spec https://www.w3.org/TR/appmanifest/ or Mozilla docs https://developer.mozilla.org/en-US/docs/Web/Manifest.

### Plugin options validation

This plugin validates plugin options set in the `gatsby-config.js`. It validates the options used by the plugin and the entire WebAppManifest spec. To see the exact implemntation of the validator see [src/plugin-options.js](src/plugin-options.js).

The WebAppManifest spec is not stable at the time of writing. This version of the validator adhears the [most recent](https://www.w3.org/TR/2019/WD-appmanifest-20190911/) version of the specification available.

## Troubleshooting

### Incompatible library version: sharp.node requires version X or later, but Z provides version Y
Expand Down
161 changes: 161 additions & 0 deletions packages/gatsby-plugin-manifest/src/__tests__/plugin-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
const Joi = require(`@hapi/joi`)

const { validOptions } = require(`../plugin-options`)

const allOptionsValid = {
name: `GatsbyJS`,
short_name: `GatsbyJS`,
start_url: `/`,
background_color: `#f7f0eb`,
display: `standalone`,
theme_color: `#a2466c`,
cache_busting_mode: `none`,
include_favicon: false,
theme_color_in_head: false,
crossOrigin: `use-credentials`,
legacy: false,
icon: `src/images/icon.png`,
icons: [
{
src: `/favicons/android-chrome-192x192.png`,
sizes: `192x192`,
type: `image/png`,
},
{
src: `/favicons/android-chrome-512x512.png`,
sizes: `512x512`,
type: `image/png`,
},
],
lang: `en`,
localize: [
{
start_url: `/de/`,
lang: `de`,
name: `Die coole Anwendung`,
short_name: `Coole Anwendung`,
description: `Die Anwendung macht coole Dinge und macht Ihr Leben besser.`,
},
],
icon_options: {
purpose: `maskable`,
},
}

const allOptionsInvalid = {
cache_busting_mode: false,
include_favicon: `no`,
theme_color_in_head: `no`,
crossOrigin: `allow`,
legacy: `false`,
icon: `bob`,
icons: [
`/favicons/android-chrome-192x192.png`,
`/favicons/android-chrome-512x512.png`,
],
lang: `en`,
localize: {
start_url: `/de/`,
lang: `de`,
name: `Die coole Anwendung`,
short_name: `Coole Anwendung`,
description: `Die Anwendung macht coole Dinge und macht Ihr Leben besser.`,
},
icon_options: [{ key: `purpose`, value: `maskable` }],
}

describe(`Options validation`, () => {
const reporter = {
panic: jest.fn(),
}

beforeEach(() => {
reporter.panic.mockClear()
})

afterEach(() => {
expect.hasAssertions()
})

const schema = validOptions(Joi)

it(`Passes with valid options`, () => {
expect(schema.validate(allOptionsValid)).resolves.toEqual(
expect.any(Object)
)
})

it(`Fails with missing required options`, async () => {
try {
await schema.validate({})
} catch (e) {
expect(e.name).toBe(`ValidationError`)
}
})

it(`Fails with empty options`, async () => {
try {
await schema.validate({
name: ``,
short_name: ``,
})
} catch (e) {
expect(e.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining(`is not allowed to be empty`),
}),
])
)
}
})

it(`Fails with options of wrong types`, async () => {
try {
await schema.validate(allOptionsInvalid)
} catch (e) {
expect(e.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining(`must be a`),
}),
])
)
}
})

it(`Fails if either or both icon or icons don't exist`, async () => {
try {
await schema.validate({})
} catch (e) {
expect(e.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining(
`must contain at least one of [icon, icons]`
),
}),
])
)
}
})

it(`Fails if lang isn't included with the use of localize`, async () => {
try {
let brokeOptions = { ...allOptionsValid }
delete brokeOptions.lang
await schema.validate(brokeOptions)
} catch (e) {
expect(e.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
context: expect.objectContaining({
main: expect.stringContaining(`localize`),
peer: expect.stringContaining(`lang`),
}),
}),
])
)
}
})
})
3 changes: 3 additions & 0 deletions packages/gatsby-plugin-manifest/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path"
import sharp from "./safe-sharp"
import { createContentDigest, cpuCoreCount } from "gatsby-core-utils"
import { defaultIcons, doesIconExist, addDigestToPath } from "./common"
import { validOptions } from "./plugin-options"

sharp.simd(true)

Expand Down Expand Up @@ -196,3 +197,5 @@ const makeManifest = async (cache, reporter, pluginOptions) => {
JSON.stringify(manifest)
)
}

exports.validatePluginOptions = ({ validator }) => validOptions(validator)
Loading