Skip to main content

CustomTypeSchema

Trait CustomTypeSchema 

Source
pub trait CustomTypeSchema {
    // Required method
    fn schema_type() -> &'static str;

    // Provided methods
    fn schema_format() -> Option<&'static str> { ... }
    fn schema_description() -> Option<String> { ... }
    fn schema_additional_properties() -> Option<Value> { ... }
    fn json_schema() -> Value { ... }
}
Expand description

Trait for types that can provide their own JSON Schema representation

This trait should be implemented for custom types that don’t have a direct JSON representation but can be serialized to JSON, such as dates, UUIDs, and other special types.

§Examples

use rstructor::schema::CustomTypeSchema;
use serde_json::json;
use serde::{Serialize, Deserialize};

// Create a custom date type
#[derive(Serialize, Deserialize)]
struct MyCustomDate {
    year: u16,
    month: u8,
    day: u8,
}

// Implement CustomTypeSchema for our custom date type
impl CustomTypeSchema for MyCustomDate {
    fn schema_type() -> &'static str {
        "string"
    }
     
    fn schema_format() -> Option<&'static str> {
        Some("date-time")
    }
     
    fn schema_description() -> Option<String> {
        Some("ISO-8601 formatted date and time".to_string())
    }
}

Required Methods§

Source

fn schema_type() -> &'static str

Returns the JSON Schema type for this custom type

This is typically “string” for dates, UUIDs, etc.

Provided Methods§

Source

fn schema_format() -> Option<&'static str>

Returns the JSON Schema format for this custom type

Common formats include “date-time”, “uuid”, “email”, etc.

Source

fn schema_description() -> Option<String>

Returns a description of this custom type for documentation

Source

fn schema_additional_properties() -> Option<Value>

Returns any additional JSON Schema properties for this type

Source

fn json_schema() -> Value

Generate a complete JSON Schema object for this type

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§