brokkr-broker::utils::templating
Rust
Tera template rendering and JSON Schema validation utilities.
This module provides functions for:
- Validating Tera template syntax at creation time
- Rendering Tera templates with parameters at instantiation time
- Validating JSON Schema definitions at creation time
- Validating parameters against JSON Schema at instantiation time
Structs
Section titled “Structs”struct TemplateError
Section titled “struct TemplateError”pub
Derives: Debug, Clone
Error type for templating operations.
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
message |
String |
|
details |
Option<String> |
struct ParameterValidationError
Section titled “struct ParameterValidationError”pub
Derives: Debug, Clone
Validation error details for parameter validation.
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
path |
String |
|
message |
String |
Functions
Section titled “Functions”fn validate_tera_syntax
Section titled “fn validate_tera_syntax”pub
fn validate_tera_syntax( template_content: &str) -> Result<(,) -> Result<(), TemplateError>Validate Tera template syntax without rendering.
Called at template creation time to fail fast on invalid templates. Does not require actual parameter values - only checks syntax.
Parameters:
| Name | Type | Description |
|---|---|---|
template_content |
&str |
The Tera template string to validate |
Returns:
Ok(())if the template syntax is validErr(TemplateError)with details about the syntax error
Examples:
use brokkr_broker::utils::templating::validate_tera_syntax;
// Valid templateassert!(validate_tera_syntax("Hello, {{ name }}!").is_ok());
// Invalid template - unclosed braceassert!(validate_tera_syntax("Hello, {{ name !").is_err());Source
pub fn validate_tera_syntax(template_content: &str) -> Result<(), TemplateError> { let mut tera = Tera::default();
// Try to add the template - this validates syntax tera.add_raw_template("__validation__", template_content) .map_err(|e| TemplateError { message: "Invalid Tera syntax".to_string(), details: Some(e.to_string()), })?;
Ok(())}fn render_template
Section titled “fn render_template”pub
fn render_template( template_content: &str, parameters: &Value,) -> Result<String, TemplateError>Render a Tera template with the provided parameters.
Called at template instantiation time to produce the final output.
Parameters:
| Name | Type | Description |
|---|---|---|
template_content |
&str |
The Tera template string to render |
parameters |
&Value |
JSON object containing parameter values |
Returns:
Ok(String)with the rendered outputErr(TemplateError)if rendering fails (e.g., missing required variable)
Examples:
use brokkr_broker::utils::templating::render_template;use serde_json::json;
let template = "name: {{ name }}\nreplicas: {{ replicas }}";let params = json!({"name": "my-app", "replicas": 3});
let result = render_template(template, ¶ms).unwrap();assert!(result.contains("name: my-app"));Source
pub fn render_template( template_content: &str, parameters: &Value,) -> Result<String, TemplateError> { let mut tera = Tera::default();
tera.add_raw_template("template", template_content) .map_err(|e| TemplateError { message: "Template parse error".to_string(), details: Some(e.to_string()), })?;
let mut context = Context::new();
// Flatten JSON parameters into Tera context if let Value::Object(map) = parameters { for (key, value) in map { context.insert(key, value); } }
tera.render("template", &context) .map_err(|e| TemplateError { message: "Template rendering failed".to_string(), details: Some(e.to_string()), })}fn validate_json_schema
Section titled “fn validate_json_schema”pub
fn validate_json_schema(schema_str: &str) -> Result<(), TemplateError>Validate that a string is a valid JSON Schema.
Called at template creation time to ensure the schema is valid.
Parameters:
| Name | Type | Description |
|---|---|---|
schema_str |
&str |
The JSON Schema as a string |
Returns:
Ok(())if the schema is validErr(TemplateError)with details about the validation error
Examples:
use brokkr_broker::utils::templating::validate_json_schema;
let schema = r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#;assert!(validate_json_schema(schema).is_ok());
// Invalid JSONassert!(validate_json_schema("not json").is_err());Source
pub fn validate_json_schema(schema_str: &str) -> Result<(), TemplateError> { let schema: Value = serde_json::from_str(schema_str).map_err(|e| TemplateError { message: "Invalid JSON".to_string(), details: Some(e.to_string()), })?;
JSONSchema::compile(&schema).map_err(|e| TemplateError { message: "Invalid JSON Schema".to_string(), details: Some(e.to_string()), })?;
Ok(())}fn validate_parameters
Section titled “fn validate_parameters”pub
fn validate_parameters( schema_str: &str, parameters: &Value,) -> Result<(), Vec<ParameterValidationError>>Validate parameters against a JSON Schema.
Called at template instantiation time to ensure parameters match the schema.
Parameters:
| Name | Type | Description |
|---|---|---|
schema_str |
&str |
The JSON Schema as a string |
parameters |
&Value |
The parameters to validate |
Returns:
Ok(())if parameters match the schemaErr(Vec<ParameterValidationError>)with all validation errors
Examples:
use brokkr_broker::utils::templating::validate_parameters;use serde_json::json;
let schema = r#"{"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}}}"#;
// Valid parameterslet params = json!({"name": "test"});assert!(validate_parameters(schema, ¶ms).is_ok());
// Missing required fieldlet params = json!({});assert!(validate_parameters(schema, ¶ms).is_err());Source
pub fn validate_parameters( schema_str: &str, parameters: &Value,) -> Result<(), Vec<ParameterValidationError>> { let schema: Value = serde_json::from_str(schema_str).map_err(|e| { vec![ParameterValidationError { path: String::new(), message: format!("Invalid schema JSON: {}", e), }] })?;
let compiled = JSONSchema::compile(&schema).map_err(|e| { vec![ParameterValidationError { path: String::new(), message: format!("Invalid schema: {}", e), }] })?;
if !compiled.is_valid(parameters) { let errors: Vec<ParameterValidationError> = compiled .validate(parameters) .err() .map(|iter| { iter.map(|e| ParameterValidationError { path: e.instance_path.to_string(), message: e.to_string(), }) .collect() }) .unwrap_or_default();
return Err(errors); }
Ok(())}