Skip to content

Commit d123ad0

Browse files
dario-piotrowiczOpenCode Claudevicb
authored
Error gracefully when encountering date values in TOML config files (#11651)
* fix: automatically convert TOML Date values in vars to strings TOML parses unquoted date values like DATE = 2024-01-01 as Date objects. Previously this caused a confusing error from Miniflare. Now wrangler automatically converts Date values to ISO date strings (YYYY-MM-DD format) and shows a warning suggesting to wrap the value in quotes. Co-authored-by: Dario Piotrowicz <dario@cloudflare.com> * error insted of trying to convert to string * Apply suggestions from code review Co-authored-by: Victor Berchet <victor@suumit.com> * Apply suggestions from code review Co-authored-by: Victor Berchet <victor@suumit.com> * Apply suggestions from code review * use hardcoded example * create toml dates using smol-toml in tests --------- Co-authored-by: OpenCode Claude <opencode-claude@anthropic.com> Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent eac5cf7 commit d123ad0

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

.changeset/floppy-tips-play.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Surface a more helpful error message for TOML Date, Date-Time, and Time values in `vars`
6+
7+
TOML parses unquoted date/time values like `DATE = 2024-01-01` as objects. Previously this would cause an unhelpful error message further down the stack. Now wrangler surfaces a more helpful error message earlier, telling you to quote the value as a string, e.g. `DATE = "2024-01-01"`.

.changeset/tangy-knives-allow.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/workers-utils": patch
3+
---
4+
5+
Surface error in diagnostics when TOML date/time values are used in `vars`
6+
7+
TOML parses unquoted date/time values like `DATE = 2024-01-01` as TOML Date, Date-Time, and Time values. The config validation now surfaces an error in the diagnostics result when this type of values are encountered, with a clear message telling you to quote the value as a string, e.g. `DATE = "2024-01-01"`.

packages/workers-utils/src/config/validation.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,9 +2069,9 @@ const validateVars =
20692069
let isValid = true;
20702070
const fieldPath =
20712071
config === undefined ? `${field}` : `env.${envName}.${field}`;
2072-
const configVars = Object.keys(config?.vars ?? {});
2073-
// If there are no top level vars then there is nothing to do here.
2074-
if (configVars.length > 0) {
2072+
2073+
// Validate var values if vars is defined
2074+
if (value !== undefined) {
20752075
if (typeof value !== "object" || value === null) {
20762076
diagnostics.errors.push(
20772077
`The field "${fieldPath}" should be an object but got ${JSON.stringify(
@@ -2080,6 +2080,23 @@ const validateVars =
20802080
);
20812081
isValid = false;
20822082
} else {
2083+
// Check each var value for invalid types (e.g., Date objects)
2084+
for (const [varName, varValue] of Object.entries(value)) {
2085+
if (varValue instanceof Date) {
2086+
diagnostics.errors.push(
2087+
`The field "${fieldPath}.${varName}" is a TOML date, which is not supported. ` +
2088+
`Please use a string instead, e.g. ${varName} = "2025-12-19".`
2089+
);
2090+
isValid = false;
2091+
}
2092+
}
2093+
}
2094+
}
2095+
2096+
// Check for vars that exist at top level but not in the current environment
2097+
const configVars = Object.keys(config?.vars ?? {});
2098+
if (configVars.length > 0) {
2099+
if (typeof value === "object" && value !== null) {
20832100
for (const varName of configVars) {
20842101
if (!(varName in value)) {
20852102
diagnostics.warnings.push(

packages/workers-utils/tests/config/validation/normalize-and-validate-config.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path";
2+
import TOML from "smol-toml";
23
import { describe, expect, it, test, vi } from "vitest";
34
import { normalizeAndValidateConfig } from "../../../src/config/validation";
45
import { normalizeString } from "../../../src/test-helpers";
@@ -5469,6 +5470,51 @@ describe("normalizeAndValidateConfig()", () => {
54695470
Please add \\"unsafe\\" to \\"env.ENV1\\"."
54705471
`);
54715472
});
5473+
5474+
it("should error on Date values in vars (parsed by TOML)", () => {
5475+
const rawConfig = TOML.parse(`
5476+
[vars]
5477+
VALID_VAR = "some string"
5478+
DATE_VAR = 2024-01-01
5479+
`) as unknown as RawConfig;
5480+
5481+
const { diagnostics } = normalizeAndValidateConfig(
5482+
rawConfig,
5483+
undefined,
5484+
undefined,
5485+
{ env: undefined }
5486+
);
5487+
5488+
expect(diagnostics.hasErrors()).toBe(true);
5489+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
5490+
"Processing wrangler configuration:
5491+
- The field \\"vars.DATE_VAR\\" is a TOML date, which is not supported. Please use a string instead, e.g. DATE_VAR = \\"2025-12-19\\"."
5492+
`);
5493+
});
5494+
5495+
it("should error on Date values in env vars (parsed by TOML)", () => {
5496+
const rawConfig = TOML.parse(`
5497+
[env.production.vars]
5498+
VALID_VAR = "some string"
5499+
RELEASE_DATE = 2025-06-15
5500+
`) as unknown as RawConfig;
5501+
5502+
const { diagnostics } = normalizeAndValidateConfig(
5503+
rawConfig,
5504+
undefined,
5505+
undefined,
5506+
{ env: "production" }
5507+
);
5508+
5509+
expect(diagnostics.hasErrors()).toBe(true);
5510+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
5511+
"Processing wrangler configuration:
5512+
5513+
- \\"env.production\\" environment configuration
5514+
- The field \\"env.production.vars.RELEASE_DATE\\" is a TOML date, which is not supported. Please use a string instead, e.g. RELEASE_DATE = \\"2025-12-19\\"."
5515+
`);
5516+
});
5517+
54725518
it("should error on node_compat", () => {
54735519
const { diagnostics } = normalizeAndValidateConfig(
54745520
// @ts-expect-error node_compat has been removed

0 commit comments

Comments
 (0)