diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 26d2052..ef90eed 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -19,11 +19,17 @@ new adapter through native inspection/comparison, processing and preflight, selection/protection, bootstrap, and output source checks. Targeted CLI integration tests passed; executable probes confirmed extension acceptance and imported-file hardlink collision protection. The application code is unchanged from the initial -review. R1 remains open. +review. Completion status for each finding is recorded below. ## R1: Schema errors lose field-level source attribution -**Priority:** P3. **Status:** Open. **Plan stage:** 2, surfaced through Stage 3. +**Priority:** P3. **Status:** Complete. **Plan stage:** 2, surfaced through Stage 3. + +**Completion:** Report leaf schema failures through their instance paths so native +metadata identifies the contributing source, with root/parent fallback for absent +fields. Regression coverage verifies an earlier input's invalid value and missing +field, including escaped collection names. Go 1.27.4 offline appconfig and CLI +tests passed. **Location:** [shared glossary loading](../../internal/appconfig/sharedglossary/load.go), lines 34–36. diff --git a/internal/appconfig/sharedglossary/load.go b/internal/appconfig/sharedglossary/load.go index b011584..30219a1 100644 --- a/internal/appconfig/sharedglossary/load.go +++ b/internal/appconfig/sharedglossary/load.go @@ -3,6 +3,9 @@ package sharedglossary import ( "context" "errors" + "strings" + + "github.com/santhosh-tekuri/jsonschema/v6" "gitea.maximumdirect.net/eric/audita/v4/internal/core/config" ck "go.maximumdirect.net/configkit" @@ -33,7 +36,12 @@ func Load(ctx context.Context, inputs []ck.Input) (*ck.Result[map[string]any], e SequenceIdentities: []ck.SequenceIdentity{config.GlossaryEntryIdentity(ck.SequenceSelector{ck.SequenceLiteralKey("collections")})}, Validate: func(document *map[string]any, validation *ck.Validation) { if err := validator.Validate(*document, "input"); err != nil { - validation.Error(ck.Keys(), err.Error()) + var schemaError *jsonschema.ValidationError + if errors.As(err, &schemaError) { + reportSchemaError(validation, schemaError) + } else { + validation.Error(ck.Keys(), err.Error()) + } } }, }, @@ -47,3 +55,23 @@ func Load(ctx context.Context, inputs []ck.Input) (*ck.Result[map[string]any], e } return result, nil } + +func reportSchemaError(validation *ck.Validation, err *jsonschema.ValidationError) { + if len(err.Causes) > 0 { + for _, cause := range err.Causes { + reportSchemaError(validation, cause) + } + return + } + path := ck.Keys() + if len(err.InstanceLocation) > 0 { + segments := make([]string, len(err.InstanceLocation)) + for i, segment := range err.InstanceLocation { + segments[i] = strings.ReplaceAll(strings.ReplaceAll(segment, "~", "~0"), "/", "~1") + } + // Escaped instance segments always form a valid JSON Pointer. Parsing + // leaves numeric keys/indexes for ConfigKit to resolve against the data. + path, _ = ck.ParsePath("/" + strings.Join(segments, "/")) + } + validation.Error(path, err.Error()) +} diff --git a/internal/appconfig/sharedglossary/load_test.go b/internal/appconfig/sharedglossary/load_test.go index e3bbadb..09bd528 100644 --- a/internal/appconfig/sharedglossary/load_test.go +++ b/internal/appconfig/sharedglossary/load_test.go @@ -131,6 +131,43 @@ func TestLoadErrorsAndEmptyInput(t *testing.T) { } } +func TestSchemaFailureUsesContributingSource(t *testing.T) { + dir := t.TempDir() + first := filepath.Join(dir, "first.yml") + last := filepath.Join(dir, "last.yml") + if err := os.WriteFile(last, []byte("schema_version: maximumdirect.glossary.v1\ncollections: {}\n"), 0600); err != nil { + t.Fatal(err) + } + for _, tc := range []struct{ name, entry, path string }{ + {"invalid value", "{id: a, name: 17, category: person}", "/collections/7~1~0/entries/0/name"}, + {"missing field", "{id: a, category: person}", "/collections/7~1~0/entries/0"}, + } { + t.Run(tc.name, func(t *testing.T) { + body := "schema_version: maximumdirect.glossary.v1\ncollections:\n '7/~':\n entries: [" + tc.entry + "]\n" + if err := os.WriteFile(first, []byte(body), 0600); err != nil { + t.Fatal(err) + } + _, err := Load(context.Background(), []ck.Input{{Source: ck.File(first)}, {Source: ck.File(last)}}) + var loadErr *ck.LoadError + if !errors.As(err, &loadErr) { + t.Fatalf("expected native load error, got %v", err) + } + sources := make(map[string]string) + for _, source := range loadErr.Sources() { + sources[source.ID] = source.Path + } + diagnostics := loadErr.Diagnostics() + if len(diagnostics) != 1 || diagnostics[0].Path.String() != tc.path { + t.Fatalf("unexpected diagnostics: %+v", diagnostics) + } + location := diagnostics[0].Primary + if location == nil || sources[location.SourceID] != first { + t.Fatalf("diagnostic points to %v with sources %v", location, sources) + } + }) + } +} + func TestLoadResolvesInheritanceAndPreservesExtensions(t *testing.T) { path := filepath.Join(t.TempDir(), "shared.yml") body := `schema_version: maximumdirect.glossary.v1