Skip to content

Commit

Permalink
chore(gatsby-plugin-feed): add pluginOptionsSchema export (#27399)
Browse files Browse the repository at this point in the history
* chore(gatsby-plugin-feed): add pluginOptionsSchema export

* Cleanup

* Fix original onPreBootstrap validation
  • Loading branch information
mxstbr committed Oct 16, 2020
1 parent 6ba5d63 commit 705ff91
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby-plugin-feed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"dependencies": {
"@babel/runtime": "^7.11.2",
"@hapi/joi": "^15.1.1",
"common-tags": "^1.8.0",
"fs-extra": "^8.1.0",
"gatsby-plugin-utils": "^0.2.27",
"lodash.merge": "^4.6.2",
"rss": "^1.2.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ exports[`Test plugin feed custom query runs 1`] = `"<?xml version=\\"1.0\\" enco
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>"`;
exports[`Test plugin feed options validation throws when invalid plugin options 1`] = `[Error: [Config Validation]: "output" is required]`;
exports[`Test plugin feed options validation throws when invalid plugin options 1`] = `[Error: [Config Validation]: "feeds[0].output" is required]`;
exports[`Test plugin feed options validation throws when invalid plugin options 2`] = `[Error: [Config Validation]: "query" is required]`;
exports[`Test plugin feed options validation throws when invalid plugin options 2`] = `[Error: [Config Validation]: "feeds[0].query" is required]`;
15 changes: 13 additions & 2 deletions packages/gatsby-plugin-feed/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs-extra"
import path from "path"
import RSS from "rss"
import merge from "lodash.merge"
import { Joi } from "gatsby-plugin-utils"

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

if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
exports.pluginOptionsSchema = pluginOptionsSchema
}

// TODO: remove in the next major release
// A default function to transform query data into feed entries.
const serialize = ({ query: { site, allMarkdownRemark } }) =>
Expand All @@ -35,9 +40,15 @@ exports.onPreBootstrap = async function onPreBootstrap(
delete pluginOptions.plugins

try {
const normalized = await pluginOptionsSchema.validate(pluginOptions)
// TODO: remove this once pluginOptionsSchema is stable
const { value: normalized, error } = await pluginOptionsSchema({
Joi,
}).validate(pluginOptions, {
externals: false,
})

if (error) throw error

// TODO: remove these checks in the next major release
if (!normalized.feeds) {
reporter.warn(
reporter.stripIndent(
Expand Down
61 changes: 46 additions & 15 deletions packages/gatsby-plugin-feed/src/plugin-options.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
import Joi from "@hapi/joi"
import { parse } from "gatsby/graphql"
import { stripIndent } from "common-tags"

// TODO: make serialize required in next major version bump
const feed = Joi.object({
output: Joi.string().required(),
query: Joi.string().required(),
title: Joi.string(),
serialize: Joi.func(),
match: Joi.string(),
link: Joi.string(),
}).unknown(true)
const feed = ({ Joi }) =>
Joi.object({
output: Joi.string().required(),
query: Joi.string().required(),
title: Joi.string(),
serialize: Joi.func(),
match: Joi.string(),
link: Joi.string(),
})
.unknown(true)
.external(({ query }) => {
if (query) {
try {
parse(query)
} catch (e) {
throw new Error(
stripIndent`
Invalid plugin options for "gatsby-plugin-feed":
"query" must be a valid GraphQL query. Received the error "${e.message}"`
)
}
}
})

// TODO: make feeds required in next major version bump
export default Joi.object({
generator: Joi.string(),
query: Joi.string(),
setup: Joi.func(),
feeds: Joi.array().items(feed),
}).unknown(true)
export default ({ Joi }) =>
Joi.object({
generator: Joi.string(),
query: Joi.string(),
setup: Joi.func(),
feeds: Joi.array().items(feed({ Joi })),
})
.unknown(true)
.external(({ query }) => {
if (query) {
try {
parse(query)
} catch (e) {
throw new Error(
stripIndent`
Invalid plugin options for "gatsby-plugin-feed":
"query" must be a valid GraphQL query. Received the error "${e.message}"`
)
}
}
})

0 comments on commit 705ff91

Please sign in to comment.