@ collidor/ struct
具有标准Schema兼容性的模块数据结构构建器,JSON Schema / Zod双向桥,Axon图端口打字,和Vee-Validate形式生成.
特征
- 📐 标准Schema V1 客户端:直接与现代形式,验证,API库相通.
- 🔄 双向桥: 无损失相交 佐德语Name (单位:千美元)
toZod, (中文(简体) ),fromZod和 杰森·斯克马 (单位:千美元)toJSONSchema, (中文(简体) ),fromJSONSchema) (中文(简体) ). - ⚡ Axon 端口子打字: 完整的图表数据类型描述器绘图和子类型相容性检查器(
isAssignable) (中文(简体) ). - 📝 动态 UI 格式生成: 自动字段布局、部件推论和交互式窗体构建器(
StructBuilder, (中文(简体) ),getFormFields) (中文(简体) ). - 🗄️ 存储和 DDL 迁移:SQLite/SQL后端的Schema DDL生成和交易迁移.
- 🧮 计算属性: 具有AST依赖性地貌分辨率的安全内膜公式评价.
安装
德诺/JSR
deno add jsr:@collidor/struct
节点/ npm/ pnpm/ Bun
npx jsr add @collidor/struct
# or
pnpm dlx jsr add @collidor/struct
# or
bunx jsr add @collidor/struct
快速启动
import { s } from "@collidor/struct";
// Define a schema
const UserSchema = s.struct({
id: s.string().uuid(),
name: s.string().min(2).max(50),
email: s.string().email(),
age: s.number().positive().optional(),
tags: s.array(s.string()).default([]),
});
// Validate synchronous or asynchronous
const result = UserSchema.safeParse({
id: "123e4567-e89b-12d3-a456-426614174000",
name: "Alykam",
email: "[email protected]",
});
if (result.success) {
console.log("Validated user:", result.data);
} else {
console.error("Validation issues:", result.issues);
}
统一窗体构建器( E)@collidor/struct/form(中文(简体) )
这个 @collidor/struct/form 二次导出统一空间布局容器、非数据 UI 装饰器、有条件渲染以及直接在结构定义内收集视图模式:
import { f } from "@collidor/struct/form";
const OnboardingForm = f.struct({
banner: f.alert({
title: "Welcome",
message: "Fill in your details below",
tone: "info",
}),
// Put two inputs on the same line
nameRow: f.row({
firstName: f.string().min(2),
lastName: f.string().min(2),
}, { gap: "1rem" }),
accountType: f.enum(["personal", "business"] as const).default("personal"),
// Conditional branch (validation bypassed when hidden)
companyInfo: f.when('accountType == "business"', {
companyName: f.string(),
vatId: f.string(),
}),
// Collection view modes (asTable, asList, asCards, asTags)
skills: f.array(f.string()).asTags(),
submitBtn: f.button({ label: "Create Account", action: "submit" }),
});
// Flat data model: UI-only nodes stripped, layout containers flattened!
const validated = OnboardingForm.parse({
firstName: "Ada",
lastName: "Lovelace",
accountType: "personal",
skills: ["computing", "math"],
});
// Framework-agnostic Headless Form Controller
const controller = f.createController(OnboardingForm, {
initialValues: validated,
onSubmit: (values) => console.log("Submitted:", values),
});
Zod & JSON Schema 接口
import { s, toZod, fromZod, toJSONSchema, fromJSONSchema } from "@collidor/struct";
import { z } from "zod";
// Convert struct to Zod
const zodUser = toZod(UserSchema);
// Convert Zod to struct
const zodSchema = z.object({ count: z.number().int().positive() });
const structFromZod = fromZod(zodSchema);
// JSON Schema export
const jsonSchema = toJSONSchema(UserSchema);
许可证
麻省理工学院 阿利坎·伯德崎