Skip to main content

Crate vld_ts

Crate vld_ts 

Source
Expand description

§vld-ts — Generate TypeScript Zod schemas from vld

vld-ts is designed for a vld-first workflow:

  • generate Zod from vld schema instances (to_zod)
  • call Type::to_zod() on your vld::schema! type via impl_to_zod!(Type)
  • generate Valibot from vld schema instances (to_valibot)
  • call Type::to_valibot() via impl_to_valibot!(Type)
  • generate plain JSON Schema via to_openapi(...) or Type::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 ToOpenApi for a vld::schema! type.
impl_to_valibot
Implement ToValibot for a vld::schema! type.
impl_to_zod
Implement ToZod for a vld::schema! type.

Traits§

ToOpenApi
Trait for types that can generate plain JSON Schema.
ToRefs
Trait for types that can return discovered $ref paths.
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 $ref paths 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.