0.1.2 |
|
---|---|
0.1.1 |
|
0.1.0 |
|
#5 in #zod
每月下载 31 次
14KB
113 行
schemars-zod
实验性 schamars
到 zod
转换器。
包含一些函数,帮助从使用 schemars 注释的 Rust 类型生成 Zod 架构。
使用方法
给定这些类型
#[derive(schemars::JsonSchema)]
struct MyStruct {
a: String,
b: u32,
}
#[derive(schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
struct MyOtherStruct {
x: f64,
y: f64,
other: MyStruct,
more: Vec<MyStruct>,
more_more: HashMap<String, MyStruct>,
time: DateTime<Utc>, // from chrono crate
}
此代码将对应 Zod 类型
let merged = merge_schemas(vec![schema_for!(MyStruct), schema_for!(MyOtherStruct)].into_iter());
let converted = convert(merged);
println!("{}", converted);
并输出
export const MyStruct = z.object({a: z.string(), b: z.number().int(),});
export type MyStruct = z.infer<typeof MyStruct>;
export const MyOtherStruct = z.object({
more: z.array(z.lazy(() => MyStruct)),
moreMore: z.record(z.lazy(() => MyStruct)),
other: z.lazy(() => MyStruct),
time: z.coerce.date(),
x: z.number(),
y: z.number(),
});
export type MyOtherStruct = z.infer<typeof MyOtherStruct>;
依赖关系
~1.7–2.7MB
~53K SLoC