validate text shape labels

This commit is contained in:
Gavin Nishizawa 2023-09-18 13:16:50 -07:00
parent b84a51e1c0
commit 731a7df195
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 32 additions and 0 deletions

View file

@ -82,6 +82,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph {
if len(c.err.Errors) == 0 { if len(c.err.Errors) == 0 {
c.validateKeys(g.Root, ir) c.validateKeys(g.Root, ir)
} }
c.validateLabels(g)
c.validateNear(g) c.validateNear(g)
c.validateEdges(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) { func (c *compiler) validateNear(g *d2graph.Graph) {
for _, obj := range g.Objects { for _, obj := range g.Objects {
if obj.NearKey != nil { if obj.NearKey != nil {

View file

@ -2712,6 +2712,26 @@ object: {
`, `,
expErr: `d2/testdata/d2compiler/TestCompile/reserved-composite.d2:1:1: reserved field shape does not accept composite`, 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 { for _, tc := range testCases {