Skip to content

Commit

Permalink
fix: check for nonempty kv id and r2 bucket_name (#1667)
Browse files Browse the repository at this point in the history
* fix: check for nonempty kv id and r2 bucket_name

* chore: run prettier

* chore: add changeset
  • Loading branch information
arjunyel authored Aug 22, 2022
1 parent 46b73b5 commit ba6451d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-sheep-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: check for nonempty kv id and r2 bucket_name
8 changes: 6 additions & 2 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,7 @@ describe("normalizeAndValidateConfig()", () => {
id: "KV_ID_2",
preview_id: 2222,
},
{ binding: "VALID", id: "" },
],
} as unknown as RawConfig,
undefined,
Expand All @@ -1654,7 +1655,8 @@ describe("normalizeAndValidateConfig()", () => {
- \\"kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
- \\"kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
- \\"kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
- \\"kv_namespaces[3]\\" bindings should, optionally, have a string \\"preview_id\\" field but got {\\"binding\\":\\"KV_BINDING_2\\",\\"id\\":\\"KV_ID_2\\",\\"preview_id\\":2222}."
- \\"kv_namespaces[3]\\" bindings should, optionally, have a string \\"preview_id\\" field but got {\\"binding\\":\\"KV_BINDING_2\\",\\"id\\":\\"KV_ID_2\\",\\"preview_id\\":2222}.
- \\"kv_namespaces[4]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\",\\"id\\":\\"\\"}."
`);
});
});
Expand Down Expand Up @@ -1740,6 +1742,7 @@ describe("normalizeAndValidateConfig()", () => {
bucket_name: "R2_BUCKET_2",
preview_bucket_name: 2555,
},
{ binding: "R2_BINDING_1", bucket_name: "" },
],
} as unknown as RawConfig,
undefined,
Expand All @@ -1759,7 +1762,8 @@ describe("normalizeAndValidateConfig()", () => {
- \\"r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
- \\"r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
- \\"r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
- \\"r2_buckets[3]\\" bindings should, optionally, have a string \\"preview_bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_2\\",\\"bucket_name\\":\\"R2_BUCKET_2\\",\\"preview_bucket_name\\":2555}."
- \\"r2_buckets[3]\\" bindings should, optionally, have a string \\"preview_bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_2\\",\\"bucket_name\\":\\"R2_BUCKET_2\\",\\"preview_bucket_name\\":2555}.
- \\"r2_buckets[4]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\",\\"bucket_name\\":\\"\\"}."
`);
});
});
Expand Down
25 changes: 19 additions & 6 deletions packages/wrangler/src/config/validation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export const validateRequiredProperty = (
container: string,
key: string,
value: unknown,
type: string,
type: TypeofType,
choices?: unknown[]
): boolean => {
if (container) {
Expand Down Expand Up @@ -417,7 +417,7 @@ export const validateOptionalProperty = (
container: string,
key: string,
value: unknown,
type: string,
type: TypeofType,
choices?: unknown[]
): boolean => {
if (value !== undefined) {
Expand All @@ -440,7 +440,7 @@ export const validateTypedArray = (
diagnostics: Diagnostics,
container: string,
value: unknown,
type: string
type: TypeofType
): boolean => {
let isValid = true;
if (!Array.isArray(value)) {
Expand Down Expand Up @@ -472,7 +472,7 @@ export const validateOptionalTypedArray = (
diagnostics: Diagnostics,
container: string,
value: unknown,
type: string
type: TypeofType
) => {
if (value !== undefined) {
return validateTypedArray(diagnostics, container, value, type);
Expand All @@ -486,7 +486,7 @@ export const validateOptionalTypedArray = (
export const isRequiredProperty = <T extends object>(
obj: object,
prop: keyof T,
type: string,
type: TypeofType,
choices?: unknown[]
): obj is T =>
hasProperty<T>(obj, prop) &&
Expand All @@ -499,7 +499,7 @@ export const isRequiredProperty = <T extends object>(
export const isOptionalProperty = <T extends object>(
obj: object,
prop: keyof T,
type: string
type: TypeofType
): obj is T => !hasProperty<T>(obj, prop) || typeof obj[prop] === type;

/**
Expand Down Expand Up @@ -582,3 +582,16 @@ const isRecord = (
value: unknown
): value is Record<string | number | symbol, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);

/**
* JavaScript `typeof` operator return values.
*/
type TypeofType =
| "string"
| "number"
| "bigint"
| "boolean"
| "symbol"
| "undefined"
| "object"
| "function";
10 changes: 8 additions & 2 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,10 @@ const validateKVBinding: ValidatorFn = (diagnostics, field, value) => {
);
isValid = false;
}
if (!isRequiredProperty(value, "id", "string")) {
if (
!isRequiredProperty(value, "id", "string") ||
(value as { id: string }).id.length === 0
) {
diagnostics.errors.push(
`"${field}" bindings should have a string "id" field but got ${JSON.stringify(
value
Expand Down Expand Up @@ -1668,7 +1671,10 @@ const validateR2Binding: ValidatorFn = (diagnostics, field, value) => {
);
isValid = false;
}
if (!isRequiredProperty(value, "bucket_name", "string")) {
if (
!isRequiredProperty(value, "bucket_name", "string") ||
(value as { bucket_name: string }).bucket_name.length === 0
) {
diagnostics.errors.push(
`"${field}" bindings should have a string "bucket_name" field but got ${JSON.stringify(
value
Expand Down

0 comments on commit ba6451d

Please sign in to comment.