-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
test-plugin-options-schema.ts
53 lines (48 loc) · 1.44 KB
/
test-plugin-options-schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Joi } from "./joi"
import { GatsbyNode } from "gatsby"
import { validateOptionsSchema } from "./validate"
import { IPluginInfoOptions } from "./types"
interface ITestPluginOptionsSchemaReturnType {
errors: Array<string>
isValid: boolean
}
export async function testPluginOptionsSchema(
pluginSchemaFunction: Exclude<GatsbyNode["pluginOptionsSchema"], undefined>,
pluginOptions: IPluginInfoOptions
): Promise<ITestPluginOptionsSchemaReturnType> {
const pluginSchema = pluginSchemaFunction({
Joi: Joi.extend(joi => {
return {
base: joi.any(),
type: `subPlugins`,
args: (): any =>
joi
.array()
.items(
joi
.alternatives(
joi.string(),
joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.custom(value => {
if (typeof value === `string`) {
value = { resolve: value }
}
return value
}, `Gatsby specific subplugin validation`)
)
.default([]),
}
}),
})
try {
await validateOptionsSchema(pluginSchema, pluginOptions)
} catch (e) {
const errors = e.details.map(detail => detail.message)
return { isValid: false, errors }
}
return { isValid: true, errors: [] }
}