Skip to content

Commit

Permalink
Add more helpful error messages when validating compatibility date
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Oct 17, 2024
1 parent 586c253 commit 6eea915
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fifty-dots-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

feat: More helpful error messages when validating compatibility date
77 changes: 77 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
39 changes: 38 additions & 1 deletion packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ function normalizeAndValidateEnvironment(
topLevelEnv,
rawEnv,
"compatibility_date",
isString,
validateCompatibilityDate,
undefined
),
compatibility_flags: inheritable(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 6eea915

Please sign in to comment.