The verbatim-module-syntax lint rule was correctly flagging that GraphConfig is only used in a type position (typeof GraphConfig). Since typeof resolves purely at the type level, import type works fine here and is the correct form. No lint exclusion needed. Also: deno fmt across all files (markdown line wrapping).
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { KindGuard, type TSchema } from "@alkdev/typebox";
|
|
import { Value } from "@alkdev/typebox/value";
|
|
import { assert } from "@std/assert";
|
|
import { EdgeType, GraphConfig, GraphSchema, NodeType } from "./types.ts";
|
|
|
|
export class SchemaBuilder {
|
|
private schema: {
|
|
config: Record<string, unknown>;
|
|
nodeTypes: Record<string, NodeType>;
|
|
edgeTypes: Record<string, EdgeType>;
|
|
} = {
|
|
config: {},
|
|
nodeTypes: {},
|
|
edgeTypes: {},
|
|
};
|
|
|
|
config(config: Partial<GraphConfig>): SchemaBuilder {
|
|
const configObj = Value.Default(GraphConfig, config) as GraphConfig;
|
|
this.check(GraphConfig, configObj);
|
|
this.schema.config = configObj as Record<string, unknown>;
|
|
return this;
|
|
}
|
|
|
|
nodeType(name: string, schema: TSchema): SchemaBuilder {
|
|
assert(
|
|
KindGuard.IsSchema(schema),
|
|
`type '${name}' is not a valid json schema.`,
|
|
);
|
|
|
|
if (!this.schema.nodeTypes) this.schema.nodeTypes = {};
|
|
const nodeTypeObj: NodeType = { name, schema };
|
|
|
|
this.check(NodeType, nodeTypeObj);
|
|
this.schema.nodeTypes[name] = nodeTypeObj;
|
|
return this;
|
|
}
|
|
|
|
edgeType(
|
|
name: string,
|
|
schema: TSchema,
|
|
options?: { allowedSourceTypes?: string[]; allowedTargetTypes?: string[] },
|
|
): SchemaBuilder {
|
|
assert(
|
|
KindGuard.IsSchema(schema),
|
|
`type '${name}' is not a valid json schema.`,
|
|
);
|
|
|
|
if (!this.schema.edgeTypes) this.schema.edgeTypes = {};
|
|
const edgeTypeObj: EdgeType = { name, schema, ...options };
|
|
|
|
this.check(EdgeType, edgeTypeObj);
|
|
this.schema.edgeTypes[name] = edgeTypeObj;
|
|
return this;
|
|
}
|
|
|
|
check(schema: TSchema, value: unknown): void {
|
|
if (!Value.Check(schema, value)) {
|
|
const errors = [...Value.Errors(schema, value)];
|
|
throw new Error(
|
|
`Invalid schema structure: ${
|
|
JSON.stringify(errors.map((e) => `${e.path}: ${e.message}`))
|
|
}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
build(): GraphSchema {
|
|
this.check(GraphSchema, this.schema);
|
|
return this.schema as GraphSchema;
|
|
}
|
|
}
|