diff --git a/.changeset/fifty-dots-end.md b/.changeset/fifty-dots-end.md new file mode 100644 index 000000000000..90b73f37d73c --- /dev/null +++ b/.changeset/fifty-dots-end.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +feat: More helpful error messages when validating compatibility date diff --git a/packages/wrangler/src/__tests__/configuration.test.ts b/packages/wrangler/src/__tests__/configuration.test.ts index 07709626448f..932d74793105 100644 --- a/packages/wrangler/src/__tests__/configuration.test.ts +++ b/packages/wrangler/src/__tests__/configuration.test.ts @@ -280,6 +280,83 @@ describe("normalizeAndValidateConfig()", () => { `); }); + describe("compatibility_date", () => { + it("should allow valid values", () => { + const expectedConfig: RawConfig = { + compatibility_date: "2024-10-01", + }; + + const { config, diagnostics } = normalizeAndValidateConfig( + expectedConfig, + undefined, + { env: undefined } + ); + + expect(config).toEqual(expect.objectContaining(expectedConfig)); + expect(diagnostics.hasWarnings()).toBe(false); + expect(diagnostics.hasErrors()).toBe(false); + }); + + it("should error for en-dashes and em-dashes", () => { + let expectedConfig: RawConfig = { + compatibility_date: "2024–10-01", // en-dash + }; + + let result = normalizeAndValidateConfig(expectedConfig, undefined, { + env: undefined, + }); + + expect(result.config).toEqual(expect.objectContaining(expectedConfig)); + expect(result.diagnostics.hasWarnings()).toBe(false); + expect(result.diagnostics.hasErrors()).toBe(true); + + expect(normalizeString(result.diagnostics.renderErrors())) + .toMatchInlineSnapshot(` + "Processing wrangler configuration: + - Hyphens (-) should be used rather than en-dashes (—) or em-dashes (–) in the \\"compatibility_date\\" field." + `); + + expectedConfig = { + compatibility_date: "2024—10-01", // em-dash + }; + + result = normalizeAndValidateConfig(expectedConfig, undefined, { + env: undefined, + }); + + expect(result.config).toEqual(expect.objectContaining(expectedConfig)); + expect(result.diagnostics.hasWarnings()).toBe(false); + expect(result.diagnostics.hasErrors()).toBe(true); + + expect(normalizeString(result.diagnostics.renderErrors())) + .toMatchInlineSnapshot(` + "Processing wrangler configuration: + - Hyphens (-) should be used rather than en-dashes (—) or em-dashes (–) in the \\"compatibility_date\\" field." + `); + }); + + it("should error for invalid date values", () => { + const expectedConfig: RawConfig = { + compatibility_date: "abc", + }; + + const { config, diagnostics } = normalizeAndValidateConfig( + expectedConfig, + undefined, + { env: undefined } + ); + + expect(config).toEqual(expect.objectContaining(expectedConfig)); + expect(diagnostics.hasErrors()).toBe(true); + + expect(normalizeString(diagnostics.renderErrors())) + .toMatchInlineSnapshot(` + "Processing wrangler configuration: + - \\"compatibility_date\\" should be a valid ISO-8601 date (YYYY-MM-DD), but got \\"abc\\"." + `); + }); + }); + describe("[site]", () => { it("should override `site` config defaults with provided values", () => { const expectedConfig: RawConfig = { diff --git a/packages/wrangler/src/config/validation.ts b/packages/wrangler/src/config/validation.ts index 80b2ac2774f9..4f3e5e41692e 100644 --- a/packages/wrangler/src/config/validation.ts +++ b/packages/wrangler/src/config/validation.ts @@ -1134,7 +1134,7 @@ function normalizeAndValidateEnvironment( topLevelEnv, rawEnv, "compatibility_date", - isString, + validateCompatibilityDate, undefined ), compatibility_flags: inheritable( @@ -3159,6 +3159,43 @@ const validateConsumer: ValidatorFn = (diagnostics, field, value, _config) => { return isValid; }; +const isValidDate = (value: string): boolean => { + const data = new Date(value); + return !isNaN(data.getTime()); +}; + +const validateCompatibilityDate: ValidatorFn = (diagnostics, field, value) => { + if (value === undefined) { + return true; + } + + if (typeof value !== "string") { + diagnostics.errors.push( + `Expected "${field}" to be of type string but got ${JSON.stringify(value)}.` + ); + return false; + } + + if ( + value.includes("–") || // en-dash + value.includes("—") // em-dash + ) { + diagnostics.errors.push( + `Hyphens (-) should be used rather than en-dashes (—) or em-dashes (–) in the "${field}" field.` + ); + return false; + } + + if (!isValidDate(value)) { + diagnostics.errors.push( + `"${field}" should be a valid ISO-8601 date (YYYY-MM-DD), but got ${JSON.stringify(value)}.` + ); + return false; + } + + return true; +}; + const validatePipelineBinding: ValidatorFn = (diagnostics, field, value) => { if (typeof value !== "object" || value === null) { diagnostics.errors.push(