mockforge_schema/
lib.rs

1//! JSON Schema generation for MockForge configuration files
2//!
3//! This crate provides functionality to generate JSON Schema definitions
4//! from MockForge's configuration structs, enabling IDE autocomplete and
5//! validation for `mockforge.yaml` and `mockforge.toml` files.
6
7use schemars::schema_for;
8use serde_json;
9
10/// Generate JSON Schema for MockForge ServerConfig
11///
12/// This function generates a complete JSON Schema that can be used by
13/// IDEs and editors to provide autocomplete, validation, and documentation
14/// for MockForge configuration files.
15///
16/// # Returns
17///
18/// A JSON Schema object as a serde_json::Value
19///
20/// # Example
21///
22/// ```rust
23/// use mockforge_schema::generate_config_schema;
24/// use serde_json;
25///
26/// let schema = generate_config_schema();
27/// let schema_json = serde_json::to_string_pretty(&schema).unwrap();
28/// println!("{}", schema_json);
29/// ```
30pub fn generate_config_schema() -> serde_json::Value {
31    // Generate schema from ServerConfig
32    // ServerConfig needs to have JsonSchema derive (via feature flag)
33    let schema = schema_for!(mockforge_core::ServerConfig);
34    serde_json::to_value(schema).expect("Failed to serialize schema")
35}
36
37/// Generate JSON Schema and return as a formatted JSON string
38///
39/// # Returns
40///
41/// A pretty-printed JSON string containing the schema
42pub fn generate_config_schema_json() -> String {
43    let schema = generate_config_schema();
44    serde_json::to_string_pretty(&schema).expect("Failed to format schema as JSON")
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_schema_generation() {
53        let schema = generate_config_schema();
54        assert!(schema.is_object());
55
56        // Verify schema has required fields
57        let obj = schema.as_object().unwrap();
58        assert!(obj.contains_key("$schema") || obj.contains_key("type"));
59    }
60
61    #[test]
62    fn test_schema_json_formatting() {
63        let json = generate_config_schema_json();
64        assert!(!json.is_empty());
65
66        // Verify it's valid JSON
67        let parsed: Result<serde_json::Value, _> = serde_json::from_str(&json);
68        assert!(parsed.is_ok());
69    }
70}