forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.ts
49 lines (43 loc) 路 1.19 KB
/
validate.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
import { ValidationOptions } from "joi"
import { ObjectSchema, Joi } from "./joi"
import { IPluginInfoOptions } from "./types"
const validationOptions: ValidationOptions = {
// Show all errors at once, rather than only the first one every time
abortEarly: false,
cache: true,
}
interface IOptions {
validateExternalRules?: boolean
returnWarnings?: boolean
}
interface IValidateAsyncResult {
value: IPluginInfoOptions
warning: {
message: string
details: Array<{
message: string
path: Array<string>
type: string
context: Array<Record<string, unknown>>
}>
}
}
export async function validateOptionsSchema(
pluginSchema: ObjectSchema,
pluginOptions: IPluginInfoOptions,
options: IOptions = {
validateExternalRules: true,
returnWarnings: true,
}
): Promise<IValidateAsyncResult> {
const { validateExternalRules, returnWarnings } = options
const warnOnUnknownSchema = pluginSchema.pattern(
/.*/,
Joi.any().warning(`any.unknown`)
)
return (await warnOnUnknownSchema.validateAsync(pluginOptions, {
...validationOptions,
externals: validateExternalRules,
warnings: returnWarnings,
})) as Promise<IValidateAsyncResult>
}