#schema #schemars #annotated #aid #generation #zod #schamars

已撤销 schamars-zod

一些函数,帮助从使用 schemars 注释的 Rust 类型生成 Zod 架构

0.1.2 2023 年 4 月 1 日
0.1.1 2023 年 4 月 1 日
0.1.0 2023 年 3 月 31 日

#5 in #zod

每月下载 31

MIT 许可证

14KB
113

schemars-zod

实验性 schamarszod 转换器。

包含一些函数,帮助从使用 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