Skip to main content

repair_yaml

Function repair_yaml 

Source
pub fn repair_yaml(yaml_content: &str, schema: &Value) -> RepairResult
Expand description

Apply the 6-stage YAML repair pipeline to yaml_content guided by schema.

Stages:

  1. Normalize — strip surrounding whitespace, normalise CRLF → LF.
  2. Parseserde_yamlserde_json::Value; returns early on parse failure.
  3. Fill defaults — inject "default" values from schema for absent fields.
  4. Collect errors — walk schema recursively and gather violations.
  5. Categorize — split into deterministic (auto-fixable) vs ambiguous errors.
  6. Fix — apply type coercion and extra-key removal; ambiguous errors become RepairResult::llm_fields for human or LLM review.

The function never panics — even an entirely unparseable input returns a RepairResult with valid = false and the original YAML unchanged.

§Arguments

  • yaml_content — raw YAML string (may be invalid).
  • schema — JSON Schema object used to guide repair (e.g. from crate::schema::SchemaRegistry).

§Example

use devops_validate::repair::repair_yaml;
use serde_json::json;

let schema = json!({
    "type": "object",
    "properties": {
        "replicas": { "type": "integer", "default": 1 },
        "name":     { "type": "string" }
    }
});

// "replicas" is a string "3" — should be coerced to integer 3
let result = repair_yaml("replicas: \"3\"\nname: my-app", &schema);

assert!(result.valid);
assert!(result.repaired_yaml.contains("replicas: 3"));
assert!(result.warnings.iter().any(|w| w.contains("Coerced")));