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§
Sourcefn schema_name() -> &'static str
fn schema_name() -> &'static str
The name of this schema in the $defs section.
Sourcefn json_schema() -> JsonSchema
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".