ToJsonSchema

Trait ToJsonSchema 

Source
pub trait ToJsonSchema {
    // Required methods
    fn schema_name() -> &'static str;
    fn json_schema() -> JsonSchema;
}
Expand description

Types that can generate JSON Schema (Draft 2020-12).

This trait provides programmatic JSON Schema generation as an alternative to the CLI-based approach.

§Example

use domainstack_schema::{ToJsonSchema, JsonSchema};

struct User {
    email: String,
    age: u8,
}

impl ToJsonSchema for User {
    fn schema_name() -> &'static str {
        "User"
    }

    fn json_schema() -> JsonSchema {
        JsonSchema::object()
            .property("email", JsonSchema::string().format("email"))
            .property("age", JsonSchema::integer().minimum(0).maximum(150))
            .required(&["email", "age"])
    }
}

Required Methods§

Source

fn schema_name() -> &'static str

The name of this schema in the $defs section.

Source

fn json_schema() -> JsonSchema

Generate the JSON Schema for this type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ToJsonSchema for bool

Source§

impl ToJsonSchema for f32

Source§

impl ToJsonSchema for f64

Source§

impl ToJsonSchema for i32

Source§

impl ToJsonSchema for i64

Source§

impl ToJsonSchema for str

Source§

impl ToJsonSchema for u8

Source§

impl ToJsonSchema for u16

Source§

impl ToJsonSchema for u32

Source§

impl ToJsonSchema for String

Source§

impl<T: ToJsonSchema> ToJsonSchema for Option<T>

Source§

impl<T: ToJsonSchema> ToJsonSchema for Vec<T>

Implementors§