Render Json

Produces a DiagnosticDto JSON payload for use in REST API error responses and MCP tool results. The DTO is the canonical error shape for all new endpoints introduced in the block-validation-dashboard feature — endpoints MUST emit DiagnosticDto rather than the legacy {"error": "..."} envelope.

Sanitisation and path policy

Two safety transforms are applied during serialisation:

  1. String fields (message, hint, yaml_excerpt) are passed through super::sanitize::sanitize_for_serialization so embedded ANSI escapes, BEL/CR, and Unicode Cf (bidi/zero-width) codepoints from a malicious YAML cannot reach a JSON consumer’s rendering surface (web UI, MCP client, log viewer).
  2. source_file is canonicalised to a workspace-relative path. When the path lies under the current working directory it is emitted relative (./blocks/postgres.yml); otherwise only the file name is emitted (postgres.yml). Absolute paths are never serialised — this prevents daemon-host filesystem layout from leaking through API/MCP responses.

Key Types

  • DiagnosticDto — JSON-serialisable DTO mirroring Diagnostic.
  • SpanDto — JSON form of super::Span.
  • JsonRenderer — implements DiagnosticRenderer for JSON output.

Example

use stackie::diagnostic::{Diagnostic, DiagnosticCode, DiagnosticRenderer};
use stackie::diagnostic::render_json::JsonRenderer;
use std::sync::Arc;

let diag = Diagnostic::new(
    DiagnosticCode::E101EmptyName,
    "Block name cannot be empty",
    Arc::from("name: \n"),
);
let json = JsonRenderer.render(&diag);
assert!(json.contains("E101"));