Skip to content

Commit 4a6b459

Browse files
authored
fix(store,world): minor config validation fixes (latticexyz#2517)
1 parent 1f952c3 commit 4a6b459

File tree

13 files changed

+89
-20
lines changed

13 files changed

+89
-20
lines changed

.changeset/dry-toes-lay.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@latticexyz/store": patch
3+
"@latticexyz/world": patch
4+
---
5+
6+
Minor fixes to config input validations:
7+
8+
- `systems.openAccess` incorrectly expected `true` as the only valid input. It now allows `boolean`.
9+
- The config complained if parts of it were defined `as const` outside the config input. This is now possible.
10+
- Shorthand inputs are now enabled.

packages/store/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"test:ci": "pnpm run test"
6060
},
6161
"dependencies": {
62-
"@arktype/util": "0.0.25",
62+
"@arktype/util": "0.0.27",
6363
"@latticexyz/common": "workspace:*",
6464
"@latticexyz/config": "workspace:*",
6565
"@latticexyz/protocol-parser": "workspace:*",

packages/store/ts/config/v2/input.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type StoreInput = {
3838
export type TableShorthandInput = SchemaInput | string;
3939

4040
export type TablesWithShorthandsInput = {
41-
[key: string]: TableInput | TableShorthandInput;
41+
readonly [key: string]: TableInput | TableShorthandInput;
4242
};
4343

4444
export type StoreWithShorthandsInput = Omit<StoreInput, "tables"> & { tables: TablesWithShorthandsInput };

packages/store/ts/config/v2/store.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,25 @@ describe("defineStore", () => {
495495
}),
496496
).throwsAndHasTypeError("Overrides of `name` and `namespace` are not allowed for tables in a store config");
497497
});
498+
499+
it("should allow const enum as input", () => {
500+
const enums = {
501+
Example: ["First", "Second"],
502+
} as const;
503+
504+
attest(defineStore({ enums }).enums).equals(enums);
505+
});
506+
507+
it("should allow a const config as input", () => {
508+
const config = {
509+
tables: {
510+
Example: {
511+
schema: { id: "address", name: "string", age: "uint256" },
512+
key: ["age"],
513+
},
514+
},
515+
} as const;
516+
517+
defineStore(config);
518+
});
498519
});

packages/store/ts/config/v2/table.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getKeySchema, getValueSchema } from "@latticexyz/protocol-parser/intern
99
describe("validateKeys", () => {
1010
it("should return a tuple of valid keys", () => {
1111
attest<
12-
["static"],
12+
readonly ["static"],
1313
validateKeys<getStaticAbiTypeKeys<{ static: "uint256"; dynamic: "string" }, AbiTypeScope>, ["static"]>
1414
>();
1515
});
@@ -18,7 +18,7 @@ describe("validateKeys", () => {
1818
const scope = extendScope(AbiTypeScope, { static: "address", dynamic: "string" });
1919

2020
attest<
21-
["static", "customStatic"],
21+
readonly ["static", "customStatic"],
2222
validateKeys<
2323
getStaticAbiTypeKeys<
2424
{ static: "uint256"; dynamic: "string"; customStatic: "static"; customDynamic: "dynamic" },
@@ -33,7 +33,7 @@ describe("validateKeys", () => {
3333
const scope = extendScope(AbiTypeScope, { static: "address", dynamic: "string" });
3434

3535
attest<
36-
["static", "customStatic"],
36+
readonly ["static", "customStatic"],
3737
validateKeys<
3838
getStaticAbiTypeKeys<
3939
{ static: "uint256"; dynamic: "string"; customStatic: "static"; customDynamic: "dynamic" },
@@ -218,13 +218,13 @@ describe("resolveTable", () => {
218218
attest(() =>
219219
defineTable({
220220
schema: { id: "address" },
221-
// @ts-expect-error Type 'string' is not assignable to type 'string[]'
221+
// @ts-expect-error Type 'string' is not assignable to type 'readonly string[]'
222222
key: "",
223223
name: "",
224224
}),
225225
)
226226
.throws('Invalid key. Expected `("id")[]`, received ``')
227-
.type.errors("Type 'string' is not assignable to type 'string[]'");
227+
.type.errors("Type 'string' is not assignable to type 'readonly string[]'");
228228
});
229229

230230
it("should throw if a string is provided as schema", () => {

packages/store/ts/config/v2/table.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export function isValidPrimaryKey<schema extends SchemaInput, scope extends Scop
3737
);
3838
}
3939

40-
export type validateKeys<validKeys extends PropertyKey, keys> = keys extends string[]
40+
export type validateKeys<validKeys extends PropertyKey, keys> = keys extends readonly string[]
4141
? {
42-
[i in keyof keys]: keys[i] extends validKeys ? keys[i] : validKeys;
42+
readonly [i in keyof keys]: keys[i] extends validKeys ? keys[i] : validKeys;
4343
}
44-
: string[];
44+
: readonly string[];
4545

4646
export type ValidateTableOptions = { inStoreContext: boolean };
4747

packages/store/ts/exports/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ export {
1616
export { storeEventsAbi } from "../storeEventsAbi";
1717
export type { StoreEventsAbi, StoreEventsAbiItem } from "../storeEventsAbi";
1818

19-
export { defineStore } from "../config/v2/store";
19+
export { defineStoreWithShorthands as defineStore } from "../config/v2/storeWithShorthands";
2020
export type { Store } from "../config/v2/output";

packages/world/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"test:ci": "pnpm run test"
5555
},
5656
"dependencies": {
57-
"@arktype/util": "0.0.25",
57+
"@arktype/util": "0.0.27",
5858
"@latticexyz/common": "workspace:*",
5959
"@latticexyz/config": "workspace:*",
6060
"@latticexyz/schema-type": "workspace:*",

packages/world/ts/config/v2/input.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type SystemInput = {
1414
*/
1515
registerFunctionSelectors?: boolean;
1616
/** If openAccess is true, any address can call the system */
17-
openAccess?: true;
17+
openAccess?: boolean;
1818
/** An array of addresses or system names that can access the system */
1919
accessList?: string[];
2020
};

packages/world/ts/config/v2/world.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -727,4 +727,29 @@ describe("defineWorld", () => {
727727
}),
728728
).type.errors("Namespaces config will be enabled soon.");
729729
});
730+
731+
it("should allow setting openAccess of a system to false", () => {
732+
const config = defineWorld({
733+
systems: {
734+
Example: {
735+
openAccess: false,
736+
},
737+
},
738+
});
739+
740+
attest<false>(config.systems.Example.openAccess).equals(false);
741+
});
742+
743+
it("should allow a const config as input", () => {
744+
const config = {
745+
tables: {
746+
Example: {
747+
schema: { id: "address", name: "string", age: "uint256" },
748+
key: ["age"],
749+
},
750+
},
751+
} as const;
752+
753+
defineWorld(config);
754+
});
730755
});

packages/world/ts/config/v2/worldWithShorthands.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,17 @@ describe("defineWorldWithShorthands", () => {
321321
"Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.",
322322
);
323323
});
324+
325+
it("should allow a const config as input", () => {
326+
const config = {
327+
tables: {
328+
Example: {
329+
schema: { id: "address", name: "string", age: "uint256" },
330+
key: ["age"],
331+
},
332+
},
333+
} as const;
334+
335+
defineWorldWithShorthands(config);
336+
});
324337
});

packages/world/ts/exports/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
export { helloWorldEvent, worldDeployedEvent } from "../worldEvents";
88

9-
export { defineWorld } from "../config/v2/world";
9+
export { defineWorldWithShorthands as defineWorld } from "../config/v2/worldWithShorthands";
1010
export type { World } from "../config/v2/output";

pnpm-lock.yaml

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)