| 279 | } |
| 280 | |
| 281 | func validateWithSchema(frontmatter map[string]any, schemaJSON, context string) error { |
| 282 | schemaCompilerLog.Printf("Validating frontmatter against schema for context: %s (%d fields)", context, len(frontmatter)) |
| 283 | |
| 284 | // Determine which cached schema to use based on the schemaJSON |
| 285 | var schema *jsonschema.Schema |
| 286 | var err error |
| 287 | |
| 288 | switch schemaJSON { |
| 289 | case mainWorkflowSchema: |
| 290 | schemaCompilerLog.Print("Using cached main workflow schema") |
| 291 | schema, err = getCompiledMainWorkflowSchema() |
| 292 | case mcpConfigSchema: |
| 293 | schemaCompilerLog.Print("Using cached MCP config schema") |
| 294 | schema, err = getCompiledMcpConfigSchema() |
| 295 | case RepoConfigSchema: |
| 296 | schemaCompilerLog.Print("Using cached repo config schema") |
| 297 | schema, err = GetCompiledRepoConfigSchema() |
| 298 | case awManifestSchema: |
| 299 | schemaCompilerLog.Print("Using cached aw manifest schema") |
| 300 | schema, err = getCompiledAwManifestSchema() |
| 301 | default: |
| 302 | // Fallback for unknown schemas (shouldn't happen in normal operation) |
| 303 | // Compile the schema on-the-fly |
| 304 | schemaCompilerLog.Print("Compiling unknown schema on-the-fly") |
| 305 | schema, err = compileSchema(schemaJSON, "http://contoso.com/schema.json") |
| 306 | } |
| 307 | |
| 308 | if err != nil { |
| 309 | return fmt.Errorf("schema validation error for %s: %w", context, err) |
| 310 | } |
| 311 | |
| 312 | // Normalize YAML-native Go types to JSON-compatible types for schema validation. |
| 313 | // goccy/go-yaml produces uint64/int64 for integers, but JSON schema validators |
| 314 | // expect float64 for all numbers (matching encoding/json's behavior). |
| 315 | // This avoids the overhead of a json.Marshal/Unmarshal roundtrip. |
| 316 | var normalized any |
| 317 | if frontmatter == nil { |
| 318 | normalized = make(map[string]any) |
| 319 | } else { |
| 320 | normalized = normalizeForJSONSchema(frontmatter) |
| 321 | } |
| 322 | |
| 323 | // Validate the normalized frontmatter |
| 324 | if err := schema.Validate(normalized); err != nil { |
| 325 | schemaCompilerLog.Printf("Schema validation failed for %s: %v", context, err) |
| 326 | return err |
| 327 | } |
| 328 | |
| 329 | schemaCompilerLog.Printf("Schema validation passed for context: %s", context) |
| 330 | return nil |
| 331 | } |
| 332 | |
| 333 | // pathPositionPrefixPattern matches the "'path' (line N, col M): " prefix that |
| 334 | // formatSchemaFailureDetail prepends to each detail line for multi-failure output. |