Skip to content

Commit

Permalink
fix: relax validation of unsafe configuration to allow an empty object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
petebacondarwin committed Dec 5, 2024
1 parent 8c873ed commit 28379b2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 64 deletions.
9 changes: 9 additions & 0 deletions .changeset/metal-feet-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: relax validation of unsafe configuration to allow an empty object

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.
88 changes: 42 additions & 46 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3442,9 +3442,9 @@ describe("normalizeAndValidateConfig()", () => {
`);
});

it("should error if unsafe.bindings, unsafe.metadata and unsafe.capnp are undefined", () => {
it("should not error if unsafe is an empty object", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ unsafe: {} } as unknown as RawConfig,
{ unsafe: {} } satisfies RawConfig,
undefined,
{ env: undefined }
);
Expand All @@ -3454,42 +3454,36 @@ describe("normalizeAndValidateConfig()", () => {
- \\"unsafe\\" fields are experimental and may change or break at any time."
`);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- The field \\"unsafe\\" should contain at least one of \\"bindings\\", \\"metadata\\" or \\"capnp\\" properties but got {}."
`);
});

it("should not error if at least unsafe.bindings is defined", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ unsafe: { bindings: [] } } as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"unsafe\\" fields are experimental and may change or break at any time."
`);
expect(diagnostics.hasErrors()).toBe(false);
"Processing wrangler configuration:
"
`);
});

it("should not error if at least unsafe.metadata is defined", () => {
it("should error if unsafe contains unexpected properties", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ unsafe: { metadata: {} } } as unknown as RawConfig,
{
unsafe: {
invalid: true,
},
} as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"unsafe\\" fields are experimental and may change or break at any time."
`);
expect(diagnostics.hasErrors()).toBe(false);
"Processing wrangler configuration:
- \\"unsafe\\" fields are experimental and may change or break at any time.
- Unexpected fields found in unsafe field: \\"invalid\\""
`);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
"
`);
});

it("should error if unsafe.bindings is an object", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ unsafe: { bindings: {} } } as unknown as RawConfig,
{ unsafe: { bindings: {} } } as RawConfig,
undefined,
{ env: undefined }
);
Expand Down Expand Up @@ -5164,32 +5158,30 @@ describe("normalizeAndValidateConfig()", () => {
`);
});

it("should error if unsafe.bindings, unsafe.metadata and unsafe.capnp are undefined", () => {
it("should not error if unsafe is an empty object", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ env: { ENV1: { unsafe: {} } } } as unknown as RawConfig,
{ env: { ENV1: { unsafe: {} } } } as RawConfig,
undefined,
{ env: "ENV1" }
);

expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
"Processing wrangler configuration:
- \\"env.ENV1\\" environment configuration
- \\"unsafe\\" fields are experimental and may change or break at any time."
`);
- \\"env.ENV1\\" environment configuration
- \\"unsafe\\" fields are experimental and may change or break at any time."
`);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"env.ENV1\\" environment configuration
- The field \\"env.ENV1.unsafe\\" should contain at least one of \\"bindings\\", \\"metadata\\" or \\"capnp\\" properties but got {}."
`);
"Processing wrangler configuration:
"
`);
});

it("should not error if at least unsafe.bindings is undefined", () => {
const { diagnostics } = normalizeAndValidateConfig(
{
env: { ENV1: { unsafe: { bindings: [] } } },
} as unknown as RawConfig,
} as RawConfig,
undefined,
{ env: "ENV1" }
);
Expand All @@ -5203,22 +5195,26 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasErrors()).toBe(false);
});

it("should not error if at least unsafe.metadata is undefined", () => {
it("should error if unsafe contains unexpected properties", () => {
const { diagnostics } = normalizeAndValidateConfig(
{
env: { ENV1: { unsafe: { metadata: {} } } },
} as unknown as RawConfig,
env: { ENV1: { unsafe: { invalid: true } } },
} as RawConfig,
undefined,
{ env: "ENV1" }
);

expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
"Processing wrangler configuration:
- \\"env.ENV1\\" environment configuration
- \\"unsafe\\" fields are experimental and may change or break at any time."
`);
expect(diagnostics.hasErrors()).toBe(false);
- \\"env.ENV1\\" environment configuration
- \\"unsafe\\" fields are experimental and may change or break at any time.
- Unexpected fields found in unsafe field: \\"invalid\\""
`);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
"
`);
});

it("should error if unsafe.bindings is an object", () => {
Expand Down
5 changes: 1 addition & 4 deletions packages/wrangler/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,7 @@ export const defaultWranglerConfig: Config = {
cloudchamber: {},
send_email: [],
browser: undefined,
unsafe: {
bindings: undefined,
metadata: undefined,
},
unsafe: {},
mtls_certificates: [],
tail_consumers: undefined,
pipelines: [],
Expand Down
20 changes: 6 additions & 14 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1875,20 +1875,6 @@ const validateUnsafeSettings =
return false;
}

// At least one of bindings and metadata must exist
if (
!hasProperty(value, "bindings") &&
!hasProperty(value, "metadata") &&
!hasProperty(value, "capnp")
) {
diagnostics.errors.push(
`The field "${fieldPath}" should contain at least one of "bindings", "metadata" or "capnp" properties but got ${JSON.stringify(
value
)}.`
);
return false;
}

// unsafe.bindings
if (hasProperty(value, "bindings") && value.bindings !== undefined) {
const validateBindingsFn = validateBindingsProperty(
Expand Down Expand Up @@ -1975,6 +1961,12 @@ const validateUnsafeSettings =
}
}

validateAdditionalProperties(diagnostics, field, Object.keys(value), [
"bindings",
"metadata",
"capnp",
]);

return true;
};

Expand Down

0 comments on commit 28379b2

Please sign in to comment.