Skip to content

Commit

Permalink
refactor(world): use validateNamespaces function (latticexyz#2896)
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaaa authored Jun 6, 2024
1 parent 978df93 commit 254d1a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
12 changes: 5 additions & 7 deletions packages/world/ts/config/v2/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ export function validateNamespaces<scope extends Scope = AbiTypeScope>(
namespaces: unknown,
scope: scope,
): asserts namespaces is NamespacesInput {
if (isObject(namespaces)) {
for (const namespace of Object.values(namespaces)) {
if (!hasOwnKey(namespace, "tables")) {
throw new Error(`Expected namespace config, received ${JSON.stringify(namespace)}`);
}
if (!isObject(namespaces)) {
throw new Error(`Expected namespaces, received ${JSON.stringify(namespaces)}`);
}
for (const namespace of Object.values(namespaces)) {
if (hasOwnKey(namespace, "tables")) {
validateTables(namespace.tables, scope);
}
return;
}
throw new Error(`Expected namespaces config, received ${JSON.stringify(namespaces)}`);
}

export type resolveNamespacedTables<world> = "namespaces" extends keyof world
Expand Down
12 changes: 2 additions & 10 deletions packages/world/ts/config/v2/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import {
Store,
hasOwnKey,
validateStore,
isObject,
} from "@latticexyz/store/config/v2";
import { SystemsInput, WorldInput } from "./input";
import { CONFIG_DEFAULTS, MODULE_DEFAULTS } from "./defaults";
import { Tables } from "@latticexyz/store/internal";
import { resolveSystems } from "./systems";
import { resolveNamespacedTables } from "./namespaces";
import { resolveNamespacedTables, validateNamespaces } from "./namespaces";
import { resolveCodegen } from "./codegen";
import { resolveDeploy } from "./deploy";

Expand All @@ -41,14 +40,7 @@ export function validateWorld(world: unknown): asserts world is WorldInput {
validateStore(world);

if (hasOwnKey(world, "namespaces")) {
if (!isObject(world.namespaces)) {
throw new Error(`Expected namespaces, received ${JSON.stringify(world.namespaces)}`);
}
for (const namespace of Object.values(world.namespaces)) {
if (hasOwnKey(namespace, "tables")) {
validateTables(namespace.tables, scope);
}
}
validateNamespaces(world.namespaces, scope);
}
}

Expand Down

0 comments on commit 254d1a7

Please sign in to comment.