diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 88db84d7e..36ce88bba 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -82,6 +82,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph { if len(c.err.Errors) == 0 { c.validateKeys(g.Root, ir) } + c.validateLabels(g) c.validateNear(g) c.validateEdges(g) @@ -998,6 +999,17 @@ func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) { } } +func (c *compiler) validateLabels(g *d2graph.Graph) { + for _, obj := range g.Objects { + if obj.Shape.Value == d2target.ShapeText { + if strings.TrimSpace(obj.Label.Value) == "" { + c.errorf(obj.Label.MapKey, "text must have a label") + continue + } + } + } +} + func (c *compiler) validateNear(g *d2graph.Graph) { for _, obj := range g.Objects { if obj.NearKey != nil { diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index a8788e95e..3c33b03ce 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2712,6 +2712,26 @@ object: { `, expErr: `d2/testdata/d2compiler/TestCompile/reserved-composite.d2:1:1: reserved field shape does not accept composite`, }, + { + name: "text_no_label", + text: `a: "ok" { + shape: text +} +b: " \n " { + shape: text +} + +c: "" { + shape: text +} +d: "" { + shape: circle +} +e: " \n " +`, + expErr: ` +`, + }, } for _, tc := range testCases {