Skip to content

Commit 02acc73

Browse files
committed
fix(go): refuse a JSON field with no target in Plan.Validate
The engine refuses a json field that names no target (json_needs_target) after the plan crosses the guest. Validate already refused target options without a target; it now refuses a JSON document with no target too, and names the field before the guest is asked. Claude-Session: https://claude.ai/code/session_01N7bCqa3ZVJMcEE99RzVqEu
1 parent 20f099e commit 02acc73

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎languages/golang/internal/record/record.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ func (p *Plan) Validate() error {
332332
if f.TargetOptions != nil && !f.IsTarget() {
333333
return fmt.Errorf("record: field %q has target options and no target", f.Name)
334334
}
335+
// A JSON document is stored only through a target that stores one
336+
// (JsonSearch); the engine refuses a json field with no target
337+
// (json_needs_target), and saying it here names the field first.
338+
if f.Kind == JSON && !f.IsTarget() {
339+
return fmt.Errorf("record: field %q is a JSON document and names no target; a document is stored through an EQL type that stores one, such as JsonSearch", f.Name)
340+
}
335341
if f.IsTarget() {
336342
if len(f.Outputs) != 0 {
337343
return fmt.Errorf("record: field %q names the EQL type %s and outputs; a field has one or the other", f.Name, f.Target)

‎languages/golang/internal/record/record_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ func TestWireGivesATargetItsOptions(t *testing.T) {
212212
if err := bad.Validate(); err == nil || !strings.Contains(err.Error(), "no target") {
213213
t.Fatalf("options with no target: %v", err)
214214
}
215+
noTarget := &Plan{Context: []string{"users"}, Fields: []Field{{Name: "profile", Kind: JSON, Outputs: []Output{Ciphertext}}}}
216+
if err := noTarget.Validate(); err == nil || !strings.Contains(err.Error(), `"profile" is a JSON document and names no target`) {
217+
t.Fatalf("a json field with no target: %v", err)
218+
}
215219
if !JSON.Known() {
216220
t.Fatal("json is a kind")
217221
}

0 commit comments

Comments
 (0)