Skip to main content

Crate typeshift

Crate typeshift 

Source
Expand description

typeshift facade crate.

This crate provides a Zod-like parse workflow using idiomatic Rust types. Use #[typeshift] on your struct or enum, then use:

Many #[validate(...)] field attributes are reflected in schema output via schemars (such as email, url, length, range, and regex).

§Example

use typeshift::typeshift;

#[typeshift]
struct User {
    #[validate(length(min = 3))]
    name: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let user: User = typeshift::parse_str(r#"{"name":"Ada"}"#)?;
    let json = typeshift::to_json(&user)?;
    let schema = typeshift::schema_json::<User>();

    assert_eq!(json, r#"{"name":"Ada"}"#);
    assert_eq!(schema["type"], "object");
    Ok(())
}

Modules§

schemars
Core trait and runtime helpers.
serde
Core trait and runtime helpers.
validator
Core trait and runtime helpers.

Enums§

TypeShiftError
Core trait and runtime helpers. Error returned by parse_str.

Traits§

TypeShift
Core trait and runtime helpers. Marker trait for types that can be parsed, validated, and schematized.

Functions§

parse_str
Core trait and runtime helpers. Parse JSON text into T and run validator::Validate.
schema_json
Core trait and runtime helpers. Generate a JSON Schema document for T as serde_json::Value.
to_json
Core trait and runtime helpers. Serialize a typed value into a compact JSON string.

Attribute Macros§

typeshift
Attribute macro that adds serde, validator, and schemars derives.

Derive Macros§

TypeShift
Legacy compatibility derive.