Skip to content

Commit 9ede45b

Browse files
fix: relax validation of unsafe configuration to allow an empty object (#7461)
The types, the default and the code in general support an empty object for this config setting. So it makes sense to avoid erroring when validating the config.
1 parent 55ec38a commit 9ede45b

File tree

4 files changed

+58
-64
lines changed

4 files changed

+58
-64
lines changed

.changeset/metal-feet-carry.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: relax validation of unsafe configuration to allow an empty object
6+
7+
The types, the default and the code in general support an empty object for this config setting.
8+
9+
So it makes sense to avoid erroring when validating the config.

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

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,9 +3442,9 @@ describe("normalizeAndValidateConfig()", () => {
34423442
`);
34433443
});
34443444

3445-
it("should error if unsafe.bindings, unsafe.metadata and unsafe.capnp are undefined", () => {
3445+
it("should not error if unsafe is an empty object", () => {
34463446
const { diagnostics } = normalizeAndValidateConfig(
3447-
{ unsafe: {} } as unknown as RawConfig,
3447+
{ unsafe: {} } satisfies RawConfig,
34483448
undefined,
34493449
{ env: undefined }
34503450
);
@@ -3454,42 +3454,36 @@ describe("normalizeAndValidateConfig()", () => {
34543454
- \\"unsafe\\" fields are experimental and may change or break at any time."
34553455
`);
34563456
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3457-
"Processing wrangler configuration:
3458-
- The field \\"unsafe\\" should contain at least one of \\"bindings\\", \\"metadata\\" or \\"capnp\\" properties but got {}."
3459-
`);
3460-
});
3461-
3462-
it("should not error if at least unsafe.bindings is defined", () => {
3463-
const { diagnostics } = normalizeAndValidateConfig(
3464-
{ unsafe: { bindings: [] } } as unknown as RawConfig,
3465-
undefined,
3466-
{ env: undefined }
3467-
);
3468-
3469-
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3470-
"Processing wrangler configuration:
3471-
- \\"unsafe\\" fields are experimental and may change or break at any time."
3472-
`);
3473-
expect(diagnostics.hasErrors()).toBe(false);
3457+
"Processing wrangler configuration:
3458+
"
3459+
`);
34743460
});
34753461

3476-
it("should not error if at least unsafe.metadata is defined", () => {
3462+
it("should error if unsafe contains unexpected properties", () => {
34773463
const { diagnostics } = normalizeAndValidateConfig(
3478-
{ unsafe: { metadata: {} } } as unknown as RawConfig,
3464+
{
3465+
unsafe: {
3466+
invalid: true,
3467+
},
3468+
} as RawConfig,
34793469
undefined,
34803470
{ env: undefined }
34813471
);
34823472

34833473
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3484-
"Processing wrangler configuration:
3485-
- \\"unsafe\\" fields are experimental and may change or break at any time."
3486-
`);
3487-
expect(diagnostics.hasErrors()).toBe(false);
3474+
"Processing wrangler configuration:
3475+
- \\"unsafe\\" fields are experimental and may change or break at any time.
3476+
- Unexpected fields found in unsafe field: \\"invalid\\""
3477+
`);
3478+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3479+
"Processing wrangler configuration:
3480+
"
3481+
`);
34883482
});
34893483

34903484
it("should error if unsafe.bindings is an object", () => {
34913485
const { diagnostics } = normalizeAndValidateConfig(
3492-
{ unsafe: { bindings: {} } } as unknown as RawConfig,
3486+
{ unsafe: { bindings: {} } } as RawConfig,
34933487
undefined,
34943488
{ env: undefined }
34953489
);
@@ -5164,32 +5158,30 @@ describe("normalizeAndValidateConfig()", () => {
51645158
`);
51655159
});
51665160

5167-
it("should error if unsafe.bindings, unsafe.metadata and unsafe.capnp are undefined", () => {
5161+
it("should not error if unsafe is an empty object", () => {
51685162
const { diagnostics } = normalizeAndValidateConfig(
5169-
{ env: { ENV1: { unsafe: {} } } } as unknown as RawConfig,
5163+
{ env: { ENV1: { unsafe: {} } } } as RawConfig,
51705164
undefined,
51715165
{ env: "ENV1" }
51725166
);
51735167

51745168
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
5175-
"Processing wrangler configuration:
5169+
"Processing wrangler configuration:
51765170
5177-
- \\"env.ENV1\\" environment configuration
5178-
- \\"unsafe\\" fields are experimental and may change or break at any time."
5179-
`);
5171+
- \\"env.ENV1\\" environment configuration
5172+
- \\"unsafe\\" fields are experimental and may change or break at any time."
5173+
`);
51805174
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
5181-
"Processing wrangler configuration:
5182-
5183-
- \\"env.ENV1\\" environment configuration
5184-
- The field \\"env.ENV1.unsafe\\" should contain at least one of \\"bindings\\", \\"metadata\\" or \\"capnp\\" properties but got {}."
5185-
`);
5175+
"Processing wrangler configuration:
5176+
"
5177+
`);
51865178
});
51875179

51885180
it("should not error if at least unsafe.bindings is undefined", () => {
51895181
const { diagnostics } = normalizeAndValidateConfig(
51905182
{
51915183
env: { ENV1: { unsafe: { bindings: [] } } },
5192-
} as unknown as RawConfig,
5184+
} as RawConfig,
51935185
undefined,
51945186
{ env: "ENV1" }
51955187
);
@@ -5203,22 +5195,26 @@ describe("normalizeAndValidateConfig()", () => {
52035195
expect(diagnostics.hasErrors()).toBe(false);
52045196
});
52055197

5206-
it("should not error if at least unsafe.metadata is undefined", () => {
5198+
it("should error if unsafe contains unexpected properties", () => {
52075199
const { diagnostics } = normalizeAndValidateConfig(
52085200
{
5209-
env: { ENV1: { unsafe: { metadata: {} } } },
5210-
} as unknown as RawConfig,
5201+
env: { ENV1: { unsafe: { invalid: true } } },
5202+
} as RawConfig,
52115203
undefined,
52125204
{ env: "ENV1" }
52135205
);
52145206

52155207
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
5216-
"Processing wrangler configuration:
5208+
"Processing wrangler configuration:
52175209
5218-
- \\"env.ENV1\\" environment configuration
5219-
- \\"unsafe\\" fields are experimental and may change or break at any time."
5220-
`);
5221-
expect(diagnostics.hasErrors()).toBe(false);
5210+
- \\"env.ENV1\\" environment configuration
5211+
- \\"unsafe\\" fields are experimental and may change or break at any time.
5212+
- Unexpected fields found in unsafe field: \\"invalid\\""
5213+
`);
5214+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
5215+
"Processing wrangler configuration:
5216+
"
5217+
`);
52225218
});
52235219

52245220
it("should error if unsafe.bindings is an object", () => {

packages/wrangler/src/config/config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,7 @@ export const defaultWranglerConfig: Config = {
373373
cloudchamber: {},
374374
send_email: [],
375375
browser: undefined,
376-
unsafe: {
377-
bindings: undefined,
378-
metadata: undefined,
379-
},
376+
unsafe: {},
380377
mtls_certificates: [],
381378
tail_consumers: undefined,
382379
pipelines: [],

packages/wrangler/src/config/validation.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,20 +1875,6 @@ const validateUnsafeSettings =
18751875
return false;
18761876
}
18771877

1878-
// At least one of bindings and metadata must exist
1879-
if (
1880-
!hasProperty(value, "bindings") &&
1881-
!hasProperty(value, "metadata") &&
1882-
!hasProperty(value, "capnp")
1883-
) {
1884-
diagnostics.errors.push(
1885-
`The field "${fieldPath}" should contain at least one of "bindings", "metadata" or "capnp" properties but got ${JSON.stringify(
1886-
value
1887-
)}.`
1888-
);
1889-
return false;
1890-
}
1891-
18921878
// unsafe.bindings
18931879
if (hasProperty(value, "bindings") && value.bindings !== undefined) {
18941880
const validateBindingsFn = validateBindingsProperty(
@@ -1975,6 +1961,12 @@ const validateUnsafeSettings =
19751961
}
19761962
}
19771963

1964+
validateAdditionalProperties(diagnostics, field, Object.keys(value), [
1965+
"bindings",
1966+
"metadata",
1967+
"capnp",
1968+
]);
1969+
19781970
return true;
19791971
};
19801972

0 commit comments

Comments
 (0)