forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-plugin-options-schema.ts
67 lines (60 loc) 路 1.77 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Joi } from "./joi"
import { GatsbyNode } from "gatsby"
import { validateOptionsSchema } from "./validate"
import { IPluginInfoOptions } from "./types"
interface ITestPluginOptionsSchemaReturnType {
errors: Array<string>
warnings: Array<string>
isValid: boolean
hasWarnings: boolean
}
export async function testPluginOptionsSchema(
pluginSchemaFunction: Exclude<GatsbyNode["pluginOptionsSchema"], undefined>,
pluginOptions: IPluginInfoOptions
): Promise<ITestPluginOptionsSchemaReturnType> {
const pluginSchema = pluginSchemaFunction({
Joi: Joi.extend(joi => {
return {
type: `subPlugins`,
base: joi
.array()
.items(
joi.alternatives(
joi.string(),
joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
)
.custom(
arrayValue =>
arrayValue.map(value => {
if (typeof value === `string`) {
value = { resolve: value }
}
return value
}),
`Gatsby specific subplugin validation`
)
.default([]),
}
}),
})
try {
const { warning } = await validateOptionsSchema(pluginSchema, pluginOptions)
const warnings = warning?.details?.map(detail => detail.message) ?? []
if (warnings?.length > 0) {
return {
isValid: true,
errors: [],
hasWarnings: true,
warnings,
}
}
} catch (e) {
const errors = e?.details?.map(detail => detail.message) ?? []
return { isValid: false, errors, hasWarnings: false, warnings: [] }
}
return { isValid: true, errors: [], hasWarnings: false, warnings: [] }
}