-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update plugin schema testing util and associated tests (#27574)
* Fix default * validate matching options, add associated test * fix error test * it's only the mismatch that matters * remove test, the utility can't handle external calls yet * use when instead of async * remove keys now that we're not using external * fix typescript * fix expected errors in tests * missed some tests
- Loading branch information
LB
authored
Oct 21, 2020
1 parent
dc0ce0c
commit 6d81283
Showing
11 changed files
with
94 additions
and
73 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
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
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
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
36 changes: 12 additions & 24 deletions
36
packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts
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,37 +1,25 @@ | ||
import { Joi } from "./joi" | ||
import { GatsbyNode } from "gatsby" | ||
import { validateOptionsSchema } from "./validate" | ||
import { IPluginInfoOptions } from "gatsby" | ||
|
||
interface ITestPluginOptionsSchemaReturnType { | ||
errors: Array<string> | ||
isValid: boolean | ||
} | ||
|
||
export function testPluginOptionsSchema<PluginOptions = object>( | ||
export async function testPluginOptionsSchema( | ||
pluginSchemaFunction: Exclude<GatsbyNode["pluginOptionsSchema"], undefined>, | ||
pluginOptions: PluginOptions | ||
): ITestPluginOptionsSchemaReturnType { | ||
const pluginOptionsNames = Object.keys(pluginOptions) | ||
pluginOptions: IPluginInfoOptions | ||
): Promise<ITestPluginOptionsSchemaReturnType> { | ||
const pluginSchema = pluginSchemaFunction({ Joi }) | ||
const errors: Array<string> = [] | ||
|
||
pluginOptionsNames.forEach(pluginOptionName => { | ||
const partialSchema = pluginSchema.extract(pluginOptionName) | ||
const { error } = partialSchema.validate(pluginOptions[pluginOptionName], { | ||
abortEarly: false, | ||
}) | ||
try { | ||
await validateOptionsSchema(pluginSchema, pluginOptions) | ||
} catch (e) { | ||
const errors = e.details.map(detail => detail.message) | ||
return { isValid: false, errors } | ||
} | ||
|
||
if (error) { | ||
const errorMessage = error.message | ||
|
||
// In the case of an array, "value" does not exist in the error message | ||
// and so we can't replace it with the plugin option name, we have to concat it | ||
const message = errorMessage.includes(`"value"`) | ||
? errorMessage.replace(`"value"`, `"${pluginOptionName}"`) | ||
: `"${pluginOptionName}" ${errorMessage}` | ||
|
||
errors.push(message) | ||
} | ||
}) | ||
|
||
return { isValid: errors.length === 0, errors } | ||
return { isValid: true, errors: [] } | ||
} |
Oops, something went wrong.