Skip to main content

schema_type

Macro schema_type 

Source
schema_type!() { /* proc-macro */ }
Expand description

Generate a new struct type derived from an existing type with field filtering.

This macro creates a new struct at compile time by picking or omitting fields from an existing type that has #[derive(Schema)].

§Syntax

// Pick specific fields
schema_type!(CreateUserRequest from User, pick = ["name", "email"]);

// Omit specific fields
schema_type!(UserResponse from User, omit = ["password", "internal_id"]);

// Without Clone derive
schema_type!(UserUpdate from User, pick = ["name"], clone = false);

§Parameters

  • NewTypeName: The name of the new struct to generate
  • from SourceType: The source type to derive from (must have #[derive(Schema)])
  • pick = [...]: List of field names to include (excludes all others)
  • omit = [...]: List of field names to exclude
  • clone = bool: Whether to derive Clone (default: true)

Note: omit and pick cannot be used together.

§Example

use vespera::{Schema, schema_type};

#[derive(Schema)]
pub struct User {
    pub id: i32,
    pub name: String,
    pub email: String,
    pub password: String,
}

// Generate CreateUserRequest with only name and email
schema_type!(CreateUserRequest from User, pick = ["name", "email"]);

// Generate UserPublic without password
schema_type!(UserPublic from User, omit = ["password"]);

// Now use in handlers:
pub async fn create_user(Json(req): Json<CreateUserRequest>) -> Json<UserPublic> {
    // ...
}