Expand description
§vld-ts — Generate TypeScript Zod schemas from vld
vld-ts is designed for a vld-first workflow:
- generate Zod from
vldschema instances (to_zod) - call
Type::to_zod()on yourvld::schema!type viaimpl_to_zod!(Type) - generate Valibot from
vldschema instances (to_valibot) - call
Type::to_valibot()viaimpl_to_valibot!(Type) - generate plain JSON Schema via
to_openapi(...)orType::to_openapi()
§Example
use vld_ts::{impl_to_openapi, impl_to_valibot, impl_to_zod, to_openapi, to_valibot, to_zod, ToOpenApi, ToRefs, ToValibot, ToZod};
vld::schema! {
pub struct Address {
pub city: String => vld::string().min(1),
}
}
vld::schema! {
pub struct Order {
pub shipping: Address => vld::nested!(Address),
}
}
impl_to_zod!(Order);
impl_to_valibot!(Order);
impl_to_openapi!(Order);
impl_to_openapi!(Address);
let zod = to_zod(&vld::string().min(2).email());
assert!(zod.contains(".email()"));
let ts = Order::to_zod();
assert!(ts.starts_with("z.object("));
let valibot = Order::to_valibot();
assert!(valibot.starts_with("v.object"));
let single_valibot = to_valibot(&vld::string().min(2).email());
assert!(single_valibot.contains("v.string()"));
let schema_json = Address::to_openapi();
assert_eq!(schema_json["type"], "object");
let refs = Order::to_refs();
assert!(refs.contains(&"#/components/schemas/Address".to_string()));
let email_schema = to_openapi(&vld::string().email());
assert_eq!(email_schema["type"], "string");Macros§
- impl_
to_ openapi - Implement
ToOpenApifor avld::schema!type. - impl_
to_ valibot - Implement
ToValibotfor avld::schema!type. - impl_
to_ zod - Implement
ToZodfor avld::schema!type.
Traits§
- ToOpen
Api - Trait for types that can generate plain JSON Schema.
- ToRefs
- Trait for types that can return discovered
$refpaths. - ToValibot
- Trait for types that can generate their Valibot representation.
- ToZod
- Trait for types that can generate their Zod representation.
Functions§
- openapi_
refs - Collect all
$refpaths from a schema object. - to_
openapi - Convert a vld schema instance to plain JSON Schema (OpenAPI-compatible).
- to_
valibot - Convert a vld schema instance directly to Valibot.
- to_zod
- Convert a vld schema instance directly to Zod.