Skip to content

Commit 86b8b26

Browse files
authored
chore(gatsby): move to latest joi (#29792)
1 parent 2758329 commit 86b8b26

File tree

13 files changed

+106
-78
lines changed

13 files changed

+106
-78
lines changed

integration-tests/structured-logging/__tests__/to-do.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ const commonAssertions = events => {
105105
const actionSchema = joi.alternatives().try(
106106
joi
107107
.object({
108-
type: joi.string().required().valid([`SET_STATUS`]),
108+
type: joi.string().required().valid(`SET_STATUS`),
109109
// TODO: We should change this to always be an Object I think pieh
110110
payload: joi
111111
.string()
112112
.required()
113-
.valid([`SUCCESS`, `IN_PROGRESS`, `FAILED`, `INTERRUPTED`]),
113+
.valid(`SUCCESS`, `IN_PROGRESS`, `FAILED`, `INTERRUPTED`),
114114
// Should this be here or one level up?
115115
timestamp: joi.string().required(),
116116
})
@@ -121,12 +121,7 @@ const commonAssertions = events => {
121121
type: joi
122122
.string()
123123
.required()
124-
.valid([
125-
`ACTIVITY_START`,
126-
`ACTIVITY_UPDATE`,
127-
`ACTIVITY_END`,
128-
`LOG`,
129-
]),
124+
.valid(`ACTIVITY_START`, `ACTIVITY_UPDATE`, `ACTIVITY_END`, `LOG`),
130125
payload: joi.object(),
131126
// Should this be here or one level up?
132127
timestamp: joi.string().required(),
@@ -135,7 +130,7 @@ const commonAssertions = events => {
135130
)
136131

137132
const eventSchema = joi.object({
138-
type: joi.string().required().valid([`LOG_ACTION`]),
133+
type: joi.string().required().valid(`LOG_ACTION`),
139134
action: actionSchema,
140135
})
141136
events.forEach(event => {

integration-tests/structured-logging/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"fs-extra": "^9.0.1",
2121
"jest": "^24.0.0",
2222
"jest-cli": "^24.0.0",
23-
"joi": "^14.3.1",
23+
"joi": "^17.4.0",
2424
"lodash": "^4.17.20",
2525
"node-fetch": "^2.6.1"
2626
}

packages/gatsby-cli/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
},
1212
"dependencies": {
1313
"@babel/code-frame": "^7.10.4",
14-
"@hapi/joi": "^15.1.1",
1514
"@types/common-tags": "^1.8.0",
1615
"better-opn": "^2.0.0",
1716
"chalk": "^4.1.0",
@@ -29,6 +28,7 @@
2928
"gatsby-telemetry": "^2.1.0-next.1",
3029
"hosted-git-info": "^3.0.6",
3130
"is-valid-path": "^0.1.1",
31+
"joi": "^17.4.0",
3232
"lodash": "^4.17.20",
3333
"meant": "^1.0.2",
3434
"node-fetch": "^2.6.1",
@@ -101,4 +101,4 @@
101101
"engines": {
102102
"node": ">=12.13.0"
103103
}
104-
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import { errorSchema } from "../error-schema"
22

3-
test(`throws invalid on an invalid error`, () => {
4-
expect(errorSchema.validate({ lol: `true` })).rejects.toBeDefined()
3+
test(`returns invalid on an invalid error`, () => {
4+
expect(errorSchema.validate({ lol: `true` })).toMatchInlineSnapshot(`
5+
Object {
6+
"error": [ValidationError: "lol" is not allowed],
7+
"value": Object {
8+
"lol": "true",
9+
},
10+
}
11+
`)
512
})
613

7-
test(`does not throw on a valid schema`, () => {
14+
test(`returns a valid value`, () => {
815
expect(
916
errorSchema.validate({
1017
context: {},
1118
})
12-
).resolves.toEqual(expect.any(Object))
19+
).toMatchInlineSnapshot(`
20+
Object {
21+
"value": Object {
22+
"context": Object {},
23+
},
24+
}
25+
`)
1326
})

packages/gatsby-cli/src/structured-errors/construct-error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const constructError = (
3838

3939
// validate
4040
const { error } = errorSchema.validate(structuredError)
41-
if (error !== null) {
41+
if (error) {
4242
console.log(`Failed to validate error`, error)
4343
process.exit(1)
4444
}

packages/gatsby-cli/src/structured-errors/error-schema.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Joi from "@hapi/joi"
1+
import Joi from "joi"
22
import { ILocationPosition, IStructuredError } from "./types"
33

44
export const Position: Joi.ObjectSchema<ILocationPosition> = Joi.object().keys({
@@ -20,9 +20,9 @@ export const errorSchema: Joi.ObjectSchema<IStructuredError> = Joi.object().keys
2020
})
2121
)
2222
.allow(null),
23-
category: Joi.string().valid([`USER`, `SYSTEM`, `THIRD_PARTY`]),
24-
level: Joi.string().valid([`ERROR`, `WARNING`, `INFO`, `DEBUG`]),
25-
type: Joi.string().valid([`GRAPHQL`, `CONFIG`, `WEBPACK`, `PLUGIN`]),
23+
category: Joi.string().valid(`USER`, `SYSTEM`, `THIRD_PARTY`),
24+
level: Joi.string().valid(`ERROR`, `WARNING`, `INFO`, `DEBUG`),
25+
type: Joi.string().valid(`GRAPHQL`, `CONFIG`, `WEBPACK`, `PLUGIN`),
2626
filePath: Joi.string(),
2727
location: Joi.object({
2828
start: Position.required(),

packages/gatsby/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@babel/types": "^7.12.6",
2020
"@gatsbyjs/reach-router": "^1.3.5",
2121
"@gatsbyjs/webpack-hot-middleware": "^2.25.2",
22-
"@hapi/joi": "^15.1.1",
2322
"@mikaelkristiansson/domready": "^1.0.10",
2423
"@nodelib/fs.walk": "^1.2.4",
2524
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
@@ -170,7 +169,6 @@
170169
"@babel/cli": "^7.12.1",
171170
"@babel/runtime": "^7.12.5",
172171
"@types/eslint": "^7.2.6",
173-
"@types/hapi__joi": "^16.0.12",
174172
"@types/micromatch": "^4.0.1",
175173
"@types/normalize-path": "^3.0.0",
176174
"@types/reach__router": "^1.3.5",

packages/gatsby/src/joi-schemas/__tests__/__snapshots__/joi.ts.snap

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,116 @@
11
import { gatsbyConfigSchema, nodeSchema } from "../joi"
22

33
describe(`gatsby config`, () => {
4-
it(`returns empty pathPrefix when not set`, async () => {
4+
it(`returns empty pathPrefix when not set`, () => {
55
const config = {}
66

7-
const result = await gatsbyConfigSchema.validate(config)
8-
expect(result).toEqual(
7+
const result = gatsbyConfigSchema.validate(config)
8+
expect(result.value).toEqual(
99
expect.objectContaining({
1010
pathPrefix: ``,
1111
})
1212
)
1313
})
1414

15-
it(`strips trailing slashes from url fields`, async () => {
15+
it(`throws when linkPrefix is set`, () => {
16+
const config = {
17+
linkPrefix: `/blog/`,
18+
}
19+
20+
const result = gatsbyConfigSchema.validate(config)
21+
expect(result.error).toMatchInlineSnapshot(
22+
`[Error: "linkPrefix" should be changed to "pathPrefix"]`
23+
)
24+
})
25+
26+
it(`strips trailing slashes from url fields`, () => {
1627
const config = {
1728
pathPrefix: `/blog///`,
1829
assetPrefix: `https://cdn.example.com/`,
1930
}
2031

21-
const result = await gatsbyConfigSchema.validate(config)
22-
expect(result).toEqual(
32+
const result = gatsbyConfigSchema.validate(config)
33+
expect(result.value).toEqual(
2334
expect.objectContaining({
2435
pathPrefix: `/blog`,
2536
assetPrefix: `https://cdn.example.com`,
2637
})
2738
)
2839
})
2940

30-
it(`allows assetPrefix to be full URL`, async () => {
41+
it(`allows assetPrefix to be full URL`, () => {
3142
const config = {
3243
assetPrefix: `https://cdn.example.com/`,
3344
}
3445

35-
const result = await gatsbyConfigSchema.validate(config)
36-
expect(result).toEqual(
46+
const result = gatsbyConfigSchema.validate(config)
47+
expect(result.value).toEqual(
3748
expect.objectContaining({
3849
assetPrefix: `https://cdn.example.com`,
3950
})
4051
)
4152
})
4253

43-
it(`allows assetPrefix to be a URL with nested paths`, async () => {
54+
it(`allows assetPrefix to be a URL with nested paths`, () => {
4455
const config = {
4556
assetPrefix: `https://cdn.example.com/some/nested/path`,
4657
}
4758

48-
const result = await gatsbyConfigSchema.validate(config)
49-
expect(result).toEqual(expect.objectContaining(config))
59+
const result = gatsbyConfigSchema.validate(config)
60+
expect(result.value).toEqual(expect.objectContaining(config))
5061
})
5162

52-
it(`allows relative paths for url fields`, async () => {
63+
it(`allows relative paths for url fields`, () => {
5364
const config = {
5465
pathPrefix: `/blog`,
5566
assetPrefix: `https://cdn.example.com`,
5667
}
5768

58-
const result = await gatsbyConfigSchema.validate(config)
59-
expect(result).toEqual(expect.objectContaining(config))
69+
const result = gatsbyConfigSchema.validate(config)
70+
expect(result.value).toEqual(expect.objectContaining(config))
6071
})
6172

62-
it(`strips trailing slash and add leading slash to pathPrefix`, async () => {
73+
it(`strips trailing slash and add leading slash to pathPrefix`, () => {
6374
const config = {
6475
pathPrefix: `blog/`,
6576
assetPrefix: `https://cdn.example.com/`,
6677
}
6778

68-
const result = await gatsbyConfigSchema.validate(config)
69-
expect(result).toEqual(
79+
const result = gatsbyConfigSchema.validate(config)
80+
expect(result.value).toEqual(
7081
expect.objectContaining({
7182
pathPrefix: `/blog`,
7283
assetPrefix: `https://cdn.example.com`,
7384
})
7485
)
7586
})
7687

77-
it(`does not allow pathPrefix to be full URL`, async () => {
78-
expect.assertions(1)
88+
it(`does not allow pathPrefix to be full URL`, () => {
7989
const config = {
8090
pathPrefix: `https://google.com`,
8191
}
8292

83-
try {
84-
await gatsbyConfigSchema.validate(config)
85-
} catch (err) {
86-
expect(err.message).toMatchSnapshot()
87-
}
93+
const result = gatsbyConfigSchema.validate(config)
94+
expect(result.error).toMatchInlineSnapshot(
95+
`[ValidationError: "pathPrefix" must be a valid relative uri]`
96+
)
8897
})
8998

90-
it(`throws when relative path used for both assetPrefix and pathPrefix`, async () => {
91-
expect.assertions(1)
99+
it(`throws when relative path used for both assetPrefix and pathPrefix`, () => {
92100
const config = {
93101
assetPrefix: `/assets`,
94102
pathPrefix: `/blog`,
95103
}
96104

97-
try {
98-
await gatsbyConfigSchema.validate(config)
99-
} catch (err) {
100-
expect(err.message).toMatchSnapshot()
101-
}
105+
const result = gatsbyConfigSchema.validate(config)
106+
expect(result.error).toMatchInlineSnapshot(
107+
`[Error: assetPrefix must be an absolute URI when used with pathPrefix]`
108+
)
102109
})
103110
})
104111

105112
describe(`node schema`, () => {
106-
it(`allows correct nodes`, async () => {
113+
it(`allows correct nodes`, () => {
107114
const node = {
108115
id: `foo`,
109116
internal: {
@@ -119,7 +126,7 @@ describe(`node schema`, () => {
119126
expect(error).toBeFalsy()
120127
})
121128

122-
it(`force id type`, async () => {
129+
it(`force id type`, () => {
123130
const node = {
124131
id: 5,
125132
internal: {
@@ -134,10 +141,10 @@ describe(`node schema`, () => {
134141
const { error } = nodeSchema.validate(node)
135142
expect(error).toBeTruthy()
136143
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
137-
expect(error!.message).toMatchSnapshot()
144+
expect(error!.message).toMatchInlineSnapshot(`"\\"id\\" must be a string"`)
138145
})
139146

140-
it(`doesn't allow unknown internal fields`, async () => {
147+
it(`doesn't allow unknown internal fields`, () => {
141148
const node = {
142149
id: `foo`,
143150
internal: {
@@ -154,6 +161,8 @@ describe(`node schema`, () => {
154161
const { error } = nodeSchema.validate(node)
155162
expect(error).toBeTruthy()
156163
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
157-
expect(error!.message).toMatchSnapshot()
164+
expect(error!.message).toMatchInlineSnapshot(
165+
`"\\"internal.customField\\" is not allowed"`
166+
)
158167
})
159168
})

packages/gatsby/src/joi-schemas/joi.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Joi from "@hapi/joi"
1+
import Joi from "joi"
22
import { IGatsbyConfig, IGatsbyPage, IGatsbyNode } from "../redux/types"
33

44
const stripTrailingSlash = (chain: Joi.StringSchema): Joi.StringSchema =>

packages/gatsby/src/redux/actions/internal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export const setSiteConfig = (config?: unknown): ISetSiteConfig => {
291291

292292
if (result.error) {
293293
const hasUnknownKeys = result.error.details.filter(
294-
details => details.type === `object.allowUnknown`
294+
details => details.type === `object.unknown`
295295
)
296296

297297
if (Array.isArray(hasUnknownKeys) && hasUnknownKeys.length) {

packages/gatsby/src/redux/actions/public.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// @flow
2-
const Joi = require(`@hapi/joi`)
32
const chalk = require(`chalk`)
43
const _ = require(`lodash`)
54
const { stripIndent } = require(`common-tags`)
@@ -647,7 +646,7 @@ const createNode = (
647646

648647
trackCli(`CREATE_NODE`, trackParams, { debounce: true })
649648

650-
const result = Joi.validate(node, nodeSchema)
649+
const result = nodeSchema.validate(node)
651650
if (result.error) {
652651
if (!hasErroredBecauseOfNodeValidation.has(result.error.message)) {
653652
const errorObj = {

0 commit comments

Comments
 (0)