schemars-zod

Crates.ioschemars-zod
lib.rsschemars-zod
version0.1.5
sourcesrc
created_at2023-04-01 09:22:06.038937
updated_at2023-04-06 19:43:02.987756
descriptionA few functions to aid Zod schema generation from rust types annotated with schemars
homepagehttps://lib.rs/crates/schemars-zod
repositoryhttps://github.com/audiocloud/schemars-zod
max_upload_size
id827182
size43,183
Bojan Ĺ ernek (baadc0de)

documentation

https://docs.rs/schamars-zod/latest/schemars_zod

README

schemars-zod

experimental schamars to zod converter.

Contains A few functions to aid Zod schema generation from rust types annotated with schemars.

usage

given these types:

#[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
}

This code will corresponding Zod types:

let merged = merge_schemas(vec![schema_for!(MyStruct), schema_for!(MyOtherStruct)].into_iter());
let converted = convert(merged);
println!("{}", converted);

And output:

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>;
Commit count: 16

cargo fmt