d2compiler: newlines in sql_tables are made invalid

This commit is contained in:
Alexander Wang 2024-08-13 22:36:22 +08:00
parent 021011afbd
commit b70f9783e2
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 42 additions and 10 deletions

View file

@ -1047,16 +1047,18 @@ func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) {
func (c *compiler) validateLabels(g *d2graph.Graph) {
for _, obj := range g.Objects {
if !strings.EqualFold(obj.Shape.Value, d2target.ShapeText) {
continue
}
if obj.Attributes.Language != "" {
// blockstrings have already been validated
continue
}
if strings.TrimSpace(obj.Label.Value) == "" {
c.errorf(obj.Label.MapKey, "shape text must have a non-empty label")
continue
if strings.EqualFold(obj.Shape.Value, d2target.ShapeText) {
if obj.Attributes.Language != "" {
// blockstrings have already been validated
continue
}
if strings.TrimSpace(obj.Label.Value) == "" {
c.errorf(obj.Label.MapKey, "shape text must have a non-empty label")
}
} else if strings.EqualFold(obj.Shape.Value, d2target.ShapeSQLTable) {
if strings.Contains(obj.Label.Value, "\n") {
c.errorf(obj.Label.MapKey, "shape sql_table cannot have newlines")
}
}
}
}

View file

@ -3114,6 +3114,21 @@ layers: {
"y/n.d2": "p",
},
},
{
name: "sql-table-header-newline",
text: `x: {
shape: sql_table
label: hello\nworld
}
y: "hello\nworld" {
shape: sql_table
hi: there
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/sql-table-header-newline.d2:3:3: shape sql_table cannot have newlines
d2/testdata/d2compiler/TestCompile/sql-table-header-newline.d2:6:1: shape sql_table cannot have newlines`,
},
}
for _, tc := range testCases {

View file

@ -0,0 +1,15 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/sql-table-header-newline.d2,2:2:26-2:21:45",
"errmsg": "d2/testdata/d2compiler/TestCompile/sql-table-header-newline.d2:3:3: shape sql_table cannot have newlines"
},
{
"range": "d2/testdata/d2compiler/TestCompile/sql-table-header-newline.d2,5:0:49-8:1:100",
"errmsg": "d2/testdata/d2compiler/TestCompile/sql-table-header-newline.d2:6:1: shape sql_table cannot have newlines"
}
]
}
}