Home

cuesonnet @main - refs - log -
-
https://git.jolheiser.com/cuesonnet.git
CUE + Jsonnet
tree log patch
more comprehensive tests Signed-off-by: jolheiser <git@jolheiser.com>
Signature
-----BEGIN SSH SIGNATURE----- U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgBTEvCQk6VqUAdN2RuH6bj1dNkY oOpbPWj+jw4ua1B1cAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5 AAAAQDUDrzwQyjb2GFcj4wxP2zBVHYWdbCUq3YbcNMlDmkobTyWmKfhp/oSWGUA9UDD54Z ONqOfXIyltwzRlwN8qmAI= -----END SSH SIGNATURE-----
jolheiser <git@jolheiser.com>
5 hours ago
75 changed files, 1561 additions(+), 94 deletions(-)
cuesonnet_decode_test.gocuesonnet_test.gotestdata/person/almost_valid.jsonnettestdata/composition/invalid.jsonnettestdata/composition/schema.cuetestdata/composition/valid.jsonnettestdata/person/invalid.jsonnettestdata/constraints/invalid_pattern.jsonnettestdata/constraints/invalid_range.jsonnettestdata/constraints/schema.cuetestdata/constraints/valid.jsonnettestdata/defaults/schema.cuetestdata/defaults/valid_minimal.jsonnettestdata/defaults/valid_override.jsonnettestdata/definitions/invalid.jsonnettestdata/definitions/schema.cuetestdata/definitions/valid.jsonnettestdata/disjunctions/invalid.jsonnettestdata/disjunctions/schema.cuetestdata/disjunctions/valid_a.jsonnettestdata/disjunctions/valid_b.jsonnettestdata/enums/invalid.jsonnettestdata/enums/schema.cuetestdata/enums/valid.jsonnettestdata/person/valid.jsonnettestdata/jsonnet/comprehensions/data.jsonnettestdata/jsonnet/comprehensions/schema.cuetestdata/jsonnet/conditionals/data.jsonnettestdata/jsonnet/conditionals/schema.cuetestdata/jsonnet/functions/data.jsonnettestdata/jsonnet/functions/schema.cuetestdata/jsonnet/inheritance/data.jsonnettestdata/jsonnet/inheritance/schema.cuetestdata/jsonnet/multiline/data.jsonnettestdata/jsonnet/multiline/schema.cuetestdata/jsonnet/object_comprehension/data.jsonnettestdata/jsonnet/object_comprehension/schema.cuetestdata/jsonnet/self_reference/data.jsonnettestdata/jsonnet/self_reference/schema.cuetestdata/jsonnet/stdlib/data.jsonnettestdata/jsonnet/stdlib/schema.cuetestdata/person/functions.jsonnettestdata/person/functions_list.jsonnettestdata/lists/invalid_element.jsonnettestdata/lists/invalid_length.jsonnettestdata/lists/schema.cuetestdata/lists/valid.jsonnettestdata/patterns/invalid.jsonnettestdata/patterns/schema.cuetestdata/patterns/valid.jsonnettestdata/primitives/invalid.jsonnettestdata/primitives/schema.cuetestdata/primitives/valid.jsonnettestdata/person/schema.cuetestdata/person/schema_list.cuetestdata/stdlib/invalid.jsonnettestdata/stdlib/schema.cuetestdata/stdlib/valid.jsonnettestdata/structs/any/invalid.jsonnettestdata/structs/any/schema.cuetestdata/structs/any/valid_object.jsonnettestdata/structs/any/valid_scalar.jsonnettestdata/structs/closed/invalid_extra.jsonnettestdata/structs/closed/schema.cuetestdata/structs/closed/valid.jsonnettestdata/structs/nested/invalid.jsonnettestdata/structs/nested/schema.cuetestdata/structs/nested/valid.jsonnettestdata/structs/optional/invalid.jsonnettestdata/structs/optional/schema.cuetestdata/structs/optional/valid_full.jsonnettestdata/structs/optional/valid_minimal.jsonnettestdata/tuples/invalid.jsonnettestdata/tuples/schema.cuetestdata/tuples/valid.jsonnet
I cuesonnet_decode_test.go
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
diff --git a/cuesonnet_decode_test.go b/cuesonnet_decode_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..366847898f4e14f0d6c1db60c0f6cf72254f462e
--- /dev/null
+++ b/cuesonnet_decode_test.go
@@ -0,0 +1,618 @@
+package cuesonnet_test
+
+import (
+	"reflect"
+	"strings"
+	"testing"
+
+	"go.jolheiser.com/cuesonnet"
+)
+
+// checkDecode decodes data into T via sc and compares with expected using reflect.DeepEqual.
+func checkDecode[T any](t *testing.T, sc cuesonnet.Schema, data string, expected T) {
+	t.Helper()
+	var got T
+	if err := sc.Decode(strings.NewReader(data), &got); err != nil {
+		t.Fatalf("decode error: %v", err)
+	}
+	if !reflect.DeepEqual(got, expected) {
+		t.Errorf("value mismatch:\n  got:  %+v\n  want: %+v", got, expected)
+	}
+}
+
+// decode wraps checkDecode into a check func for use in table-driven tests.
+func decode[T any](expected T) func(*testing.T, cuesonnet.Schema, string) {
+	return func(t *testing.T, sc cuesonnet.Schema, data string) {
+		t.Helper()
+		checkDecode(t, sc, data, expected)
+	}
+}
+
+type (
+	PersonDecode struct {
+		FirstName string `json:"firstName"`
+		LastName  string `json:"lastName"`
+		Age       int    `json:"age"`
+		Birthday  string `json:"birthday"`
+		Gopher    bool   `json:"gopher"`
+	}
+
+	PrimitivesDecode struct {
+		Str  string  `json:"str"`
+		Num  int     `json:"num"`
+		Dec  float64 `json:"dec"`
+		Flag bool    `json:"flag"`
+		Qty  float64 `json:"qty"`
+	}
+
+	ConstraintsDecode struct {
+		Age      int     `json:"age"`
+		Score    float64 `json:"score"`
+		Username string  `json:"username"`
+		Bio      string  `json:"bio"`
+		Level    int     `json:"level"`
+		Slug     string  `json:"slug"`
+	}
+
+	// Metadata and Annotations are _ in CUE; decoded type depends on the data.
+	AnyDecode struct {
+		Name        string `json:"name"`
+		Metadata    any    `json:"metadata"`
+		Annotations any    `json:"annotations,omitempty"`
+	}
+
+	ClosedDecode struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+
+	OptionalDecode struct {
+		Name  string `json:"name"`
+		Email string `json:"email"`
+		Phone string `json:"phone,omitempty"`
+		Bio   string `json:"bio,omitempty"`
+	}
+
+	AddressDecode struct {
+		Street string `json:"street"`
+		City   string `json:"city"`
+		Zip    string `json:"zip"`
+	}
+	ContactDecode struct {
+		Label string `json:"label"`
+		Value string `json:"value"`
+	}
+	NestedDecode struct {
+		Name     string          `json:"name"`
+		Address  AddressDecode   `json:"address"`
+		Contacts []ContactDecode `json:"contacts"`
+	}
+
+	ListItemDecode struct {
+		ID    int    `json:"id"`
+		Label string `json:"label"`
+	}
+	ListsDecode struct {
+		Items   []ListItemDecode `json:"items"`
+		Tags    []string         `json:"tags"`
+		Counts  []int            `json:"counts"`
+		Limited []string         `json:"limited"`
+	}
+
+	// Value and Nullable are disjunction types; any holds the concrete runtime value.
+	DisjunctionsDecode struct {
+		Value    any    `json:"value"`
+		Mode     string `json:"mode"`
+		Priority int    `json:"priority"`
+		Nullable any    `json:"nullable"`
+	}
+
+	DefaultsDecode struct {
+		Name    string   `json:"name"`
+		Role    string   `json:"role"`
+		Enabled bool     `json:"enabled"`
+		Timeout int      `json:"timeout"`
+		Tags    []string `json:"tags"`
+	}
+
+	EnumsDecode struct {
+		Color    string   `json:"color"`
+		Status   string   `json:"status"`
+		Priority int      `json:"priority"`
+		Flags    []string `json:"flags"`
+	}
+
+	DefNameDecode struct {
+		First string `json:"first"`
+		Last  string `json:"last"`
+	}
+	DefContactDecode struct {
+		Email string `json:"email,omitempty"`
+		Phone string `json:"phone,omitempty"`
+	}
+	DefPersonDecode struct {
+		Name    DefNameDecode    `json:"name"`
+		Age     int              `json:"age"`
+		Contact DefContactDecode `json:"contact"`
+	}
+
+	StdlibDecode struct {
+		CreatedAt string   `json:"createdAt"`
+		Birthday  string   `json:"birthday"`
+		Username  string   `json:"username"`
+		Tags      []string `json:"tags"`
+		Initials  string   `json:"initials"`
+		Angle     float64  `json:"angle"`
+	}
+
+	CompositionDecode struct {
+		FirstName string `json:"firstName"`
+		LastName  string `json:"lastName"`
+		CreatedAt string `json:"createdAt"`
+		UpdatedAt string `json:"updatedAt"`
+		Age       int    `json:"age"`
+	}
+
+	PatternsDecode struct {
+		Name   string            `json:"name"`
+		Labels map[string]string `json:"labels"`
+		Counts map[string]int    `json:"counts"`
+	}
+
+	// CUE fixed-length tuples decode positionally; Tag is [string, int] so []any.
+	TuplesDecode struct {
+		Color    []int     `json:"color"`
+		Position []float64 `json:"position"`
+		Tag      []any     `json:"tag"`
+	}
+
+	FunctionUserDecode struct {
+		Name     string `json:"name"`
+		Greeting string `json:"greeting"`
+		Age      int    `json:"age"`
+	}
+
+	ComprehensionItemDecode struct {
+		Index   int    `json:"index"`
+		Squared int    `json:"squared"`
+		Label   string `json:"label"`
+	}
+
+	ConditionalEntryDecode struct {
+		Value    int    `json:"value"`
+		Category string `json:"category"`
+		Doubled  int    `json:"doubled"`
+	}
+
+	InheritanceDecode struct {
+		Kind     string `json:"kind"`
+		Version  string `json:"version"`
+		Name     string `json:"name"`
+		Replicas int    `json:"replicas"`
+		Image    string `json:"image"`
+	}
+
+	MultilineDecode struct {
+		Description string `json:"description"`
+		Template    string `json:"template"`
+	}
+
+	SelfRefDecode struct {
+		Width     int `json:"width"`
+		Height    int `json:"height"`
+		Area      int `json:"area"`
+		Perimeter int `json:"perimeter"`
+	}
+
+	JStdlibDecode struct {
+		Upper     string   `json:"upper"`
+		Joined    string   `json:"joined"`
+		Formatted string   `json:"formatted"`
+		Count     int      `json:"count"`
+		Words     []string `json:"words"`
+		Mapped    []string `json:"mapped"`
+		Filtered  []string `json:"filtered"`
+	}
+)
+
+func TestCUEsonnetDecode(t *testing.T) {
+	type decodeTest struct {
+		name   string
+		schema string
+		data   string
+		check  func(*testing.T, cuesonnet.Schema, string)
+	}
+
+	jim := PersonDecode{
+		FirstName: "Jim",
+		LastName:  "Jimbly",
+		Age:       45,
+		Birthday:  "01/02/2003",
+		Gopher:    true,
+	}
+
+	tests := []decodeTest{
+		// person
+		{
+			name:   "person/valid",
+			schema: "testdata/person/schema.cue",
+			data:   "testdata/person/valid.jsonnet",
+			check:  decode(jim),
+		},
+		{
+			name:   "person/functions",
+			schema: "testdata/person/schema.cue",
+			data:   "testdata/person/functions.jsonnet",
+			check:  decode(jim),
+		},
+		{
+			name:   "person/list",
+			schema: "testdata/person/schema_list.cue",
+			data:   "testdata/person/functions_list.jsonnet",
+			check:  decode([]PersonDecode{jim}),
+		},
+
+		// primitives
+		{
+			name:   "primitives/valid",
+			schema: "testdata/primitives/schema.cue",
+			data:   "testdata/primitives/valid.jsonnet",
+			check:  decode(PrimitivesDecode{Str: "hello", Num: 42, Dec: 3.14, Flag: true, Qty: 2.718}),
+		},
+
+		// constraints
+		{
+			name:   "constraints/valid",
+			schema: "testdata/constraints/schema.cue",
+			data:   "testdata/constraints/valid.jsonnet",
+			check:  decode(ConstraintsDecode{Age: 30, Score: 95.5, Username: "john_doe", Bio: "A software engineer.", Level: 5, Slug: "my-slug"}),
+		},
+
+		// structs/any
+		// valid_scalar: metadata is a plain string — no type ambiguity.
+		{
+			name:   "structs/any/valid_scalar",
+			schema: "testdata/structs/any/schema.cue",
+			data:   "testdata/structs/any/valid_scalar.jsonnet",
+			check:  decode(AnyDecode{Name: "Alice", Metadata: "just a plain string"}),
+		},
+		// valid_object: metadata is a JSON object; CUE decodes it to map[string]any.
+		{
+			name:   "structs/any/valid_object",
+			schema: "testdata/structs/any/schema.cue",
+			data:   "testdata/structs/any/valid_object.jsonnet",
+			check: func(t *testing.T, sc cuesonnet.Schema, data string) {
+				t.Helper()
+				var got AnyDecode
+				if err := sc.Decode(strings.NewReader(data), &got); err != nil {
+					t.Fatalf("decode error: %v", err)
+				}
+				if got.Name != "Alice" {
+					t.Errorf("Name: got %q, want %q", got.Name, "Alice")
+				}
+				meta, ok := got.Metadata.(map[string]any)
+				if !ok {
+					t.Fatalf("Metadata: got %T, want map[string]any", got.Metadata)
+				}
+				if meta["role"] != "admin" {
+					t.Errorf("Metadata.role: got %v, want %q", meta["role"], "admin")
+				}
+				if _, ok := got.Annotations.(map[string]any); !ok {
+					t.Errorf("Annotations: got %T, want map[string]any", got.Annotations)
+				}
+			},
+		},
+
+		// structs/closed
+		{
+			name:   "structs/closed/valid",
+			schema: "testdata/structs/closed/schema.cue",
+			data:   "testdata/structs/closed/valid.jsonnet",
+			check:  decode(ClosedDecode{Name: "Alice", Age: 30}),
+		},
+
+		// structs/optional
+		{
+			name:   "structs/optional/full",
+			schema: "testdata/structs/optional/schema.cue",
+			data:   "testdata/structs/optional/valid_full.jsonnet",
+			check:  decode(OptionalDecode{Name: "Alice", Email: "alice@example.com", Phone: "555-1234", Bio: "Software engineer."}),
+		},
+		{
+			name:   "structs/optional/minimal",
+			schema: "testdata/structs/optional/schema.cue",
+			data:   "testdata/structs/optional/valid_minimal.jsonnet",
+			check:  decode(OptionalDecode{Name: "Alice", Email: "alice@example.com"}),
+		},
+
+		// structs/nested
+		{
+			name:   "structs/nested/valid",
+			schema: "testdata/structs/nested/schema.cue",
+			data:   "testdata/structs/nested/valid.jsonnet",
+			check: decode(NestedDecode{
+				Name:    "Alice",
+				Address: AddressDecode{Street: "123 Main St", City: "Springfield", Zip: "12345"},
+				Contacts: []ContactDecode{
+					{Label: "email", Value: "alice@example.com"},
+					{Label: "phone", Value: "555-1234"},
+				},
+			}),
+		},
+
+		// lists
+		{
+			name:   "lists/valid",
+			schema: "testdata/lists/schema.cue",
+			data:   "testdata/lists/valid.jsonnet",
+			check: decode(ListsDecode{
+				Items:   []ListItemDecode{{ID: 1, Label: "first"}, {ID: 2, Label: "second"}},
+				Tags:    []string{"go", "cue", "jsonnet"},
+				Counts:  []int{0, 1, 2, 100},
+				Limited: []string{"only", "a", "few"},
+			}),
+		},
+
+		// disjunctions
+		// valid_a: all any fields hold strings — no type ambiguity.
+		{
+			name:   "disjunctions/valid_a",
+			schema: "testdata/disjunctions/schema.cue",
+			data:   "testdata/disjunctions/valid_a.jsonnet",
+			check:  decode(DisjunctionsDecode{Value: "text", Mode: "read", Priority: 1, Nullable: "present"}),
+		},
+		// valid_b: Value is an integer and Nullable is null.
+		// CUE decodes integers into any as int64; null becomes nil.
+		{
+			name:   "disjunctions/valid_b",
+			schema: "testdata/disjunctions/schema.cue",
+			data:   "testdata/disjunctions/valid_b.jsonnet",
+			check: func(t *testing.T, sc cuesonnet.Schema, data string) {
+				t.Helper()
+				var got DisjunctionsDecode
+				if err := sc.Decode(strings.NewReader(data), &got); err != nil {
+					t.Fatalf("decode error: %v", err)
+				}
+				if got.Mode != "write" {
+					t.Errorf("Mode: got %q, want %q", got.Mode, "write")
+				}
+				if got.Priority != 3 {
+					t.Errorf("Priority: got %d, want 3", got.Priority)
+				}
+				if got.Nullable != nil {
+					t.Errorf("Nullable: got %v, want nil", got.Nullable)
+				}
+				switch v := got.Value.(type) {
+				case int64:
+					if v != 42 {
+						t.Errorf("Value: got %d, want 42", v)
+					}
+				case float64:
+					if v != 42 {
+						t.Errorf("Value: got %g, want 42", v)
+					}
+				default:
+					t.Errorf("Value: unexpected type %T (value %v)", got.Value, got.Value)
+				}
+			},
+		},
+
+		// defaults
+		// minimal: only name provided; CUE fills role="user", enabled=true, timeout=30, tags=[].
+		{
+			name:   "defaults/minimal",
+			schema: "testdata/defaults/schema.cue",
+			data:   "testdata/defaults/valid_minimal.jsonnet",
+			check:  decode(DefaultsDecode{Name: "Alice", Role: "user", Enabled: true, Timeout: 30, Tags: []string{}}),
+		},
+		{
+			name:   "defaults/override",
+			schema: "testdata/defaults/schema.cue",
+			data:   "testdata/defaults/valid_override.jsonnet",
+			check:  decode(DefaultsDecode{Name: "Alice", Role: "admin", Enabled: false, Timeout: 60, Tags: []string{"api", "v2"}}),
+		},
+
+		// enums
+		{
+			name:   "enums/valid",
+			schema: "testdata/enums/schema.cue",
+			data:   "testdata/enums/valid.jsonnet",
+			check:  decode(EnumsDecode{Color: "red", Status: "active", Priority: 3, Flags: []string{"a", "b"}}),
+		},
+
+		// definitions
+		{
+			name:   "definitions/valid",
+			schema: "testdata/definitions/schema.cue",
+			data:   "testdata/definitions/valid.jsonnet",
+			check: decode([]DefPersonDecode{
+				{Name: DefNameDecode{First: "Alice", Last: "Smith"}, Age: 30, Contact: DefContactDecode{Email: "alice@example.com"}},
+				{Name: DefNameDecode{First: "Bob", Last: "Jones"}, Age: 25, Contact: DefContactDecode{Phone: "555-123-4567"}},
+				{Name: DefNameDecode{First: "Carol", Last: "White"}, Age: 42, Contact: DefContactDecode{Email: "carol@example.com", Phone: "123-456-7890"}},
+			}),
+		},
+
+		// stdlib
+		{
+			name:   "stdlib/valid",
+			schema: "testdata/stdlib/schema.cue",
+			data:   "testdata/stdlib/valid.jsonnet",
+			check: decode(StdlibDecode{
+				CreatedAt: "2024-01-15T10:30:00Z",
+				Birthday:  "01/15/1990",
+				Username:  "alice42",
+				Tags:      []string{"go", "cue", "jsonnet"},
+				Initials:  "AB",
+				Angle:     3.14,
+			}),
+		},
+
+		// composition
+		{
+			name:   "composition/valid",
+			schema: "testdata/composition/schema.cue",
+			data:   "testdata/composition/valid.jsonnet",
+			check: decode([]CompositionDecode{
+				{FirstName: "Alice", LastName: "Smith", CreatedAt: "2024-01-15T10:00:00Z", UpdatedAt: "2024-06-01T12:00:00Z", Age: 30},
+				{FirstName: "Bob", LastName: "Jones", CreatedAt: "2023-05-20T08:30:00Z", UpdatedAt: "2023-05-20T08:30:00Z", Age: 25},
+			}),
+		},
+
+		// patterns
+		{
+			name:   "patterns/valid",
+			schema: "testdata/patterns/schema.cue",
+			data:   "testdata/patterns/valid.jsonnet",
+			check: decode(PatternsDecode{
+				Name: "service-a",
+				Labels: map[string]string{
+					"env":    "production",
+					"region": "us-east-1",
+					"team":   "platform",
+				},
+				Counts: map[string]int{
+					"requests": 1000,
+					"errors":   5,
+					"warnings": 42,
+				},
+			}),
+		},
+
+		// tuples
+		// Color and Position are uniformly typed; Tag is [string, int] so []any.
+		// The int element decodes as int64 or float64 depending on CUE version.
+		{
+			name:   "tuples/valid",
+			schema: "testdata/tuples/schema.cue",
+			data:   "testdata/tuples/valid.jsonnet",
+			check: func(t *testing.T, sc cuesonnet.Schema, data string) {
+				t.Helper()
+				var got TuplesDecode
+				if err := sc.Decode(strings.NewReader(data), &got); err != nil {
+					t.Fatalf("decode error: %v", err)
+				}
+				if !reflect.DeepEqual(got.Color, []int{255, 128, 0}) {
+					t.Errorf("Color: got %v, want [255 128 0]", got.Color)
+				}
+				if !reflect.DeepEqual(got.Position, []float64{1.5, 2.7}) {
+					t.Errorf("Position: got %v, want [1.5 2.7]", got.Position)
+				}
+				if len(got.Tag) != 2 {
+					t.Fatalf("Tag: got len %d, want 2", len(got.Tag))
+				}
+				if got.Tag[0] != "priority" {
+					t.Errorf("Tag[0]: got %v, want %q", got.Tag[0], "priority")
+				}
+				switch v := got.Tag[1].(type) {
+				case int64:
+					if v != 1 {
+						t.Errorf("Tag[1]: got %d, want 1", v)
+					}
+				case float64:
+					if v != 1 {
+						t.Errorf("Tag[1]: got %g, want 1", v)
+					}
+				default:
+					t.Errorf("Tag[1]: unexpected type %T (value %v)", got.Tag[1], got.Tag[1])
+				}
+			},
+		},
+
+		// jsonnet/functions
+		{
+			name:   "jsonnet/functions",
+			schema: "testdata/jsonnet/functions/schema.cue",
+			data:   "testdata/jsonnet/functions/data.jsonnet",
+			check: decode([]FunctionUserDecode{
+				{Name: "Alice", Greeting: "Hello, Alice!", Age: 30},
+				{Name: "Bob", Greeting: "Hello, Bob!", Age: 25},
+				{Name: "Carol", Greeting: "Hello, Carol!", Age: 35},
+			}),
+		},
+
+		// jsonnet/comprehensions
+		{
+			name:   "jsonnet/comprehensions",
+			schema: "testdata/jsonnet/comprehensions/schema.cue",
+			data:   "testdata/jsonnet/comprehensions/data.jsonnet",
+			check: decode([]ComprehensionItemDecode{
+				{Index: 0, Squared: 1, Label: "item-1"},
+				{Index: 1, Squared: 4, Label: "item-2"},
+				{Index: 2, Squared: 9, Label: "item-3"},
+				{Index: 3, Squared: 16, Label: "item-4"},
+				{Index: 4, Squared: 25, Label: "item-5"},
+			}),
+		},
+
+		// jsonnet/conditionals
+		{
+			name:   "jsonnet/conditionals",
+			schema: "testdata/jsonnet/conditionals/schema.cue",
+			data:   "testdata/jsonnet/conditionals/data.jsonnet",
+			check: decode([]ConditionalEntryDecode{
+				{Value: 3, Category: "small", Doubled: 6},
+				{Value: 42, Category: "medium", Doubled: 84},
+				{Value: 150, Category: "large", Doubled: 300},
+			}),
+		},
+
+		// jsonnet/inheritance
+		{
+			name:   "jsonnet/inheritance",
+			schema: "testdata/jsonnet/inheritance/schema.cue",
+			data:   "testdata/jsonnet/inheritance/data.jsonnet",
+			check:  decode(InheritanceDecode{Kind: "Deployment", Version: "v1", Name: "my-app", Replicas: 3, Image: "myapp:latest"}),
+		},
+
+		// jsonnet/multiline
+		{
+			name:   "jsonnet/multiline",
+			schema: "testdata/jsonnet/multiline/schema.cue",
+			data:   "testdata/jsonnet/multiline/data.jsonnet",
+			check: decode(MultilineDecode{
+				Description: "This is a multi-line\nstring in Jsonnet.\nIt supports indentation.\n",
+				Template:    "Name: my-app\nVersion: v1.0\n",
+			}),
+		},
+
+		// jsonnet/self_reference
+		{
+			name:   "jsonnet/self_reference",
+			schema: "testdata/jsonnet/self_reference/schema.cue",
+			data:   "testdata/jsonnet/self_reference/data.jsonnet",
+			check:  decode(SelfRefDecode{Width: 10, Height: 20, Area: 200, Perimeter: 60}),
+		},
+
+		// jsonnet/object_comprehension
+		// Schema is [string]: int; decode directly into map[string]int.
+		{
+			name:   "jsonnet/object_comprehension",
+			schema: "testdata/jsonnet/object_comprehension/schema.cue",
+			data:   "testdata/jsonnet/object_comprehension/data.jsonnet",
+			check:  decode(map[string]int{"alice_score": 95, "bob_score": 87, "carol_score": 92}),
+		},
+
+		// jsonnet/stdlib
+		{
+			name:   "jsonnet/stdlib",
+			schema: "testdata/jsonnet/stdlib/schema.cue",
+			data:   "testdata/jsonnet/stdlib/data.jsonnet",
+			check: decode(JStdlibDecode{
+				Upper:     "HELLO",
+				Joined:    "hello, world, jsonnet",
+				Formatted: "count=3, first=hello",
+				Count:     3,
+				Words:     []string{"hello", "world", "jsonnet"},
+				Mapped:    []string{"HELLO", "WORLD", "JSONNET"},
+				Filtered:  []string{"jsonnet"},
+			}),
+		},
+	}
+
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			t.Parallel()
+			tc.check(t, cuesonnet.Schema(mustReadFile(t, tc.schema)), mustReadFile(t, tc.data))
+		})
+	}
+}
M cuesonnet_test.go -> cuesonnet_test.go
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
diff --git a/cuesonnet_test.go b/cuesonnet_test.go
index 8f52a14d70c2e021ca7763fc7ada029882651bb9..9c835d68e45956b24236d2e0d452a01771dce5dc 100644
--- a/cuesonnet_test.go
+++ b/cuesonnet_test.go
@@ -1,105 +1,363 @@
 package cuesonnet_test
 
 import (
-	_ "embed"
+	"os"
 	"strings"
 	"testing"
 
 	"go.jolheiser.com/cuesonnet"
 )
 
-var (
-	//go:embed testdata/schema.cue
-	schema cuesonnet.Schema
-	//go:embed testdata/schema_list.cue
-	schemaList cuesonnet.Schema
-	//go:embed testdata/config.jsonnet
-	config string
-	//go:embed testdata/almost-fixed.jsonnet
-	almostFixed string
-	//go:embed testdata/fixed.jsonnet
-	fixed string
-	//go:embed testdata/kitchen.jsonnet
-	kitchen string
-	//go:embed testdata/kitchen_list.jsonnet
-	kitchenList string
-)
+type testCase struct {
+	name        string
+	schema      string
+	data        string
+	wantErr     bool
+	errContains []string
+}
 
-type Schema struct {
-	FirstName string `json:"firstName"`
-	LastName  string `json:"lastName"`
-	Age       int    `json:"age"`
-	Birthday  string `json:"birthday"`
-	Gopher    bool   `json:"gopher"`
+var cases = []testCase{
+	// person
+	{
+		name:        "person/invalid",
+		schema:      "testdata/person/schema.cue",
+		data:        "testdata/person/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"firstName", "age"},
+	},
+	{
+		name:        "person/almost_valid",
+		schema:      "testdata/person/schema.cue",
+		data:        "testdata/person/almost_valid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"LastName"},
+	},
+	{
+		name:   "person/valid",
+		schema: "testdata/person/schema.cue",
+		data:   "testdata/person/valid.jsonnet",
+	},
+	{
+		name:   "person/functions",
+		schema: "testdata/person/schema.cue",
+		data:   "testdata/person/functions.jsonnet",
+	},
+	{
+		name:   "person/list",
+		schema: "testdata/person/schema_list.cue",
+		data:   "testdata/person/functions_list.jsonnet",
+	},
+
+	// primitives
+	{
+		name:   "primitives/valid",
+		schema: "testdata/primitives/schema.cue",
+		data:   "testdata/primitives/valid.jsonnet",
+	},
+	{
+		name:        "primitives/invalid",
+		schema:      "testdata/primitives/schema.cue",
+		data:        "testdata/primitives/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"str", "num", "dec", "flag", "qty"},
+	},
+
+	// constraints
+	{
+		name:   "constraints/valid",
+		schema: "testdata/constraints/schema.cue",
+		data:   "testdata/constraints/valid.jsonnet",
+	},
+	{
+		name:        "constraints/invalid_range",
+		schema:      "testdata/constraints/schema.cue",
+		data:        "testdata/constraints/invalid_range.jsonnet",
+		wantErr:     true,
+		errContains: []string{"age", "score", "level"},
+	},
+	{
+		name:        "constraints/invalid_pattern",
+		schema:      "testdata/constraints/schema.cue",
+		data:        "testdata/constraints/invalid_pattern.jsonnet",
+		wantErr:     true,
+		errContains: []string{"username", "bio", "slug"},
+	},
+
+	// structs/any
+	{
+		name:   "structs/any/valid_object",
+		schema: "testdata/structs/any/schema.cue",
+		data:   "testdata/structs/any/valid_object.jsonnet",
+	},
+	{
+		name:   "structs/any/valid_scalar",
+		schema: "testdata/structs/any/schema.cue",
+		data:   "testdata/structs/any/valid_scalar.jsonnet",
+	},
+	{
+		name:        "structs/any/invalid",
+		schema:      "testdata/structs/any/schema.cue",
+		data:        "testdata/structs/any/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"name"},
+	},
+
+	// structs/closed
+	{
+		name:   "structs/closed/valid",
+		schema: "testdata/structs/closed/schema.cue",
+		data:   "testdata/structs/closed/valid.jsonnet",
+	},
+	{
+		name:        "structs/closed/invalid",
+		schema:      "testdata/structs/closed/schema.cue",
+		data:        "testdata/structs/closed/invalid_extra.jsonnet",
+		wantErr:     true,
+		errContains: []string{"email"},
+	},
+
+	// structs/optional
+	{
+		name:   "structs/optional/full",
+		schema: "testdata/structs/optional/schema.cue",
+		data:   "testdata/structs/optional/valid_full.jsonnet",
+	},
+	{
+		name:   "structs/optional/minimal",
+		schema: "testdata/structs/optional/schema.cue",
+		data:   "testdata/structs/optional/valid_minimal.jsonnet",
+	},
+	{
+		name:        "structs/optional/invalid",
+		schema:      "testdata/structs/optional/schema.cue",
+		data:        "testdata/structs/optional/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"email"},
+	},
+
+	// structs/nested
+	{
+		name:   "structs/nested/valid",
+		schema: "testdata/structs/nested/schema.cue",
+		data:   "testdata/structs/nested/valid.jsonnet",
+	},
+	{
+		name:        "structs/nested/invalid",
+		schema:      "testdata/structs/nested/schema.cue",
+		data:        "testdata/structs/nested/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"zip", "value"},
+	},
+
+	// lists
+	{
+		name:   "lists/valid",
+		schema: "testdata/lists/schema.cue",
+		data:   "testdata/lists/valid.jsonnet",
+	},
+	{
+		name:        "lists/invalid_element",
+		schema:      "testdata/lists/schema.cue",
+		data:        "testdata/lists/invalid_element.jsonnet",
+		wantErr:     true,
+		errContains: []string{"id", "label"},
+	},
+	{
+		name:        "lists/invalid_length",
+		schema:      "testdata/lists/schema.cue",
+		data:        "testdata/lists/invalid_length.jsonnet",
+		wantErr:     true,
+		errContains: []string{"limited"},
+	},
+
+	// disjunctions
+	{
+		name:   "disjunctions/valid_a",
+		schema: "testdata/disjunctions/schema.cue",
+		data:   "testdata/disjunctions/valid_a.jsonnet",
+	},
+	{
+		name:   "disjunctions/valid_b",
+		schema: "testdata/disjunctions/schema.cue",
+		data:   "testdata/disjunctions/valid_b.jsonnet",
+	},
+	{
+		name:        "disjunctions/invalid",
+		schema:      "testdata/disjunctions/schema.cue",
+		data:        "testdata/disjunctions/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"mode", "priority"},
+	},
+
+	// defaults
+	{
+		name:   "defaults/minimal",
+		schema: "testdata/defaults/schema.cue",
+		data:   "testdata/defaults/valid_minimal.jsonnet",
+	},
+	{
+		name:   "defaults/override",
+		schema: "testdata/defaults/schema.cue",
+		data:   "testdata/defaults/valid_override.jsonnet",
+	},
+
+	// enums
+	{
+		name:   "enums/valid",
+		schema: "testdata/enums/schema.cue",
+		data:   "testdata/enums/valid.jsonnet",
+	},
+	{
+		name:        "enums/invalid",
+		schema:      "testdata/enums/schema.cue",
+		data:        "testdata/enums/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"color", "status", "priority"},
+	},
+
+	// definitions
+	{
+		name:   "definitions/valid",
+		schema: "testdata/definitions/schema.cue",
+		data:   "testdata/definitions/valid.jsonnet",
+	},
+	{
+		name:        "definitions/invalid",
+		schema:      "testdata/definitions/schema.cue",
+		data:        "testdata/definitions/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"age", "email", "phone"},
+	},
+
+	// stdlib (time, strings, list, math packages)
+	{
+		name:   "stdlib/valid",
+		schema: "testdata/stdlib/schema.cue",
+		data:   "testdata/stdlib/valid.jsonnet",
+	},
+	{
+		name:        "stdlib/invalid",
+		schema:      "testdata/stdlib/schema.cue",
+		data:        "testdata/stdlib/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"createdAt", "birthday", "username", "tags", "initials", "angle"},
+	},
+
+	// composition (definition embedding, let bindings)
+	{
+		name:   "composition/valid",
+		schema: "testdata/composition/schema.cue",
+		data:   "testdata/composition/valid.jsonnet",
+	},
+	{
+		name:        "composition/invalid",
+		schema:      "testdata/composition/schema.cue",
+		data:        "testdata/composition/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"updatedAt", "age"},
+	},
+
+	// patterns ([string]: type pattern constraints)
+	{
+		name:   "patterns/valid",
+		schema: "testdata/patterns/schema.cue",
+		data:   "testdata/patterns/valid.jsonnet",
+	},
+	{
+		name:        "patterns/invalid",
+		schema:      "testdata/patterns/schema.cue",
+		data:        "testdata/patterns/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"labels", "counts"},
+	},
+
+	// tuples (fixed-length positional lists)
+	{
+		name:   "tuples/valid",
+		schema: "testdata/tuples/schema.cue",
+		data:   "testdata/tuples/valid.jsonnet",
+	},
+	{
+		name:        "tuples/invalid",
+		schema:      "testdata/tuples/schema.cue",
+		data:        "testdata/tuples/invalid.jsonnet",
+		wantErr:     true,
+		errContains: []string{"color", "position", "tag"},
+	},
+
+	// jsonnet features
+	{
+		name:   "jsonnet/functions",
+		schema: "testdata/jsonnet/functions/schema.cue",
+		data:   "testdata/jsonnet/functions/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/comprehensions",
+		schema: "testdata/jsonnet/comprehensions/schema.cue",
+		data:   "testdata/jsonnet/comprehensions/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/object_comprehension",
+		schema: "testdata/jsonnet/object_comprehension/schema.cue",
+		data:   "testdata/jsonnet/object_comprehension/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/conditionals",
+		schema: "testdata/jsonnet/conditionals/schema.cue",
+		data:   "testdata/jsonnet/conditionals/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/inheritance",
+		schema: "testdata/jsonnet/inheritance/schema.cue",
+		data:   "testdata/jsonnet/inheritance/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/multiline",
+		schema: "testdata/jsonnet/multiline/schema.cue",
+		data:   "testdata/jsonnet/multiline/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/self_reference",
+		schema: "testdata/jsonnet/self_reference/schema.cue",
+		data:   "testdata/jsonnet/self_reference/data.jsonnet",
+	},
+	{
+		name:   "jsonnet/stdlib",
+		schema: "testdata/jsonnet/stdlib/schema.cue",
+		data:   "testdata/jsonnet/stdlib/data.jsonnet",
+	},
 }
 
 func TestCUEsonnet(t *testing.T) {
-	t.Run("config", func(t *testing.T) {
-		t.Parallel()
-		var s Schema
-		err := schema.Decode(strings.NewReader(config), &s)
-		for _, s := range []string{"2", "firstName", "age"} {
-			if !strings.Contains(err.Error(), s) {
-				t.Logf("did not find %q in error", s)
-				t.Fail()
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			t.Parallel()
+			sc := cuesonnet.Schema(mustReadFile(t, tc.schema))
+			var v any
+			err := sc.Decode(strings.NewReader(mustReadFile(t, tc.data)), &v)
+			if tc.wantErr {
+				if err == nil {
+					t.Fatal("expected error, got nil")
+				}
+				for _, sub := range tc.errContains {
+					if !strings.Contains(err.Error(), sub) {
+						t.Errorf("error missing %q\n  full error: %v", sub, err)
+					}
+				}
+				return
 			}
-		}
-	})
-
-	t.Run("almost-fixed", func(t *testing.T) {
-		t.Parallel()
-		var s Schema
-		err := schema.Decode(strings.NewReader(almostFixed), &s)
-		for _, s := range []string{"1", "LastName"} {
-			if !strings.Contains(err.Error(), s) {
-				t.Logf("did not find %q in error: %q", s, err)
-				t.Fail()
+			if err != nil {
+				t.Fatalf("unexpected error: %v", err)
 			}
-		}
-	})
-
-	expect := Schema{
-		FirstName: "Jim",
-		LastName:  "Jimbly",
-		Age:       45,
-		Birthday:  "01/02/2003",
-		Gopher:    true,
+		})
 	}
-	t.Run("fixed", func(t *testing.T) {
-		t.Parallel()
-		var s Schema
-		if err := schema.Decode(strings.NewReader(fixed), &s); err != nil {
-			t.Logf("fixed config should not fail to decode: %v", err)
-			t.Fail()
-		}
-		if s != expect {
-			t.Logf("did not get expected decoding: %+v != %+v", s, expect)
-			t.Fail()
-		}
-	})
-	t.Run("kitchen", func(t *testing.T) {
-		t.Parallel()
-		var s Schema
-		if err := schema.Decode(strings.NewReader(kitchen), &s); err != nil {
-			t.Logf("kitchen config should not fail to decode: %v", err)
-			t.Fail()
-		}
-		if s != expect {
-			t.Logf("did not get expected decoding: %+v != %+v", s, expect)
-			t.Fail()
-		}
-	})
+}
 
-	t.Run("list", func(t *testing.T) {
-		t.Parallel()
-		var s []Schema
-		if err := schemaList.Decode(strings.NewReader(kitchenList), &s); err != nil {
-			t.Logf("kitchen_list config should not fail to decode: %v", err)
-			t.Fail()
-		}
-		if s[0] != expect {
-			t.Logf("did not get expected decoding: %+v != %+v", s, expect)
-			t.Fail()
-		}
-	})
+func mustReadFile(t *testing.T, path string) string {
+	t.Helper()
+	b, err := os.ReadFile(path)
+	if err != nil {
+		t.Fatalf("read %s: %v", path, err)
+	}
+	return string(b)
 }
M testdata/almost-fixed.jsonnet -> testdata/person/almost_valid.jsonnet
1
2
3
diff --git a/testdata/almost-fixed.jsonnet b/testdata/person/almost_valid.jsonnet
rename from testdata/almost-fixed.jsonnet
rename to testdata/person/almost_valid.jsonnet
I testdata/composition/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
diff --git a/testdata/composition/invalid.jsonnet b/testdata/composition/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..4780607208c5030b9348c8748ccb5630c2bc4868
--- /dev/null
+++ b/testdata/composition/invalid.jsonnet
@@ -0,0 +1,9 @@
+[
+  {
+    firstName: 'Alice',
+    lastName: 'Smith',
+    createdAt: '2024-01-15T10:00:00Z',
+    updatedAt: 'not-a-timestamp',
+    age: 200,
+  },
+]
I testdata/composition/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
diff --git a/testdata/composition/schema.cue b/testdata/composition/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..de5130045a7bac8718dbc3ffa645a50b1b1f000f
--- /dev/null
+++ b/testdata/composition/schema.cue
@@ -0,0 +1,23 @@
+import "time"
+
+let MaxAge = 150
+
+#Named: {
+	firstName: string
+	lastName:  string
+}
+
+#Timestamped: {
+	createdAt: time.Format(time.RFC3339)
+	updatedAt: time.Format(time.RFC3339)
+}
+
+// Embed definitions inside the body to compose them with new fields.
+// & between two closed definitions cannot add new fields; embedding can.
+#Person: {
+	#Named
+	#Timestamped
+	age: int & >=0 & <=MaxAge
+}
+
+[...#Person]
I testdata/composition/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
diff --git a/testdata/composition/valid.jsonnet b/testdata/composition/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..2a063ddae0fa367e38cfa15b853ccacd9189a26b
--- /dev/null
+++ b/testdata/composition/valid.jsonnet
@@ -0,0 +1,16 @@
+[
+  {
+    firstName: 'Alice',
+    lastName: 'Smith',
+    createdAt: '2024-01-15T10:00:00Z',
+    updatedAt: '2024-06-01T12:00:00Z',
+    age: 30,
+  },
+  {
+    firstName: 'Bob',
+    lastName: 'Jones',
+    createdAt: '2023-05-20T08:30:00Z',
+    updatedAt: '2023-05-20T08:30:00Z',
+    age: 25,
+  },
+]
M testdata/config.jsonnet -> testdata/person/invalid.jsonnet
1
2
3
diff --git a/testdata/config.jsonnet b/testdata/person/invalid.jsonnet
rename from testdata/config.jsonnet
rename to testdata/person/invalid.jsonnet
I testdata/constraints/invalid_pattern.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/constraints/invalid_pattern.jsonnet b/testdata/constraints/invalid_pattern.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..dc09400648a89ff529cbd473599dd34be10db721
--- /dev/null
+++ b/testdata/constraints/invalid_pattern.jsonnet
@@ -0,0 +1,8 @@
+{
+  age: 30,
+  score: 50.0,
+  username: 'John Doe',
+  bio: '',
+  level: 3,
+  slug: 'Has Spaces!',
+}
I testdata/constraints/invalid_range.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/constraints/invalid_range.jsonnet b/testdata/constraints/invalid_range.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..5176e1c5e506a8aa120ebc180c332b163ba3ffa1
--- /dev/null
+++ b/testdata/constraints/invalid_range.jsonnet
@@ -0,0 +1,8 @@
+{
+  age: -1,
+  score: 101.0,
+  username: 'john_doe',
+  bio: 'A software engineer.',
+  level: 0,
+  slug: 'valid-slug',
+}
I testdata/constraints/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
diff --git a/testdata/constraints/schema.cue b/testdata/constraints/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..5fd3d5f853d5635e6cf46d7ecbca8e525838d342
--- /dev/null
+++ b/testdata/constraints/schema.cue
@@ -0,0 +1,11 @@
+import "strings"
+
+#Schema: {
+	age:      int & >=0 & <=150
+	score:    float & >=0.0 & <=100.0
+	username: string & =~"^[a-z][a-z0-9_]{1,19}$"
+	bio:      string & strings.MinRunes(1) & strings.MaxRunes(200)
+	level:    int & !=0
+	slug:     string & !~"[^a-z0-9-]"
+}
+#Schema
I testdata/constraints/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/constraints/valid.jsonnet b/testdata/constraints/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..7ec10540399c856e816d17d91ee7e0f85dc3b44b
--- /dev/null
+++ b/testdata/constraints/valid.jsonnet
@@ -0,0 +1,8 @@
+{
+  age: 30,
+  score: 95.5,
+  username: 'john_doe',
+  bio: 'A software engineer.',
+  level: 5,
+  slug: 'my-slug',
+}
I testdata/defaults/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/defaults/schema.cue b/testdata/defaults/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..d0a16302c240ac5fe3be6ea147a9a29b130a957a
--- /dev/null
+++ b/testdata/defaults/schema.cue
@@ -0,0 +1,8 @@
+#Schema: {
+	name:    string
+	role:    "admin" | "user" | "guest" | *"user"
+	enabled: bool | *true
+	timeout: int | *30
+	tags:    [...string] | *[]
+}
+#Schema
I testdata/defaults/valid_minimal.jsonnet
1
2
3
4
5
6
7
8
9
diff --git a/testdata/defaults/valid_minimal.jsonnet b/testdata/defaults/valid_minimal.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..d50010d13b8ca5671f7769ebea3e66ac96f82eb0
--- /dev/null
+++ b/testdata/defaults/valid_minimal.jsonnet
@@ -0,0 +1,3 @@
+{
+  name: 'Alice',
+}
I testdata/defaults/valid_override.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
diff --git a/testdata/defaults/valid_override.jsonnet b/testdata/defaults/valid_override.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..6dbd7f7d47b3b87ce86a0031960348a64c38539f
--- /dev/null
+++ b/testdata/defaults/valid_override.jsonnet
@@ -0,0 +1,7 @@
+{
+  name: 'Alice',
+  role: 'admin',
+  enabled: false,
+  timeout: 60,
+  tags: ['api', 'v2'],
+}
I testdata/definitions/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
diff --git a/testdata/definitions/invalid.jsonnet b/testdata/definitions/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..89dec2f25a933c1a00a0384102957ac87bcd0811
--- /dev/null
+++ b/testdata/definitions/invalid.jsonnet
@@ -0,0 +1,12 @@
+[
+  {
+    name: { first: 'Alice', last: 'Smith' },
+    age: -1,
+    contact: { email: 'not-an-email' },
+  },
+  {
+    name: { first: 'Bob' },
+    age: 25,
+    contact: { phone: '5551234567' },
+  },
+]
I testdata/definitions/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
diff --git a/testdata/definitions/schema.cue b/testdata/definitions/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..a24508c56723ac43c24b66e1827357dfb476eb2c
--- /dev/null
+++ b/testdata/definitions/schema.cue
@@ -0,0 +1,17 @@
+#Name: {
+	first: string
+	last:  string
+}
+
+#Contact: {
+	email?: =~"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"
+	phone?: =~"^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
+}
+
+#Person: {
+	name:    #Name
+	age:     int & >=0
+	contact: #Contact
+}
+
+[...#Person]
I testdata/definitions/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
diff --git a/testdata/definitions/valid.jsonnet b/testdata/definitions/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..b17d4e1025ff148a269f96608d740f567201eee1
--- /dev/null
+++ b/testdata/definitions/valid.jsonnet
@@ -0,0 +1,17 @@
+[
+  {
+    name: { first: 'Alice', last: 'Smith' },
+    age: 30,
+    contact: { email: 'alice@example.com' },
+  },
+  {
+    name: { first: 'Bob', last: 'Jones' },
+    age: 25,
+    contact: { phone: '555-123-4567' },
+  },
+  {
+    name: { first: 'Carol', last: 'White' },
+    age: 42,
+    contact: { email: 'carol@example.com', phone: '123-456-7890' },
+  },
+]
I testdata/disjunctions/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/disjunctions/invalid.jsonnet b/testdata/disjunctions/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..c77e81450b4a8b5795f5c954f929008912a1ab1a
--- /dev/null
+++ b/testdata/disjunctions/invalid.jsonnet
@@ -0,0 +1,6 @@
+{
+  value: 3.14,
+  mode: 'exec',
+  priority: 5,
+  nullable: 42,
+}
I testdata/disjunctions/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/testdata/disjunctions/schema.cue b/testdata/disjunctions/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..4ed8057441b22dc508926a16f14427e0061f7d24
--- /dev/null
+++ b/testdata/disjunctions/schema.cue
@@ -0,0 +1,14 @@
+#Schema: {
+	// Tagged union: string, int, or bool
+	value: string | int | bool
+
+	// String enum (disjunction of literals)
+	mode: "read" | "write" | "readwrite"
+
+	// Integer enum
+	priority: 1 | 2 | 3
+
+	// Nullable field
+	nullable: string | null
+}
+#Schema
I testdata/disjunctions/valid_a.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/disjunctions/valid_a.jsonnet b/testdata/disjunctions/valid_a.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..a70f88a0bc29d4cd8c1396b19763c45466f6bdca
--- /dev/null
+++ b/testdata/disjunctions/valid_a.jsonnet
@@ -0,0 +1,6 @@
+{
+  value: 'text',
+  mode: 'read',
+  priority: 1,
+  nullable: 'present',
+}
I testdata/disjunctions/valid_b.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/disjunctions/valid_b.jsonnet b/testdata/disjunctions/valid_b.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..b7f4f87f26e5e9e008becb72b7ae1ef7322a4543
--- /dev/null
+++ b/testdata/disjunctions/valid_b.jsonnet
@@ -0,0 +1,6 @@
+{
+  value: 42,
+  mode: 'write',
+  priority: 3,
+  nullable: null,
+}
I testdata/enums/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/enums/invalid.jsonnet b/testdata/enums/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..f80b92ccf8ce39eb5b56ad27c1b8a116a28ee0ef
--- /dev/null
+++ b/testdata/enums/invalid.jsonnet
@@ -0,0 +1,6 @@
+{
+  color: 'purple',
+  status: 'deleted',
+  priority: 6,
+  flags: ['a', 'd'],
+}
I testdata/enums/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
diff --git a/testdata/enums/schema.cue b/testdata/enums/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..dd8b91b1068bc4fced0ea2176f4f5e6f65419d48
--- /dev/null
+++ b/testdata/enums/schema.cue
@@ -0,0 +1,11 @@
+#Color:    "red" | "green" | "blue"
+#Status:   "active" | "inactive" | "pending"
+#Priority: 1 | 2 | 3 | 4 | 5
+
+#Schema: {
+	color:    #Color
+	status:   #Status
+	priority: #Priority
+	flags:    [...("a" | "b" | "c")]
+}
+#Schema
I testdata/enums/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/enums/valid.jsonnet b/testdata/enums/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..4b1c60021c02f77a2bb8378b70bdc7c9aaa46448
--- /dev/null
+++ b/testdata/enums/valid.jsonnet
@@ -0,0 +1,6 @@
+{
+  color: 'red',
+  status: 'active',
+  priority: 3,
+  flags: ['a', 'b'],
+}
M testdata/fixed.jsonnet -> testdata/person/valid.jsonnet
1
2
3
diff --git a/testdata/fixed.jsonnet b/testdata/person/valid.jsonnet
rename from testdata/fixed.jsonnet
rename to testdata/person/valid.jsonnet
I testdata/jsonnet/comprehensions/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
diff --git a/testdata/jsonnet/comprehensions/data.jsonnet b/testdata/jsonnet/comprehensions/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..76719de64ca5e56515794e184319e2573b968ce1
--- /dev/null
+++ b/testdata/jsonnet/comprehensions/data.jsonnet
@@ -0,0 +1,10 @@
+local nums = [1, 2, 3, 4, 5];
+
+[
+  {
+    index: n - 1,
+    squared: n * n,
+    label: 'item-' + std.toString(n),
+  }
+  for n in nums
+]
I testdata/jsonnet/comprehensions/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/jsonnet/comprehensions/schema.cue b/testdata/jsonnet/comprehensions/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..951d5b7f89e50e70fbd2d990bf9c22eda9d51784
--- /dev/null
+++ b/testdata/jsonnet/comprehensions/schema.cue
@@ -0,0 +1,6 @@
+#Item: {
+	index:   int & >=0
+	squared: int & >=0
+	label:   string
+}
+[...#Item]
I testdata/jsonnet/conditionals/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
diff --git a/testdata/jsonnet/conditionals/data.jsonnet b/testdata/jsonnet/conditionals/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..6f2eb25a8b8c35420f5d457f0d85978a13d25bb7
--- /dev/null
+++ b/testdata/jsonnet/conditionals/data.jsonnet
@@ -0,0 +1,15 @@
+local classify = function(n)
+  if n < 10 then 'small'
+  else if n < 100 then 'medium'
+  else 'large';
+
+local nums = [3, 42, 150];
+
+[
+  {
+    value: n,
+    category: classify(n),
+    doubled: n * 2,
+  }
+  for n in nums
+]
I testdata/jsonnet/conditionals/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/jsonnet/conditionals/schema.cue b/testdata/jsonnet/conditionals/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..03b6ad2455eff2dc1729e2dc44c703e5052947a3
--- /dev/null
+++ b/testdata/jsonnet/conditionals/schema.cue
@@ -0,0 +1,6 @@
+#Entry: {
+	value:    int
+	category: "small" | "medium" | "large"
+	doubled:  int
+}
+[...#Entry]
I testdata/jsonnet/functions/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
diff --git a/testdata/jsonnet/functions/data.jsonnet b/testdata/jsonnet/functions/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..901ec9ab4b75e4e5262da635447218cdf6f74d35
--- /dev/null
+++ b/testdata/jsonnet/functions/data.jsonnet
@@ -0,0 +1,13 @@
+local greet = function(name) 'Hello, ' + name + '!';
+
+local makeUser = function(name, age) {
+  name: name,
+  greeting: greet(name),
+  age: age,
+};
+
+[
+  makeUser('Alice', 30),
+  makeUser('Bob', 25),
+  makeUser('Carol', 35),
+]
I testdata/jsonnet/functions/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/jsonnet/functions/schema.cue b/testdata/jsonnet/functions/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..41ba021d2da2f5eccd4207800b2c1e5795f8d094
--- /dev/null
+++ b/testdata/jsonnet/functions/schema.cue
@@ -0,0 +1,6 @@
+#User: {
+	name:     string
+	greeting: string
+	age:      int & >=0
+}
+[...#User]
I testdata/jsonnet/inheritance/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
diff --git a/testdata/jsonnet/inheritance/data.jsonnet b/testdata/jsonnet/inheritance/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..b85eb4f1a7d1da97977bac73f0163c83e15ce710
--- /dev/null
+++ b/testdata/jsonnet/inheritance/data.jsonnet
@@ -0,0 +1,13 @@
+local base = {
+  kind: 'Deployment',
+  version: 'v1',
+  replicas: 1,
+};
+
+local production = base + {
+  name: 'my-app',
+  replicas: 3,
+  image: 'myapp:latest',
+};
+
+production
I testdata/jsonnet/inheritance/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/jsonnet/inheritance/schema.cue b/testdata/jsonnet/inheritance/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..3160904bdc68d7b516c732bc00ea725bd020ebc0
--- /dev/null
+++ b/testdata/jsonnet/inheritance/schema.cue
@@ -0,0 +1,8 @@
+#Schema: {
+	kind:     string
+	version:  string
+	name:     string
+	replicas: int & >=1
+	image:    string
+}
+#Schema
I testdata/jsonnet/multiline/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
diff --git a/testdata/jsonnet/multiline/data.jsonnet b/testdata/jsonnet/multiline/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..d20f01a6971df4cb54f7b45ef5e2278c9ae2cb9b
--- /dev/null
+++ b/testdata/jsonnet/multiline/data.jsonnet
@@ -0,0 +1,15 @@
+local description = |||
+    This is a multi-line
+    string in Jsonnet.
+    It supports indentation.
+|||;
+
+local template = |||
+    Name: %s
+    Version: %s
+||| % ['my-app', 'v1.0'];
+
+{
+  description: description,
+  template: template,
+}
I testdata/jsonnet/multiline/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
diff --git a/testdata/jsonnet/multiline/schema.cue b/testdata/jsonnet/multiline/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..f60a8a8a6f18da7b8c54d2d26e028937837edbb2
--- /dev/null
+++ b/testdata/jsonnet/multiline/schema.cue
@@ -0,0 +1,5 @@
+#Schema: {
+	description: string
+	template:    string
+}
+#Schema
I testdata/jsonnet/object_comprehension/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/jsonnet/object_comprehension/data.jsonnet b/testdata/jsonnet/object_comprehension/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..97f1f8164e91b1b79a40c4af753fa1b9a4b5e114
--- /dev/null
+++ b/testdata/jsonnet/object_comprehension/data.jsonnet
@@ -0,0 +1,6 @@
+local scores = { alice: 95, bob: 87, carol: 92 };
+
+{
+  [k + '_score']: scores[k]
+  for k in std.objectFields(scores)
+}
I testdata/jsonnet/object_comprehension/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
diff --git a/testdata/jsonnet/object_comprehension/schema.cue b/testdata/jsonnet/object_comprehension/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..48b3cd92c13c35c51c423b408807cedb79879a21
--- /dev/null
+++ b/testdata/jsonnet/object_comprehension/schema.cue
@@ -0,0 +1,5 @@
+// Pattern constraint validates an object with arbitrary string keys.
+#Schema: {
+	[string]: int & >=0
+}
+#Schema
I testdata/jsonnet/self_reference/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/jsonnet/self_reference/data.jsonnet b/testdata/jsonnet/self_reference/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..f88118c63d638b61b8e304f2bffdf7efd3aa60f5
--- /dev/null
+++ b/testdata/jsonnet/self_reference/data.jsonnet
@@ -0,0 +1,6 @@
+{
+  width: 10,
+  height: 20,
+  area: self.width * self.height,
+  perimeter: 2 * (self.width + self.height),
+}
I testdata/jsonnet/self_reference/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
diff --git a/testdata/jsonnet/self_reference/schema.cue b/testdata/jsonnet/self_reference/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..30913447c3c57fac7c7539f54f8fb3150f97e3b9
--- /dev/null
+++ b/testdata/jsonnet/self_reference/schema.cue
@@ -0,0 +1,7 @@
+#Schema: {
+	width:     int & >0
+	height:    int & >0
+	area:      int & >0
+	perimeter: int & >0
+}
+#Schema
I testdata/jsonnet/stdlib/data.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
diff --git a/testdata/jsonnet/stdlib/data.jsonnet b/testdata/jsonnet/stdlib/data.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..87b37d2cc97b6f0cbbff8afb3fdc80f456f7338d
--- /dev/null
+++ b/testdata/jsonnet/stdlib/data.jsonnet
@@ -0,0 +1,11 @@
+local words = ['hello', 'world', 'jsonnet'];
+
+{
+  upper: std.asciiUpper(words[0]),
+  joined: std.join(', ', words),
+  formatted: std.format('count=%d, first=%s', [std.length(words), words[0]]),
+  count: std.length(words),
+  words: words,
+  mapped: std.map(function(w) std.asciiUpper(w), words),
+  filtered: std.filter(function(w) std.length(w) > 5, words),
+}
I testdata/jsonnet/stdlib/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
diff --git a/testdata/jsonnet/stdlib/schema.cue b/testdata/jsonnet/stdlib/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..d53cf96ae36adeda423fae36b25c33333dbfafc7
--- /dev/null
+++ b/testdata/jsonnet/stdlib/schema.cue
@@ -0,0 +1,10 @@
+#Schema: {
+	upper:     string
+	joined:    string
+	formatted: string
+	count:     int & >=0
+	words:     [...string]
+	mapped:    [...string]
+	filtered:  [...string]
+}
+#Schema
M testdata/kitchen.jsonnet -> testdata/person/functions.jsonnet
1
2
3
diff --git a/testdata/kitchen.jsonnet b/testdata/person/functions.jsonnet
rename from testdata/kitchen.jsonnet
rename to testdata/person/functions.jsonnet
M testdata/kitchen_list.jsonnet -> testdata/person/functions_list.jsonnet
1
2
3
diff --git a/testdata/kitchen_list.jsonnet b/testdata/person/functions_list.jsonnet
rename from testdata/kitchen_list.jsonnet
rename to testdata/person/functions_list.jsonnet
I testdata/lists/invalid_element.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
diff --git a/testdata/lists/invalid_element.jsonnet b/testdata/lists/invalid_element.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..3901ebdb253252ba4ca9a1a51b0a228b2f01ce94
--- /dev/null
+++ b/testdata/lists/invalid_element.jsonnet
@@ -0,0 +1,9 @@
+{
+  items: [
+    { id: 0, label: 'bad-id' },
+    { id: 1, label: 42 },
+  ],
+  tags: ['a', 2, 'c'],
+  counts: [1, -1, 3],
+  limited: ['ok'],
+}
I testdata/lists/invalid_length.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/lists/invalid_length.jsonnet b/testdata/lists/invalid_length.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..d3d2fa32964c8736aa6e23152bfb95b69fd78f4e
--- /dev/null
+++ b/testdata/lists/invalid_length.jsonnet
@@ -0,0 +1,6 @@
+{
+  items: [],
+  tags: [],
+  counts: [],
+  limited: ['a', 'b', 'c', 'd', 'e', 'f'],
+}
I testdata/lists/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/testdata/lists/schema.cue b/testdata/lists/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..ebae88dc81e9ecc9e1bbaa4e7ea3145d7105aa1b
--- /dev/null
+++ b/testdata/lists/schema.cue
@@ -0,0 +1,14 @@
+import "list"
+
+#Item: {
+	id:    int & >0
+	label: string
+}
+
+#Schema: {
+	items:   [...#Item]
+	tags:    [...string]
+	counts:  [...int & >=0]
+	limited: list.MinItems(1) & list.MaxItems(5) & [...string]
+}
+#Schema
I testdata/lists/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
diff --git a/testdata/lists/valid.jsonnet b/testdata/lists/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..d72d418575bd40936343d3301289af778770be4d
--- /dev/null
+++ b/testdata/lists/valid.jsonnet
@@ -0,0 +1,9 @@
+{
+  items: [
+    { id: 1, label: 'first' },
+    { id: 2, label: 'second' },
+  ],
+  tags: ['go', 'cue', 'jsonnet'],
+  counts: [0, 1, 2, 100],
+  limited: ['only', 'a', 'few'],
+}
I testdata/patterns/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
diff --git a/testdata/patterns/invalid.jsonnet b/testdata/patterns/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..b0b5c37a3383bd7af4786150595c127ba701698a
--- /dev/null
+++ b/testdata/patterns/invalid.jsonnet
@@ -0,0 +1,9 @@
+{
+  name: 'service-a',
+  labels: {
+    env: 42,
+  },
+  counts: {
+    requests: -1,
+  },
+}
I testdata/patterns/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
diff --git a/testdata/patterns/schema.cue b/testdata/patterns/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..2f50ff4c4e7f18f85e5f3c033fdb5b65af013403
--- /dev/null
+++ b/testdata/patterns/schema.cue
@@ -0,0 +1,7 @@
+// [string]: type constrains all values for arbitrary string keys.
+#Schema: {
+	name:   string
+	labels: {[string]: string}
+	counts: {[string]: int & >=0}
+}
+#Schema
I testdata/patterns/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
diff --git a/testdata/patterns/valid.jsonnet b/testdata/patterns/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..e410039a99161c9ce92467a11257884ee2205694
--- /dev/null
+++ b/testdata/patterns/valid.jsonnet
@@ -0,0 +1,13 @@
+{
+  name: 'service-a',
+  labels: {
+    env: 'production',
+    region: 'us-east-1',
+    team: 'platform',
+  },
+  counts: {
+    requests: 1000,
+    errors: 5,
+    warnings: 42,
+  },
+}
I testdata/primitives/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
diff --git a/testdata/primitives/invalid.jsonnet b/testdata/primitives/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..cf6d5776dcf749ad41fe84ebd52208f931ad6bd8
--- /dev/null
+++ b/testdata/primitives/invalid.jsonnet
@@ -0,0 +1,7 @@
+{
+  str: 99,
+  num: 'not-a-number',
+  dec: false,
+  flag: 'yes',
+  qty: 'not-a-number',
+}
I testdata/primitives/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/primitives/schema.cue b/testdata/primitives/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..17bc916f80bc35eba968ef272813ce3aba7854fa
--- /dev/null
+++ b/testdata/primitives/schema.cue
@@ -0,0 +1,8 @@
+#Schema: {
+	str:  string
+	num:  int
+	dec:  float
+	flag: bool
+	qty:  number
+}
+#Schema
I testdata/primitives/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
diff --git a/testdata/primitives/valid.jsonnet b/testdata/primitives/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..16ded5412a818dddf48578c1ca2a773996403660
--- /dev/null
+++ b/testdata/primitives/valid.jsonnet
@@ -0,0 +1,7 @@
+{
+  str: 'hello',
+  num: 42,
+  dec: 3.14,
+  flag: true,
+  qty: 2.718,
+}
M testdata/schema.cue -> testdata/person/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
diff --git a/testdata/schema.cue b/testdata/person/schema.cue
rename from testdata/schema.cue
rename to testdata/person/schema.cue
index b23b930621669bc096d552ba6cd728f851830175..c937dff1fe4dc431092f73c65db212ae60f28c30 100644
--- a/testdata/schema.cue
+++ b/testdata/person/schema.cue
@@ -1,20 +1,20 @@
 import "time"
 
 #Schema: {
-	// Basic schema
+	// Basic fields
 	firstName: string
 	lastName:  string
 	age:       int
 	birthday:  string
 
-	// Refine as needed
+	// Refined constraints
 	#Title:    =~"^[A-Z]" | error("must start with an uppercase letter")
 	firstName: #Title
 	lastName:  #Title
 	age:       >0
 	birthday:  time.Format("01/02/2006")
 
-	// Defaults
+	// Default values
 	gopher: bool | *true
 }
 
M testdata/schema_list.cue -> testdata/person/schema_list.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
diff --git a/testdata/schema_list.cue b/testdata/person/schema_list.cue
rename from testdata/schema_list.cue
rename to testdata/person/schema_list.cue
index 02024313163d96024bd64c4f01ef2dbd7e83b788..9abfedb7f42f9f0353e041601e04a9e891f13ec8 100644
--- a/testdata/schema_list.cue
+++ b/testdata/person/schema_list.cue
@@ -1,22 +1,22 @@
 import "time"
 
 #Schema: {
-	// Basic schema
+	// Basic fields
 	firstName: string
 	lastName:  string
 	age:       int
 	birthday:  string
 
-	// Refine as needed
+	// Refined constraints
 	#Title:    =~"^[A-Z]" | error("must start with an uppercase letter")
 	firstName: #Title
 	lastName:  #Title
 	age:       >0
 	birthday:  time.Format("01/02/2006")
 
-	// Defaults
+	// Default values
 	gopher: bool | *true
 }
 
-// Apply the schema to root
+// Apply the schema to a list
 [...#Schema]
I testdata/stdlib/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/stdlib/invalid.jsonnet b/testdata/stdlib/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..8ec396e931bf3c3822cf34d5f4d86f578bac13f1
--- /dev/null
+++ b/testdata/stdlib/invalid.jsonnet
@@ -0,0 +1,8 @@
+{
+  createdAt: 'not-a-timestamp',
+  birthday: '1990-01-15',
+  username: 'ab',
+  tags: [],
+  initials: 'abc',
+  angle: 7.0,
+}
I testdata/stdlib/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
diff --git a/testdata/stdlib/schema.cue b/testdata/stdlib/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..c5104d116494fc25b387bf9d7a399159d671c9a2
--- /dev/null
+++ b/testdata/stdlib/schema.cue
@@ -0,0 +1,16 @@
+import (
+	"time"
+	"strings"
+	"list"
+	"math"
+)
+
+#Schema: {
+	createdAt: time.Format(time.RFC3339)
+	birthday:  time.Format("01/02/2006")
+	username:  strings.MinRunes(3) & strings.MaxRunes(20) & =~"^[a-z]"
+	tags:      list.MinItems(1) & list.MaxItems(10) & [...string]
+	initials:  strings.MaxRunes(3) & =~"^[A-Z]+"
+	angle:     float & >=0.0 & <=(math.Pi * 2)
+}
+#Schema
I testdata/stdlib/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/stdlib/valid.jsonnet b/testdata/stdlib/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..55f3c358823ba2c7f9566775bfb1a1dc0c1f5132
--- /dev/null
+++ b/testdata/stdlib/valid.jsonnet
@@ -0,0 +1,8 @@
+{
+  createdAt: '2024-01-15T10:30:00Z',
+  birthday: '01/15/1990',
+  username: 'alice42',
+  tags: ['go', 'cue', 'jsonnet'],
+  initials: 'AB',
+  angle: 3.14,
+}
I testdata/structs/any/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diff --git a/testdata/structs/any/invalid.jsonnet b/testdata/structs/any/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..cc91a2b3c3e734f7f556ed2d980ed31c225aa7c4
--- /dev/null
+++ b/testdata/structs/any/invalid.jsonnet
@@ -0,0 +1,4 @@
+{
+  name: 42,
+  metadata: 'anything',
+}
I testdata/structs/any/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
diff --git a/testdata/structs/any/schema.cue b/testdata/structs/any/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..d1b29ece2f7124bc795294f7e14960f3b1242616
--- /dev/null
+++ b/testdata/structs/any/schema.cue
@@ -0,0 +1,9 @@
+// Fields typed as _ accept any concrete value while the rest of the
+// definition stays closed. This is the correct escape hatch — using ...
+// does not work because cue.Final() closes all open structs at validation time.
+#Schema: {
+	name:         string
+	metadata:     _   // required; any concrete value accepted
+	annotations?: _   // optional; any concrete value accepted
+}
+#Schema
I testdata/structs/any/valid_object.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
diff --git a/testdata/structs/any/valid_object.jsonnet b/testdata/structs/any/valid_object.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..ae697f8c0307988996a77f8f7cf76fb5db28766e
--- /dev/null
+++ b/testdata/structs/any/valid_object.jsonnet
@@ -0,0 +1,12 @@
+{
+  name: 'Alice',
+  metadata: {
+    role: 'admin',
+    tags: ['a', 'b'],
+    active: true,
+  },
+  annotations: {
+    created: '2024-01-15',
+    source: 'api',
+  },
+}
I testdata/structs/any/valid_scalar.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diff --git a/testdata/structs/any/valid_scalar.jsonnet b/testdata/structs/any/valid_scalar.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..c6acaf141074546ab6ed32da91de0ab056a4dcd2
--- /dev/null
+++ b/testdata/structs/any/valid_scalar.jsonnet
@@ -0,0 +1,4 @@
+{
+  name: 'Alice',
+  metadata: 'just a plain string',
+}
I testdata/structs/closed/invalid_extra.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
diff --git a/testdata/structs/closed/invalid_extra.jsonnet b/testdata/structs/closed/invalid_extra.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..008bec65ce3611fa0aff82dd84552e38d9783d81
--- /dev/null
+++ b/testdata/structs/closed/invalid_extra.jsonnet
@@ -0,0 +1,5 @@
+{
+  name: 'Alice',
+  age: 30,
+  email: 'alice@example.com',
+}
I testdata/structs/closed/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/structs/closed/schema.cue b/testdata/structs/closed/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..c27474ead35f7591a87148cc2f4b40934d9218ce
--- /dev/null
+++ b/testdata/structs/closed/schema.cue
@@ -0,0 +1,6 @@
+// Definition — closed by default; fields not listed here are rejected.
+#Schema: {
+	name: string
+	age:  int
+}
+#Schema
I testdata/structs/closed/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diff --git a/testdata/structs/closed/valid.jsonnet b/testdata/structs/closed/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..3aa5ee8e796a53350b2d902bd99d5f0f3e68efca
--- /dev/null
+++ b/testdata/structs/closed/valid.jsonnet
@@ -0,0 +1,4 @@
+{
+  name: 'Alice',
+  age: 30,
+}
I testdata/structs/nested/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
diff --git a/testdata/structs/nested/invalid.jsonnet b/testdata/structs/nested/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..5b59b7ab93d4072cc18d0c13fd20cf7be7d1ae94
--- /dev/null
+++ b/testdata/structs/nested/invalid.jsonnet
@@ -0,0 +1,11 @@
+{
+  name: 'Alice',
+  address: {
+    street: '123 Main St',
+    city: 'Springfield',
+    zip: 'not-a-zip',
+  },
+  contacts: [
+    { label: 'email' },
+  ],
+}
I testdata/structs/nested/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
diff --git a/testdata/structs/nested/schema.cue b/testdata/structs/nested/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..fe696e34eb7653060e17a314ddd6177883a4b863
--- /dev/null
+++ b/testdata/structs/nested/schema.cue
@@ -0,0 +1,17 @@
+#Address: {
+	street: string
+	city:   string
+	zip:    =~"^[0-9]{5}$"
+}
+
+#Contact: {
+	label: string
+	value: string
+}
+
+#Schema: {
+	name:     string
+	address:  #Address
+	contacts: [...#Contact]
+}
+#Schema
I testdata/structs/nested/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
diff --git a/testdata/structs/nested/valid.jsonnet b/testdata/structs/nested/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..4bf73fc867d9fe6e0394ee1bcd2747c71074a49b
--- /dev/null
+++ b/testdata/structs/nested/valid.jsonnet
@@ -0,0 +1,12 @@
+{
+  name: 'Alice',
+  address: {
+    street: '123 Main St',
+    city: 'Springfield',
+    zip: '12345',
+  },
+  contacts: [
+    { label: 'email', value: 'alice@example.com' },
+    { label: 'phone', value: '555-1234' },
+  ],
+}
I testdata/structs/optional/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diff --git a/testdata/structs/optional/invalid.jsonnet b/testdata/structs/optional/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..01b8320e2fae613f1c9858435d1324779f07034c
--- /dev/null
+++ b/testdata/structs/optional/invalid.jsonnet
@@ -0,0 +1,4 @@
+{
+  name: 'Alice',
+  phone: '555-1234',
+}
I testdata/structs/optional/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
diff --git a/testdata/structs/optional/schema.cue b/testdata/structs/optional/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..8f02546a6fd326feca36004a004dda090fd25721
--- /dev/null
+++ b/testdata/structs/optional/schema.cue
@@ -0,0 +1,8 @@
+// Required fields: name, email.  Optional fields: phone, bio.
+#Schema: {
+	name:   string
+	email:  string
+	phone?: string
+	bio?:   string
+}
+#Schema
I testdata/structs/optional/valid_full.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/testdata/structs/optional/valid_full.jsonnet b/testdata/structs/optional/valid_full.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..2246204975553bc8d22091a7249c19e61fef3665
--- /dev/null
+++ b/testdata/structs/optional/valid_full.jsonnet
@@ -0,0 +1,6 @@
+{
+  name: 'Alice',
+  email: 'alice@example.com',
+  phone: '555-1234',
+  bio: 'Software engineer.',
+}
I testdata/structs/optional/valid_minimal.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
diff --git a/testdata/structs/optional/valid_minimal.jsonnet b/testdata/structs/optional/valid_minimal.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..44eaf4ef927571a56a320ee57fac468ac40ddbe4
--- /dev/null
+++ b/testdata/structs/optional/valid_minimal.jsonnet
@@ -0,0 +1,4 @@
+{
+  name: 'Alice',
+  email: 'alice@example.com',
+}
I testdata/tuples/invalid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
diff --git a/testdata/tuples/invalid.jsonnet b/testdata/tuples/invalid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..efff60d93d4f4684b8f81682e2f33b643b497694
--- /dev/null
+++ b/testdata/tuples/invalid.jsonnet
@@ -0,0 +1,5 @@
+{
+  color: [256, 128, 0],
+  position: [1.5],
+  tag: ['priority', 'one'],
+}
I testdata/tuples/schema.cue
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
diff --git a/testdata/tuples/schema.cue b/testdata/tuples/schema.cue
new file mode 100644
index 0000000000000000000000000000000000000000..d21a3a338293abdcce01a9fd01bf72cd31b0b823
--- /dev/null
+++ b/testdata/tuples/schema.cue
@@ -0,0 +1,10 @@
+// Fixed-length lists with typed positional elements.
+#RGB:   [int & >=0 & <=255, int & >=0 & <=255, int & >=0 & <=255]
+#Point: [float, float]
+
+#Schema: {
+	color:    #RGB
+	position: #Point
+	tag:      [string, int]
+}
+#Schema
I testdata/tuples/valid.jsonnet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
diff --git a/testdata/tuples/valid.jsonnet b/testdata/tuples/valid.jsonnet
new file mode 100644
index 0000000000000000000000000000000000000000..fe860e56b4255498946b7a19c08d6ce589d8f8e7
--- /dev/null
+++ b/testdata/tuples/valid.jsonnet
@@ -0,0 +1,5 @@
+{
+  color: [255, 128, 0],
+  position: [1.5, 2.7],
+  tag: ['priority', 1],
+}