-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathjoi.ts
114 lines (109 loc) · 3.13 KB
/
joi.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import Joi from "@hapi/joi"
import { IGatsbyConfig, IGatsbyPage, IGatsbyNode } from "../redux/types"
const stripTrailingSlash = (chain: Joi.StringSchema): Joi.StringSchema =>
chain.replace(/(\w)\/+$/, `$1`)
// only add leading slash on relative urls
const addLeadingSlash = (chain: Joi.StringSchema): Joi.StringSchema =>
chain.when(Joi.string().uri({ relativeOnly: true }), {
then: chain.replace(/^([^/])/, `/$1`),
})
export const gatsbyConfigSchema: Joi.ObjectSchema<IGatsbyConfig> = Joi.object()
.keys({
__experimentalThemes: Joi.array(),
polyfill: Joi.boolean().default(true),
assetPrefix: stripTrailingSlash(
Joi.string().uri({
allowRelative: true,
})
),
pathPrefix: addLeadingSlash(
stripTrailingSlash(
Joi.string()
.uri({
allowRelative: true,
relativeOnly: true,
})
.default(``)
// removes single / value
.allow(``)
.replace(/^\/$/, ``)
)
),
linkPrefix: Joi.forbidden().error(
new Error(`"linkPrefix" should be changed to "pathPrefix"`)
),
siteMetadata: Joi.object({
siteUrl: stripTrailingSlash(Joi.string()).uri(),
}).unknown(),
mapping: Joi.object(),
plugins: Joi.array(),
proxy: Joi.array()
.items(
Joi.object().keys({
prefix: Joi.string().required(),
url: Joi.string().required(),
})
)
.single(),
developMiddleware: Joi.func(),
})
// throws when both assetPrefix and pathPrefix are defined
.when(
Joi.object({
assetPrefix: Joi.string().uri({
allowRelative: true,
relativeOnly: true,
}),
pathPrefix: Joi.string()
.uri({
allowRelative: true,
relativeOnly: true,
})
.default(``),
}),
{
then: Joi.object({
assetPrefix: Joi.string()
.uri({
allowRelative: false,
})
.error(
new Error(
`assetPrefix must be an absolute URI when used with pathPrefix`
)
),
}),
}
)
export const pageSchema: Joi.ObjectSchema<IGatsbyPage> = Joi.object()
.keys({
path: Joi.string().required(),
matchPath: Joi.string(),
component: Joi.string().required(),
componentChunkName: Joi.string().required(),
context: Joi.object(),
pluginCreator___NODE: Joi.string(),
pluginCreatorId: Joi.string(),
})
.unknown()
export const nodeSchema: Joi.ObjectSchema<IGatsbyNode> = Joi.object()
.keys({
id: Joi.string().required(),
children: Joi.array().items(Joi.string(), Joi.object().forbidden()),
parent: Joi.string().allow(null),
fields: Joi.object(),
internal: Joi.object()
.keys({
contentDigest: Joi.string().required(),
mediaType: Joi.string(),
type: Joi.string().required(),
owner: Joi.string().required(),
fieldOwners: Joi.object(),
content: Joi.string().allow(``),
description: Joi.string(),
ignoreType: Joi.boolean(),
counter: Joi.number(),
})
.unknown(false), // Don't allow non-standard fields
})
.unknown()