Skip to content

Commit 08e3a49

Browse files
fix: warn on unexpected fields on migrations (#1166)
This adds a warning for unexpected fields on `[migrations]` config, reported in #1165. It also adds a test for incorrect `renamed_classes` in a migration.
1 parent de4e3c2 commit 08e3a49

File tree

3 files changed

+124
-66
lines changed

3 files changed

+124
-66
lines changed

.changeset/heavy-monkeys-relax.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: warn on unexpected fields on migrations
6+
7+
This adds a warning for unexpected fields on `[migrations]` config, reported in https://github.com/cloudflare/wrangler2/issues/1165. It also adds a test for incorrect `renamed_classes` in a migration.

packages/wrangler/src/__tests__/configuration.test.ts

+99-57
Original file line numberDiff line numberDiff line change
@@ -165,68 +165,110 @@ describe("normalizeAndValidateConfig()", () => {
165165
`);
166166
});
167167

168-
it("should override `migrations` config defaults with provided values", () => {
169-
const expectedConfig: RawConfig = {
170-
migrations: [
171-
{
172-
tag: "TAG",
173-
new_classes: ["CLASS_1", "CLASS_2"],
174-
renamed_classes: [
175-
{
176-
from: "FROM_CLASS",
177-
to: "TO_CLASS",
178-
},
179-
],
180-
deleted_classes: ["CLASS_3", "CLASS_4"],
181-
},
182-
],
183-
};
168+
describe("migrations", () => {
169+
it("should override `migrations` config defaults with provided values", () => {
170+
const expectedConfig: RawConfig = {
171+
migrations: [
172+
{
173+
tag: "TAG",
174+
new_classes: ["CLASS_1", "CLASS_2"],
175+
renamed_classes: [
176+
{
177+
from: "FROM_CLASS",
178+
to: "TO_CLASS",
179+
},
180+
],
181+
deleted_classes: ["CLASS_3", "CLASS_4"],
182+
},
183+
],
184+
};
184185

185-
const { config, diagnostics } = normalizeAndValidateConfig(
186-
expectedConfig,
187-
undefined,
188-
{ env: undefined }
189-
);
186+
const { config, diagnostics } = normalizeAndValidateConfig(
187+
expectedConfig,
188+
undefined,
189+
{ env: undefined }
190+
);
190191

191-
expect(config).toEqual(expect.objectContaining(expectedConfig));
192-
expect(diagnostics.hasErrors()).toBe(false);
193-
expect(diagnostics.hasWarnings()).toBe(false);
194-
});
192+
expect(config).toEqual(expect.objectContaining(expectedConfig));
193+
expect(diagnostics.hasErrors()).toBe(false);
194+
expect(diagnostics.hasWarnings()).toBe(false);
195+
});
195196

196-
it("should error on invalid `migrations` values", () => {
197-
const expectedConfig = {
198-
migrations: [
199-
{
200-
tag: 111,
201-
new_classes: [222, 333],
202-
renamed_classes: [
203-
{
204-
from: 444,
205-
to: 555,
206-
},
207-
],
208-
deleted_classes: [666, 777],
209-
},
210-
],
211-
};
197+
it("should error on invalid `migrations` values", () => {
198+
const expectedConfig = {
199+
migrations: [
200+
{
201+
tag: 111,
202+
new_classes: [222, 333],
203+
renamed_classes: [
204+
{
205+
from: 444,
206+
to: 555,
207+
},
208+
],
209+
deleted_classes: [666, 777],
210+
},
211+
],
212+
};
212213

213-
const { config, diagnostics } = normalizeAndValidateConfig(
214-
expectedConfig as unknown as RawConfig,
215-
undefined,
216-
{ env: undefined }
217-
);
214+
const { config, diagnostics } = normalizeAndValidateConfig(
215+
expectedConfig as unknown as RawConfig,
216+
undefined,
217+
{ env: undefined }
218+
);
218219

219-
expect(config).toEqual(expect.objectContaining(expectedConfig));
220-
expect(diagnostics.hasWarnings()).toBe(false);
221-
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
222-
"Processing wrangler configuration:
223-
- Expected \\"migrations[0].tag\\" to be of type string but got 111.
224-
- Expected \\"migrations[0].new_classes.[0]\\" to be of type string but got 222.
225-
- Expected \\"migrations[0].new_classes.[1]\\" to be of type string but got 333.
226-
- Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
227-
- Expected \\"migrations[0].deleted_classes.[0]\\" to be of type string but got 666.
228-
- Expected \\"migrations[0].deleted_classes.[1]\\" to be of type string but got 777."
229-
`);
220+
expect(config).toEqual(expect.objectContaining(expectedConfig));
221+
expect(diagnostics.hasWarnings()).toBe(false);
222+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
223+
"Processing wrangler configuration:
224+
- Expected \\"migrations[0].tag\\" to be of type string but got 111.
225+
- Expected \\"migrations[0].new_classes.[0]\\" to be of type string but got 222.
226+
- Expected \\"migrations[0].new_classes.[1]\\" to be of type string but got 333.
227+
- Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
228+
- Expected \\"migrations[0].deleted_classes.[0]\\" to be of type string but got 666.
229+
- Expected \\"migrations[0].deleted_classes.[1]\\" to be of type string but got 777."
230+
`);
231+
});
232+
233+
it("should warn/error on unexpected fields on `migrations`", async () => {
234+
const expectedConfig = {
235+
migrations: [
236+
{
237+
tag: "TAG",
238+
new_classes: ["CLASS_1", "CLASS_2"],
239+
renamed_classes: [
240+
{
241+
from: "FROM_CLASS",
242+
to: "TO_CLASS",
243+
},
244+
{
245+
a: "something",
246+
b: "someone",
247+
},
248+
],
249+
deleted_classes: ["CLASS_3", "CLASS_4"],
250+
unrecognized_field: "FOO",
251+
},
252+
],
253+
};
254+
255+
const { config, diagnostics } = normalizeAndValidateConfig(
256+
expectedConfig as unknown as RawConfig,
257+
undefined,
258+
{ env: undefined }
259+
);
260+
261+
expect(config).toEqual(expect.objectContaining(expectedConfig));
262+
expect(diagnostics.hasErrors()).toBe(true);
263+
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
264+
"Processing wrangler configuration:
265+
- Unexpected fields found in migrations field: \\"unrecognized_field\\""
266+
`);
267+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
268+
"Processing wrangler configuration:
269+
- Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":\\"FROM_CLASS\\",\\"to\\":\\"TO_CLASS\\"},{\\"a\\":\\"something\\",\\"b\\":\\"someone\\"}]."
270+
`);
271+
});
230272
});
231273

232274
describe("site", () => {

packages/wrangler/src/config/validation.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -384,29 +384,38 @@ function normalizeAndValidateMigrations(
384384
return [];
385385
} else {
386386
for (let i = 0; i < rawMigrations.length; i++) {
387-
const migration = rawMigrations[i];
387+
const { tag, new_classes, renamed_classes, deleted_classes, ...rest } =
388+
rawMigrations[i];
389+
390+
validateAdditionalProperties(
391+
diagnostics,
392+
"migrations",
393+
Object.keys(rest),
394+
[]
395+
);
396+
388397
validateRequiredProperty(
389398
diagnostics,
390399
`migrations[${i}]`,
391400
`tag`,
392-
migration.tag,
401+
tag,
393402
"string"
394403
);
395404
validateOptionalTypedArray(
396405
diagnostics,
397406
`migrations[${i}].new_classes`,
398-
migration.new_classes,
407+
new_classes,
399408
"string"
400409
);
401-
if (migration.renamed_classes !== undefined) {
402-
if (!Array.isArray(migration.renamed_classes)) {
410+
if (renamed_classes !== undefined) {
411+
if (!Array.isArray(renamed_classes)) {
403412
diagnostics.errors.push(
404413
`Expected "migrations[${i}].renamed_classes" to be an array of "{from: string, to: string}" objects but got ${JSON.stringify(
405-
migration.renamed_classes
414+
renamed_classes
406415
)}.`
407416
);
408417
} else if (
409-
migration.renamed_classes.some(
418+
renamed_classes.some(
410419
(c) =>
411420
typeof c !== "object" ||
412421
!isRequiredProperty(c, "from", "string") ||
@@ -415,15 +424,15 @@ function normalizeAndValidateMigrations(
415424
) {
416425
diagnostics.errors.push(
417426
`Expected "migrations[${i}].renamed_classes" to be an array of "{from: string, to: string}" objects but got ${JSON.stringify(
418-
migration.renamed_classes
427+
renamed_classes
419428
)}.`
420429
);
421430
}
422431
}
423432
validateOptionalTypedArray(
424433
diagnostics,
425434
`migrations[${i}].deleted_classes`,
426-
migration.deleted_classes,
435+
deleted_classes,
427436
"string"
428437
);
429438
}

0 commit comments

Comments
 (0)