diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 9ef9c3937..4718636c5 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -886,6 +886,10 @@ func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) { if !in && arrowheadIn { c.errorf(f.LastPrimaryKey(), fmt.Sprintf(`invalid shape, can only set "%s" for arrowheads`, obj.Shape.Value)) } + case "constraint": + if obj.Shape.Value != d2target.ShapeSQLTable { + c.errorf(f.LastPrimaryKey(), `"constraint" keyword can only be used in "sql_table" shapes`) + } } return } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 1ecd7ff30..19f41a60b 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2117,9 +2117,7 @@ ok: { label: bar constraint: BIZ }`, - assertions: func(t *testing.T, g *d2graph.Graph) { - assert.String(t, "bar", g.Objects[0].Label.Value) - }, + expErr: `d2/testdata/d2compiler/TestCompile/constraint_label.d2:3:3: "constraint" keyword can only be used in "sql_table" shapes`, }, { name: "invalid_direction", diff --git a/testdata/d2compiler/TestCompile/constraint_label.exp.json b/testdata/d2compiler/TestCompile/constraint_label.exp.json index ddc5dcdea..a866cd12c 100644 --- a/testdata/d2compiler/TestCompile/constraint_label.exp.json +++ b/testdata/d2compiler/TestCompile/constraint_label.exp.json @@ -1,180 +1,11 @@ { - "graph": { - "name": "", - "isFolderOnly": false, - "ast": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-3:1:38", - "nodes": [ - { - "map_key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-3:1:38", - "key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:3:3", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:3:3", - "value": [ - { - "string": "foo", - "raw_string": "foo" - } - ] - } - } - ] - }, - "primary": {}, - "value": { - "map": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:4:4-3:1:38", - "nodes": [ - { - "map_key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:2:8-1:12:18", - "key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:2:8-1:7:13", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:2:8-1:7:13", - "value": [ - { - "string": "label", - "raw_string": "label" - } - ] - } - } - ] - }, - "primary": {}, - "value": { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:9:15-1:12:18", - "value": [ - { - "string": "bar", - "raw_string": "bar" - } - ] - } - } - } - }, - { - "map_key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:17:36", - "key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:12:31", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:12:31", - "value": [ - { - "string": "constraint", - "raw_string": "constraint" - } - ] - } - } - ] - }, - "primary": {}, - "value": { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:14:33-2:17:36", - "value": [ - { - "string": "BIZ", - "raw_string": "BIZ" - } - ] - } - } - } - } - ] - } - } - } - } - ] - }, - "root": { - "id": "", - "id_val": "", - "attributes": { - "label": { - "value": "" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 - }, - "edges": null, - "objects": [ + "graph": null, + "err": { + "errs": [ { - "id": "foo", - "id_val": "foo", - "references": [ - { - "key": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:3:3", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:3:3", - "value": [ - { - "string": "foo", - "raw_string": "foo" - } - ] - } - } - ] - }, - "key_path_index": 0, - "map_key_edge_index": -1 - } - ], - "attributes": { - "label": { - "value": "bar" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": [ - "BIZ" - ] - }, - "zIndex": 0 + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:17:36", + "errmsg": "d2/testdata/d2compiler/TestCompile/constraint_label.d2:3:3: \"constraint\" keyword can only be used in \"sql_table\" shapes" } ] - }, - "err": null + } }