Skip to main content

schema_for

Function schema_for 

Source
pub fn schema_for<T: JsonSchema>() -> Result<Value>
Expand description

Generate the JSON Schema 2020-12 document for T and parse it into a crate::Value tree, ready for indexing, transcoding, or further programmatic walking.

The schema honours #[doc] comments on fields (they become description properties), #[serde(rename = "...")] (renames the schema property), #[serde(default)] (drops the property from required), and the rest of the conventions documented upstream in [schemars].

§Errors

  • The schema serializer fails (vanishingly unlikely; schemas never contain non-serializable shapes).
  • The serialized JSON cannot be re-parsed as YAML (would indicate a noyalib parser bug, since JSON ⊂ YAML 1.2).

§Examples

use noyalib::{schema_for, JsonSchema};

#[derive(JsonSchema)]
struct Cfg { port: u16 }

let schema = schema_for::<Cfg>().unwrap();
assert_eq!(schema["type"].as_str(), Some("object"));