Skip to content

Commit 705ff91

Browse files
authoredOct 16, 2020
chore(gatsby-plugin-feed): add pluginOptionsSchema export (#27399)
* chore(gatsby-plugin-feed): add pluginOptionsSchema export * Cleanup * Fix original onPreBootstrap validation
1 parent 6ba5d63 commit 705ff91

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed
 

‎packages/gatsby-plugin-feed/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"dependencies": {
1010
"@babel/runtime": "^7.11.2",
1111
"@hapi/joi": "^15.1.1",
12+
"common-tags": "^1.8.0",
1213
"fs-extra": "^8.1.0",
14+
"gatsby-plugin-utils": "^0.2.27",
1315
"lodash.merge": "^4.6.2",
1416
"rss": "^1.2.2"
1517
},

‎packages/gatsby-plugin-feed/src/__tests__/__snapshots__/gatsby-node.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ exports[`Test plugin feed custom query runs 1`] = `"<?xml version=\\"1.0\\" enco
66
77
exports[`Test plugin feed default settings work properly 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[a sample title]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-slug</link><guid isPermaLink=\\"false\\">http://dummy.url/a-slug</guid><content:encoded></content:encoded></item></channel></rss>"`;
88
9-
exports[`Test plugin feed options validation throws when invalid plugin options 1`] = `[Error: [Config Validation]: "output" is required]`;
9+
exports[`Test plugin feed options validation throws when invalid plugin options 1`] = `[Error: [Config Validation]: "feeds[0].output" is required]`;
1010
11-
exports[`Test plugin feed options validation throws when invalid plugin options 2`] = `[Error: [Config Validation]: "query" is required]`;
11+
exports[`Test plugin feed options validation throws when invalid plugin options 2`] = `[Error: [Config Validation]: "feeds[0].query" is required]`;

‎packages/gatsby-plugin-feed/src/gatsby-node.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "fs-extra"
22
import path from "path"
33
import RSS from "rss"
44
import merge from "lodash.merge"
5+
import { Joi } from "gatsby-plugin-utils"
56

67
import { defaultOptions, runQuery } from "./internals"
78
import pluginOptionsSchema from "./plugin-options"
@@ -15,6 +16,10 @@ const warnMessage = (error, behavior) => `
1516
For more info, check out: https://gatsby.dev/adding-rss-feed
1617
`
1718

19+
if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
20+
exports.pluginOptionsSchema = pluginOptionsSchema
21+
}
22+
1823
// TODO: remove in the next major release
1924
// A default function to transform query data into feed entries.
2025
const serialize = ({ query: { site, allMarkdownRemark } }) =>
@@ -35,9 +40,15 @@ exports.onPreBootstrap = async function onPreBootstrap(
3540
delete pluginOptions.plugins
3641

3742
try {
38-
const normalized = await pluginOptionsSchema.validate(pluginOptions)
43+
// TODO: remove this once pluginOptionsSchema is stable
44+
const { value: normalized, error } = await pluginOptionsSchema({
45+
Joi,
46+
}).validate(pluginOptions, {
47+
externals: false,
48+
})
49+
50+
if (error) throw error
3951

40-
// TODO: remove these checks in the next major release
4152
if (!normalized.feeds) {
4253
reporter.warn(
4354
reporter.stripIndent(
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
1-
import Joi from "@hapi/joi"
1+
import { parse } from "gatsby/graphql"
2+
import { stripIndent } from "common-tags"
23

34
// TODO: make serialize required in next major version bump
4-
const feed = Joi.object({
5-
output: Joi.string().required(),
6-
query: Joi.string().required(),
7-
title: Joi.string(),
8-
serialize: Joi.func(),
9-
match: Joi.string(),
10-
link: Joi.string(),
11-
}).unknown(true)
5+
const feed = ({ Joi }) =>
6+
Joi.object({
7+
output: Joi.string().required(),
8+
query: Joi.string().required(),
9+
title: Joi.string(),
10+
serialize: Joi.func(),
11+
match: Joi.string(),
12+
link: Joi.string(),
13+
})
14+
.unknown(true)
15+
.external(({ query }) => {
16+
if (query) {
17+
try {
18+
parse(query)
19+
} catch (e) {
20+
throw new Error(
21+
stripIndent`
22+
Invalid plugin options for "gatsby-plugin-feed":
23+
"query" must be a valid GraphQL query. Received the error "${e.message}"`
24+
)
25+
}
26+
}
27+
})
1228

1329
// TODO: make feeds required in next major version bump
14-
export default Joi.object({
15-
generator: Joi.string(),
16-
query: Joi.string(),
17-
setup: Joi.func(),
18-
feeds: Joi.array().items(feed),
19-
}).unknown(true)
30+
export default ({ Joi }) =>
31+
Joi.object({
32+
generator: Joi.string(),
33+
query: Joi.string(),
34+
setup: Joi.func(),
35+
feeds: Joi.array().items(feed({ Joi })),
36+
})
37+
.unknown(true)
38+
.external(({ query }) => {
39+
if (query) {
40+
try {
41+
parse(query)
42+
} catch (e) {
43+
throw new Error(
44+
stripIndent`
45+
Invalid plugin options for "gatsby-plugin-feed":
46+
"query" must be a valid GraphQL query. Received the error "${e.message}"`
47+
)
48+
}
49+
}
50+
})

0 commit comments

Comments
 (0)
Please sign in to comment.