Skip to content

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

pub

Derives: Debug, Clone

Error type for templating operations.

Name Type Description
message String
details Option<String>

pub

Derives: Debug, Clone

Validation error details for parameter validation.

Name Type Description
path String
message String

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 valid
  • Err(TemplateError) with details about the syntax error

Examples:

use brokkr_broker::utils::templating::validate_tera_syntax;
// Valid template
assert!(validate_tera_syntax("Hello, {{ name }}!").is_ok());
// Invalid template - unclosed brace
assert!(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(())
}

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 output
  • Err(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, &params).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()),
})
}

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 valid
  • Err(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 JSON
assert!(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(())
}

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 schema
  • Err(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 parameters
let params = json!({"name": "test"});
assert!(validate_parameters(schema, &params).is_ok());
// Missing required field
let params = json!({});
assert!(validate_parameters(schema, &params).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(())
}