@kurotako/ir 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -19,16 +19,18 @@ declare const ScalarTypeSchema: v.PicklistSchema<["string", "boolean", "int", "b
19
19
  declare const StringFormatSchema: v.PicklistSchema<["email", "url", "uuid", "cuid", "cuid2", "ulid", "datetime", "date", "time", "duration", "ipv4", "ipv6"], undefined>;
20
20
  declare const ReferentialActionSchema: v.PicklistSchema<["cascade", "restrict", "setNull", "setDefault", "noAction"], undefined>;
21
21
  declare const IndexTypeSchema: v.PicklistSchema<["btree", "hash", "gin", "gist", "brin", "spgist"], undefined>;
22
- declare const FieldTypeSchema: v.VariantSchema<"kind", [v.ObjectSchema<{
23
- readonly kind: v.LiteralSchema<"scalar", undefined>;
24
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
25
- }, undefined>, v.ObjectSchema<{
26
- readonly kind: v.LiteralSchema<"enum", undefined>;
27
- readonly ref: v.StringSchema<undefined>;
28
- }, undefined>, v.ObjectSchema<{
29
- readonly kind: v.LiteralSchema<"unknown", undefined>;
30
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
31
- }, undefined>], undefined>;
22
+ /**
23
+ * Recursive because of the `union` variant — wrapped in `v.lazy` with an explicit
24
+ * `v.GenericSchema<FieldType>` annotation, exactly like `JsonValueSchema`. The
25
+ * schema does **not** enforce `variants.length >= 2`: degenerate unions are
26
+ * tolerated on read and normalised in the cross-reference pass.
27
+ */
28
+ declare const FieldTypeSchema: v.GenericSchema<FieldType>;
29
+ declare const TypeAliasSchema: v.ObjectSchema<{
30
+ readonly name: v.StringSchema<undefined>;
31
+ readonly type: v.GenericSchema<FieldType>;
32
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
33
+ }, undefined>;
32
34
  declare const ConstraintsSchema: v.ObjectSchema<{
33
35
  readonly min: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
34
36
  readonly max: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
@@ -48,16 +50,7 @@ declare const DefaultValueSchema: v.VariantSchema<"kind", [v.ObjectSchema<{
48
50
  }, undefined>], undefined>;
49
51
  declare const FieldSchema: v.ObjectSchema<{
50
52
  readonly name: v.StringSchema<undefined>;
51
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
52
- readonly kind: v.LiteralSchema<"scalar", undefined>;
53
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
54
- }, undefined>, v.ObjectSchema<{
55
- readonly kind: v.LiteralSchema<"enum", undefined>;
56
- readonly ref: v.StringSchema<undefined>;
57
- }, undefined>, v.ObjectSchema<{
58
- readonly kind: v.LiteralSchema<"unknown", undefined>;
59
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
60
- }, undefined>], undefined>;
53
+ readonly type: v.GenericSchema<FieldType>;
61
54
  readonly list: v.BooleanSchema<undefined>;
62
55
  readonly optional: v.BooleanSchema<undefined>;
63
56
  readonly nullable: v.BooleanSchema<undefined>;
@@ -128,16 +121,7 @@ declare const EntitySchema: v.ObjectSchema<{
128
121
  readonly name: v.StringSchema<undefined>;
129
122
  readonly fields: v.ArraySchema<v.ObjectSchema<{
130
123
  readonly name: v.StringSchema<undefined>;
131
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
132
- readonly kind: v.LiteralSchema<"scalar", undefined>;
133
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
134
- }, undefined>, v.ObjectSchema<{
135
- readonly kind: v.LiteralSchema<"enum", undefined>;
136
- readonly ref: v.StringSchema<undefined>;
137
- }, undefined>, v.ObjectSchema<{
138
- readonly kind: v.LiteralSchema<"unknown", undefined>;
139
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
140
- }, undefined>], undefined>;
124
+ readonly type: v.GenericSchema<FieldType>;
141
125
  readonly list: v.BooleanSchema<undefined>;
142
126
  readonly optional: v.BooleanSchema<undefined>;
143
127
  readonly nullable: v.BooleanSchema<undefined>;
@@ -161,6 +145,7 @@ declare const EntitySchema: v.ObjectSchema<{
161
145
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
162
146
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
163
147
  }, undefined>, undefined>;
148
+ readonly additionalProperties: v.OptionalSchema<v.GenericSchema<FieldType>, undefined>;
164
149
  readonly relations: v.ArraySchema<v.ObjectSchema<{
165
150
  readonly name: v.StringSchema<undefined>;
166
151
  readonly target: v.ObjectSchema<{
@@ -207,16 +192,7 @@ declare const SourceIrSchema: v.ObjectSchema<{
207
192
  readonly name: v.StringSchema<undefined>;
208
193
  readonly fields: v.ArraySchema<v.ObjectSchema<{
209
194
  readonly name: v.StringSchema<undefined>;
210
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
211
- readonly kind: v.LiteralSchema<"scalar", undefined>;
212
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
213
- }, undefined>, v.ObjectSchema<{
214
- readonly kind: v.LiteralSchema<"enum", undefined>;
215
- readonly ref: v.StringSchema<undefined>;
216
- }, undefined>, v.ObjectSchema<{
217
- readonly kind: v.LiteralSchema<"unknown", undefined>;
218
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
219
- }, undefined>], undefined>;
195
+ readonly type: v.GenericSchema<FieldType>;
220
196
  readonly list: v.BooleanSchema<undefined>;
221
197
  readonly optional: v.BooleanSchema<undefined>;
222
198
  readonly nullable: v.BooleanSchema<undefined>;
@@ -240,6 +216,7 @@ declare const SourceIrSchema: v.ObjectSchema<{
240
216
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
241
217
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
242
218
  }, undefined>, undefined>;
219
+ readonly additionalProperties: v.OptionalSchema<v.GenericSchema<FieldType>, undefined>;
243
220
  readonly relations: v.ArraySchema<v.ObjectSchema<{
244
221
  readonly name: v.StringSchema<undefined>;
245
222
  readonly target: v.ObjectSchema<{
@@ -288,6 +265,11 @@ declare const SourceIrSchema: v.ObjectSchema<{
288
265
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
289
266
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
290
267
  }, undefined>, undefined>;
268
+ readonly typeAliases: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
269
+ readonly name: v.StringSchema<undefined>;
270
+ readonly type: v.GenericSchema<FieldType>;
271
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
272
+ }, undefined>, undefined>, undefined>;
291
273
  }, undefined>;
292
274
  declare const IrSchema: v.ObjectSchema<{
293
275
  readonly irVersion: v.StringSchema<undefined>;
@@ -299,16 +281,7 @@ declare const IrSchema: v.ObjectSchema<{
299
281
  readonly name: v.StringSchema<undefined>;
300
282
  readonly fields: v.ArraySchema<v.ObjectSchema<{
301
283
  readonly name: v.StringSchema<undefined>;
302
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
303
- readonly kind: v.LiteralSchema<"scalar", undefined>;
304
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
305
- }, undefined>, v.ObjectSchema<{
306
- readonly kind: v.LiteralSchema<"enum", undefined>;
307
- readonly ref: v.StringSchema<undefined>;
308
- }, undefined>, v.ObjectSchema<{
309
- readonly kind: v.LiteralSchema<"unknown", undefined>;
310
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
311
- }, undefined>], undefined>;
284
+ readonly type: v.GenericSchema<FieldType>;
312
285
  readonly list: v.BooleanSchema<undefined>;
313
286
  readonly optional: v.BooleanSchema<undefined>;
314
287
  readonly nullable: v.BooleanSchema<undefined>;
@@ -332,6 +305,7 @@ declare const IrSchema: v.ObjectSchema<{
332
305
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
333
306
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
334
307
  }, undefined>, undefined>;
308
+ readonly additionalProperties: v.OptionalSchema<v.GenericSchema<FieldType>, undefined>;
335
309
  readonly relations: v.ArraySchema<v.ObjectSchema<{
336
310
  readonly name: v.StringSchema<undefined>;
337
311
  readonly target: v.ObjectSchema<{
@@ -380,6 +354,11 @@ declare const IrSchema: v.ObjectSchema<{
380
354
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
381
355
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
382
356
  }, undefined>, undefined>;
357
+ readonly typeAliases: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
358
+ readonly name: v.StringSchema<undefined>;
359
+ readonly type: v.GenericSchema<FieldType>;
360
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
361
+ }, undefined>, undefined>, undefined>;
383
362
  }, undefined>, undefined>;
384
363
  }, undefined>;
385
364
 
@@ -389,12 +368,44 @@ declare const IrSchema: v.ObjectSchema<{
389
368
  * hand-written interface here.
390
369
  */
391
370
 
371
+ /**
372
+ * Hand-written because `FieldTypeSchema` is recursive (`union` variant) and
373
+ * `v.lazy` needs an explicit type — same pattern as `JsonValue`.
374
+ * `ref` is a bare name resolved against the same source (`entities` then
375
+ * `typeAliases`); there is no namespace qualifier in v1.
376
+ */
377
+ type FieldType = {
378
+ kind: 'scalar';
379
+ scalar: ScalarType;
380
+ } | {
381
+ kind: 'enum';
382
+ ref: string;
383
+ } | {
384
+ kind: 'unknown';
385
+ hint?: string;
386
+ } | {
387
+ kind: 'ref';
388
+ ref: string;
389
+ } | {
390
+ kind: 'map';
391
+ value: FieldType;
392
+ } | {
393
+ kind: 'array';
394
+ element: FieldType;
395
+ } | {
396
+ kind: 'union';
397
+ variants: FieldType[];
398
+ discriminator?: {
399
+ propertyName: string;
400
+ mapping?: Record<string, string>;
401
+ };
402
+ };
392
403
  type IR = v.InferOutput<typeof IrSchema>;
393
404
  type SourceIR = v.InferOutput<typeof SourceIrSchema>;
394
405
  type Entity = v.InferOutput<typeof EntitySchema>;
395
406
  type Field = v.InferOutput<typeof FieldSchema>;
396
- type FieldType = v.InferOutput<typeof FieldTypeSchema>;
397
407
  type ScalarType = v.InferOutput<typeof ScalarTypeSchema>;
408
+ type TypeAlias = v.InferOutput<typeof TypeAliasSchema>;
398
409
  type StringFormat = v.InferOutput<typeof StringFormatSchema>;
399
410
  type ReferentialAction = v.InferOutput<typeof ReferentialActionSchema>;
400
411
  type IndexType = v.InferOutput<typeof IndexTypeSchema>;
@@ -407,7 +418,7 @@ type EnumValue = v.InferOutput<typeof EnumValueSchema>;
407
418
  type IndexDef = v.InferOutput<typeof IndexDefSchema>;
408
419
  type CompositeUnique = v.InferOutput<typeof CompositeUniqueSchema>;
409
420
 
410
- type IrIssueCode = 'version_incompatible' | 'namespace_key_mismatch' | 'entity_key_mismatch' | 'duplicate_field' | 'duplicate_enum_value' | 'unresolved_enum_ref' | 'unresolved_field_ref' | 'unresolved_relation_target' | 'unresolved_back_relation' | 'invalid_constraint' | 'invalid_regex' | 'shape';
421
+ type IrIssueCode = 'version_incompatible' | 'namespace_key_mismatch' | 'entity_key_mismatch' | 'duplicate_field' | 'duplicate_enum_value' | 'unresolved_enum_ref' | 'unresolved_field_ref' | 'unresolved_relation_target' | 'unresolved_back_relation' | 'unresolved_ref' | 'unresolved_type_alias' | 'type_alias_key_mismatch' | 'degenerate_union' | 'union_cycle' | 'invalid_constraint' | 'invalid_regex' | 'shape';
411
422
  interface IrIssue {
412
423
  path: string;
413
424
  code: IrIssueCode;
@@ -416,6 +427,7 @@ interface IrIssue {
416
427
  type IrValidation<T> = {
417
428
  ok: true;
418
429
  value: T;
430
+ info?: IrIssue[];
419
431
  } | {
420
432
  ok: false;
421
433
  issues: IrIssue[];
@@ -443,9 +455,30 @@ interface EnumBuilder {
443
455
  doc(text: string): this;
444
456
  dbName(name: string): this;
445
457
  }
458
+ /** Type-setters shared by `UnionBuilder` and `TypeAliasBuilder`. */
459
+ interface TypeVariantBuilder {
460
+ scalar(t: ScalarType): this;
461
+ enum(ref: string): this;
462
+ ref(name: string): this;
463
+ union(build: (u: UnionBuilder) => void): this;
464
+ map(build: (value: TypeVariantBuilder) => void): this;
465
+ array(build: (element: TypeVariantBuilder) => void): this;
466
+ unknown(hint?: string): this;
467
+ }
468
+ interface UnionBuilder extends TypeVariantBuilder {
469
+ /** `mapping` values must name a `ref` variant of this union. */
470
+ discriminator(propertyName: string, mapping?: Record<string, string>): this;
471
+ }
472
+ interface TypeAliasBuilder extends TypeVariantBuilder {
473
+ doc(text: string): this;
474
+ }
446
475
  interface FieldBuilder {
447
476
  scalar(t: ScalarType): this;
448
477
  enum(ref: string): this;
478
+ ref(name: string): this;
479
+ union(build: (u: UnionBuilder) => void): this;
480
+ map(build: (value: TypeVariantBuilder) => void): this;
481
+ array(build: (element: TypeVariantBuilder) => void): this;
449
482
  unknown(hint?: string): this;
450
483
  list(): this;
451
484
  optional(): this;
@@ -476,6 +509,7 @@ interface RelationBuilder {
476
509
  }
477
510
  interface EntityBuilder {
478
511
  field(name: string, def: (f: FieldBuilder) => void): this;
512
+ additionalProperties(def: (value: TypeVariantBuilder) => void): this;
479
513
  relation(name: string, def: (r: RelationBuilder) => void): this;
480
514
  localEnum(name: string, def: (e: EnumBuilder) => void): this;
481
515
  primaryKey(...fields: string[]): this;
@@ -492,6 +526,7 @@ interface EntityBuilder {
492
526
  interface SourceIrBuilder {
493
527
  addEnum(name: string, def: (e: EnumBuilder) => void): this;
494
528
  addEntity(name: string, def: (e: EntityBuilder) => void): this;
529
+ addTypeAlias(name: string, def: (t: TypeAliasBuilder) => void): this;
495
530
  build(): SourceIR;
496
531
  }
497
532
  declare function createSourceIR(init: {
@@ -510,6 +545,35 @@ declare function getSource(ir: IR, namespace: string): SourceIR | undefined;
510
545
  declare function resolveEntity(ir: IR, namespace: string, name: string): Entity | undefined;
511
546
  /** Entity-local enums shadow source-level enums of the same name. */
512
547
  declare function resolveEnum(source: SourceIR, entity: Entity | undefined, ref: string): EnumDef | undefined;
548
+ /**
549
+ * Resolve a `{ kind: 'ref' }` name against the same source: an entity first,
550
+ * then a type alias. Same-namespace only — v1 has no namespace qualifier.
551
+ */
552
+ declare function resolveRef(source: SourceIR, ref: string): Entity | TypeAlias | undefined;
553
+ declare function resolveTypeAlias(source: SourceIR, name: string): TypeAlias | undefined;
554
+ declare function iterTypeAliases(ir: IR): Iterable<{
555
+ namespace: string;
556
+ alias: TypeAlias;
557
+ }>;
558
+ /** Every `{ kind: 'ref' }` name reachable from a field type (through unions). */
559
+ declare function collectRefNames(type: FieldType, into?: Set<string>): Set<string>;
560
+ /**
561
+ * Names of entities / type aliases that take part in at least one `ref` cycle
562
+ * within `source` — following field-type `ref`, alias `type` `ref` and union
563
+ * variant `ref` edges. Mirrors the informational `union_cycle` pass in
564
+ * `validate.ts`; a generator consumes this (through `GenerateContext.cycles`)
565
+ * to decide which references must be `z.lazy`-wrapped / widened rather than
566
+ * emitted as a bare forward reference.
567
+ */
568
+ declare function refCycleMembers(source: SourceIR): Set<string>;
569
+ /**
570
+ * Flatten nested unions into a single variant list and drop
571
+ * structurally-identical duplicates, preserving first-seen order. The
572
+ * discriminator of a nested union is not carried up.
573
+ */
574
+ declare function flattenUnion(type: Extract<FieldType, {
575
+ kind: 'union';
576
+ }>): FieldType[];
513
577
  /**
514
578
  * A relation is cross-source when its target namespace is anything other than
515
579
  * the entity's own namespace — including the "absent" (empty) namespace, which
@@ -566,11 +630,11 @@ declare function scalarTsType(type: FieldType): string;
566
630
  * (independent versioning): a single string, bumped only on a breaking change
567
631
  * to the format itself.
568
632
  */
569
- declare const IR_VERSION = "1";
633
+ declare const IR_VERSION = "4";
570
634
  /**
571
635
  * Whether an IR produced against `irVersion` can be consumed by this build.
572
636
  * v1 rule: strict equality.
573
637
  */
574
638
  declare function isCompatible(irVersion: string): boolean;
575
639
 
576
- export { type CompositeUnique, CompositeUniqueSchema, type Constraints, ConstraintsSchema, type DefaultValue, DefaultValueSchema, type Entity, type EntityBuilder, EntitySchema, type EnumBuilder, type EnumDef, EnumDefSchema, type EnumValue, EnumValueSchema, type Field, type FieldBuilder, FieldSchema, type FieldType, FieldTypeSchema, type IR, IR_VERSION, type IndexDef, IndexDefSchema, type IndexType, IndexTypeSchema, IrBuildError, type IrIssue, type IrIssueCode, IrSchema, type IrValidation, IrValidationError, type JsonValue, JsonValueSchema, type ReferentialAction, ReferentialActionSchema, type Relation, type RelationBuilder, RelationSchema, type RelationTarget, RelationTargetSchema, type ScalarTsType, type ScalarType, ScalarTypeSchema, type SourceIR, type SourceIrBuilder, SourceIrSchema, type StringFormat, StringFormatSchema, assertIR, assertSourceIR, createFields, createSourceIR, getSource, isCompatible, isCreateOptional, isCrossSource, isDbAssigned, iterEntities, iterFields, parseIR, primaryKeyFields, resolveEntity, resolveEnum, resolveRelationTarget, scalarTsType, updateFields, validateIR, validateSourceIR };
640
+ export { type CompositeUnique, CompositeUniqueSchema, type Constraints, ConstraintsSchema, type DefaultValue, DefaultValueSchema, type Entity, type EntityBuilder, EntitySchema, type EnumBuilder, type EnumDef, EnumDefSchema, type EnumValue, EnumValueSchema, type Field, type FieldBuilder, FieldSchema, type FieldType, FieldTypeSchema, type IR, IR_VERSION, type IndexDef, IndexDefSchema, type IndexType, IndexTypeSchema, IrBuildError, type IrIssue, type IrIssueCode, IrSchema, type IrValidation, IrValidationError, type JsonValue, JsonValueSchema, type ReferentialAction, ReferentialActionSchema, type Relation, type RelationBuilder, RelationSchema, type RelationTarget, RelationTargetSchema, type ScalarTsType, type ScalarType, ScalarTypeSchema, type SourceIR, type SourceIrBuilder, SourceIrSchema, type StringFormat, StringFormatSchema, type TypeAlias, type TypeAliasBuilder, TypeAliasSchema, type TypeVariantBuilder, type UnionBuilder, assertIR, assertSourceIR, collectRefNames, createFields, createSourceIR, flattenUnion, getSource, isCompatible, isCreateOptional, isCrossSource, isDbAssigned, iterEntities, iterFields, iterTypeAliases, parseIR, primaryKeyFields, refCycleMembers, resolveEntity, resolveEnum, resolveRef, resolveRelationTarget, resolveTypeAlias, scalarTsType, updateFields, validateIR, validateSourceIR };
package/dist/index.d.ts CHANGED
@@ -19,16 +19,18 @@ declare const ScalarTypeSchema: v.PicklistSchema<["string", "boolean", "int", "b
19
19
  declare const StringFormatSchema: v.PicklistSchema<["email", "url", "uuid", "cuid", "cuid2", "ulid", "datetime", "date", "time", "duration", "ipv4", "ipv6"], undefined>;
20
20
  declare const ReferentialActionSchema: v.PicklistSchema<["cascade", "restrict", "setNull", "setDefault", "noAction"], undefined>;
21
21
  declare const IndexTypeSchema: v.PicklistSchema<["btree", "hash", "gin", "gist", "brin", "spgist"], undefined>;
22
- declare const FieldTypeSchema: v.VariantSchema<"kind", [v.ObjectSchema<{
23
- readonly kind: v.LiteralSchema<"scalar", undefined>;
24
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
25
- }, undefined>, v.ObjectSchema<{
26
- readonly kind: v.LiteralSchema<"enum", undefined>;
27
- readonly ref: v.StringSchema<undefined>;
28
- }, undefined>, v.ObjectSchema<{
29
- readonly kind: v.LiteralSchema<"unknown", undefined>;
30
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
31
- }, undefined>], undefined>;
22
+ /**
23
+ * Recursive because of the `union` variant — wrapped in `v.lazy` with an explicit
24
+ * `v.GenericSchema<FieldType>` annotation, exactly like `JsonValueSchema`. The
25
+ * schema does **not** enforce `variants.length >= 2`: degenerate unions are
26
+ * tolerated on read and normalised in the cross-reference pass.
27
+ */
28
+ declare const FieldTypeSchema: v.GenericSchema<FieldType>;
29
+ declare const TypeAliasSchema: v.ObjectSchema<{
30
+ readonly name: v.StringSchema<undefined>;
31
+ readonly type: v.GenericSchema<FieldType>;
32
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
33
+ }, undefined>;
32
34
  declare const ConstraintsSchema: v.ObjectSchema<{
33
35
  readonly min: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
34
36
  readonly max: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
@@ -48,16 +50,7 @@ declare const DefaultValueSchema: v.VariantSchema<"kind", [v.ObjectSchema<{
48
50
  }, undefined>], undefined>;
49
51
  declare const FieldSchema: v.ObjectSchema<{
50
52
  readonly name: v.StringSchema<undefined>;
51
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
52
- readonly kind: v.LiteralSchema<"scalar", undefined>;
53
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
54
- }, undefined>, v.ObjectSchema<{
55
- readonly kind: v.LiteralSchema<"enum", undefined>;
56
- readonly ref: v.StringSchema<undefined>;
57
- }, undefined>, v.ObjectSchema<{
58
- readonly kind: v.LiteralSchema<"unknown", undefined>;
59
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
60
- }, undefined>], undefined>;
53
+ readonly type: v.GenericSchema<FieldType>;
61
54
  readonly list: v.BooleanSchema<undefined>;
62
55
  readonly optional: v.BooleanSchema<undefined>;
63
56
  readonly nullable: v.BooleanSchema<undefined>;
@@ -128,16 +121,7 @@ declare const EntitySchema: v.ObjectSchema<{
128
121
  readonly name: v.StringSchema<undefined>;
129
122
  readonly fields: v.ArraySchema<v.ObjectSchema<{
130
123
  readonly name: v.StringSchema<undefined>;
131
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
132
- readonly kind: v.LiteralSchema<"scalar", undefined>;
133
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
134
- }, undefined>, v.ObjectSchema<{
135
- readonly kind: v.LiteralSchema<"enum", undefined>;
136
- readonly ref: v.StringSchema<undefined>;
137
- }, undefined>, v.ObjectSchema<{
138
- readonly kind: v.LiteralSchema<"unknown", undefined>;
139
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
140
- }, undefined>], undefined>;
124
+ readonly type: v.GenericSchema<FieldType>;
141
125
  readonly list: v.BooleanSchema<undefined>;
142
126
  readonly optional: v.BooleanSchema<undefined>;
143
127
  readonly nullable: v.BooleanSchema<undefined>;
@@ -161,6 +145,7 @@ declare const EntitySchema: v.ObjectSchema<{
161
145
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
162
146
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
163
147
  }, undefined>, undefined>;
148
+ readonly additionalProperties: v.OptionalSchema<v.GenericSchema<FieldType>, undefined>;
164
149
  readonly relations: v.ArraySchema<v.ObjectSchema<{
165
150
  readonly name: v.StringSchema<undefined>;
166
151
  readonly target: v.ObjectSchema<{
@@ -207,16 +192,7 @@ declare const SourceIrSchema: v.ObjectSchema<{
207
192
  readonly name: v.StringSchema<undefined>;
208
193
  readonly fields: v.ArraySchema<v.ObjectSchema<{
209
194
  readonly name: v.StringSchema<undefined>;
210
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
211
- readonly kind: v.LiteralSchema<"scalar", undefined>;
212
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
213
- }, undefined>, v.ObjectSchema<{
214
- readonly kind: v.LiteralSchema<"enum", undefined>;
215
- readonly ref: v.StringSchema<undefined>;
216
- }, undefined>, v.ObjectSchema<{
217
- readonly kind: v.LiteralSchema<"unknown", undefined>;
218
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
219
- }, undefined>], undefined>;
195
+ readonly type: v.GenericSchema<FieldType>;
220
196
  readonly list: v.BooleanSchema<undefined>;
221
197
  readonly optional: v.BooleanSchema<undefined>;
222
198
  readonly nullable: v.BooleanSchema<undefined>;
@@ -240,6 +216,7 @@ declare const SourceIrSchema: v.ObjectSchema<{
240
216
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
241
217
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
242
218
  }, undefined>, undefined>;
219
+ readonly additionalProperties: v.OptionalSchema<v.GenericSchema<FieldType>, undefined>;
243
220
  readonly relations: v.ArraySchema<v.ObjectSchema<{
244
221
  readonly name: v.StringSchema<undefined>;
245
222
  readonly target: v.ObjectSchema<{
@@ -288,6 +265,11 @@ declare const SourceIrSchema: v.ObjectSchema<{
288
265
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
289
266
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
290
267
  }, undefined>, undefined>;
268
+ readonly typeAliases: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
269
+ readonly name: v.StringSchema<undefined>;
270
+ readonly type: v.GenericSchema<FieldType>;
271
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
272
+ }, undefined>, undefined>, undefined>;
291
273
  }, undefined>;
292
274
  declare const IrSchema: v.ObjectSchema<{
293
275
  readonly irVersion: v.StringSchema<undefined>;
@@ -299,16 +281,7 @@ declare const IrSchema: v.ObjectSchema<{
299
281
  readonly name: v.StringSchema<undefined>;
300
282
  readonly fields: v.ArraySchema<v.ObjectSchema<{
301
283
  readonly name: v.StringSchema<undefined>;
302
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
303
- readonly kind: v.LiteralSchema<"scalar", undefined>;
304
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
305
- }, undefined>, v.ObjectSchema<{
306
- readonly kind: v.LiteralSchema<"enum", undefined>;
307
- readonly ref: v.StringSchema<undefined>;
308
- }, undefined>, v.ObjectSchema<{
309
- readonly kind: v.LiteralSchema<"unknown", undefined>;
310
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
311
- }, undefined>], undefined>;
284
+ readonly type: v.GenericSchema<FieldType>;
312
285
  readonly list: v.BooleanSchema<undefined>;
313
286
  readonly optional: v.BooleanSchema<undefined>;
314
287
  readonly nullable: v.BooleanSchema<undefined>;
@@ -332,6 +305,7 @@ declare const IrSchema: v.ObjectSchema<{
332
305
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
333
306
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
334
307
  }, undefined>, undefined>;
308
+ readonly additionalProperties: v.OptionalSchema<v.GenericSchema<FieldType>, undefined>;
335
309
  readonly relations: v.ArraySchema<v.ObjectSchema<{
336
310
  readonly name: v.StringSchema<undefined>;
337
311
  readonly target: v.ObjectSchema<{
@@ -380,6 +354,11 @@ declare const IrSchema: v.ObjectSchema<{
380
354
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
381
355
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
382
356
  }, undefined>, undefined>;
357
+ readonly typeAliases: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
358
+ readonly name: v.StringSchema<undefined>;
359
+ readonly type: v.GenericSchema<FieldType>;
360
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
361
+ }, undefined>, undefined>, undefined>;
383
362
  }, undefined>, undefined>;
384
363
  }, undefined>;
385
364
 
@@ -389,12 +368,44 @@ declare const IrSchema: v.ObjectSchema<{
389
368
  * hand-written interface here.
390
369
  */
391
370
 
371
+ /**
372
+ * Hand-written because `FieldTypeSchema` is recursive (`union` variant) and
373
+ * `v.lazy` needs an explicit type — same pattern as `JsonValue`.
374
+ * `ref` is a bare name resolved against the same source (`entities` then
375
+ * `typeAliases`); there is no namespace qualifier in v1.
376
+ */
377
+ type FieldType = {
378
+ kind: 'scalar';
379
+ scalar: ScalarType;
380
+ } | {
381
+ kind: 'enum';
382
+ ref: string;
383
+ } | {
384
+ kind: 'unknown';
385
+ hint?: string;
386
+ } | {
387
+ kind: 'ref';
388
+ ref: string;
389
+ } | {
390
+ kind: 'map';
391
+ value: FieldType;
392
+ } | {
393
+ kind: 'array';
394
+ element: FieldType;
395
+ } | {
396
+ kind: 'union';
397
+ variants: FieldType[];
398
+ discriminator?: {
399
+ propertyName: string;
400
+ mapping?: Record<string, string>;
401
+ };
402
+ };
392
403
  type IR = v.InferOutput<typeof IrSchema>;
393
404
  type SourceIR = v.InferOutput<typeof SourceIrSchema>;
394
405
  type Entity = v.InferOutput<typeof EntitySchema>;
395
406
  type Field = v.InferOutput<typeof FieldSchema>;
396
- type FieldType = v.InferOutput<typeof FieldTypeSchema>;
397
407
  type ScalarType = v.InferOutput<typeof ScalarTypeSchema>;
408
+ type TypeAlias = v.InferOutput<typeof TypeAliasSchema>;
398
409
  type StringFormat = v.InferOutput<typeof StringFormatSchema>;
399
410
  type ReferentialAction = v.InferOutput<typeof ReferentialActionSchema>;
400
411
  type IndexType = v.InferOutput<typeof IndexTypeSchema>;
@@ -407,7 +418,7 @@ type EnumValue = v.InferOutput<typeof EnumValueSchema>;
407
418
  type IndexDef = v.InferOutput<typeof IndexDefSchema>;
408
419
  type CompositeUnique = v.InferOutput<typeof CompositeUniqueSchema>;
409
420
 
410
- type IrIssueCode = 'version_incompatible' | 'namespace_key_mismatch' | 'entity_key_mismatch' | 'duplicate_field' | 'duplicate_enum_value' | 'unresolved_enum_ref' | 'unresolved_field_ref' | 'unresolved_relation_target' | 'unresolved_back_relation' | 'invalid_constraint' | 'invalid_regex' | 'shape';
421
+ type IrIssueCode = 'version_incompatible' | 'namespace_key_mismatch' | 'entity_key_mismatch' | 'duplicate_field' | 'duplicate_enum_value' | 'unresolved_enum_ref' | 'unresolved_field_ref' | 'unresolved_relation_target' | 'unresolved_back_relation' | 'unresolved_ref' | 'unresolved_type_alias' | 'type_alias_key_mismatch' | 'degenerate_union' | 'union_cycle' | 'invalid_constraint' | 'invalid_regex' | 'shape';
411
422
  interface IrIssue {
412
423
  path: string;
413
424
  code: IrIssueCode;
@@ -416,6 +427,7 @@ interface IrIssue {
416
427
  type IrValidation<T> = {
417
428
  ok: true;
418
429
  value: T;
430
+ info?: IrIssue[];
419
431
  } | {
420
432
  ok: false;
421
433
  issues: IrIssue[];
@@ -443,9 +455,30 @@ interface EnumBuilder {
443
455
  doc(text: string): this;
444
456
  dbName(name: string): this;
445
457
  }
458
+ /** Type-setters shared by `UnionBuilder` and `TypeAliasBuilder`. */
459
+ interface TypeVariantBuilder {
460
+ scalar(t: ScalarType): this;
461
+ enum(ref: string): this;
462
+ ref(name: string): this;
463
+ union(build: (u: UnionBuilder) => void): this;
464
+ map(build: (value: TypeVariantBuilder) => void): this;
465
+ array(build: (element: TypeVariantBuilder) => void): this;
466
+ unknown(hint?: string): this;
467
+ }
468
+ interface UnionBuilder extends TypeVariantBuilder {
469
+ /** `mapping` values must name a `ref` variant of this union. */
470
+ discriminator(propertyName: string, mapping?: Record<string, string>): this;
471
+ }
472
+ interface TypeAliasBuilder extends TypeVariantBuilder {
473
+ doc(text: string): this;
474
+ }
446
475
  interface FieldBuilder {
447
476
  scalar(t: ScalarType): this;
448
477
  enum(ref: string): this;
478
+ ref(name: string): this;
479
+ union(build: (u: UnionBuilder) => void): this;
480
+ map(build: (value: TypeVariantBuilder) => void): this;
481
+ array(build: (element: TypeVariantBuilder) => void): this;
449
482
  unknown(hint?: string): this;
450
483
  list(): this;
451
484
  optional(): this;
@@ -476,6 +509,7 @@ interface RelationBuilder {
476
509
  }
477
510
  interface EntityBuilder {
478
511
  field(name: string, def: (f: FieldBuilder) => void): this;
512
+ additionalProperties(def: (value: TypeVariantBuilder) => void): this;
479
513
  relation(name: string, def: (r: RelationBuilder) => void): this;
480
514
  localEnum(name: string, def: (e: EnumBuilder) => void): this;
481
515
  primaryKey(...fields: string[]): this;
@@ -492,6 +526,7 @@ interface EntityBuilder {
492
526
  interface SourceIrBuilder {
493
527
  addEnum(name: string, def: (e: EnumBuilder) => void): this;
494
528
  addEntity(name: string, def: (e: EntityBuilder) => void): this;
529
+ addTypeAlias(name: string, def: (t: TypeAliasBuilder) => void): this;
495
530
  build(): SourceIR;
496
531
  }
497
532
  declare function createSourceIR(init: {
@@ -510,6 +545,35 @@ declare function getSource(ir: IR, namespace: string): SourceIR | undefined;
510
545
  declare function resolveEntity(ir: IR, namespace: string, name: string): Entity | undefined;
511
546
  /** Entity-local enums shadow source-level enums of the same name. */
512
547
  declare function resolveEnum(source: SourceIR, entity: Entity | undefined, ref: string): EnumDef | undefined;
548
+ /**
549
+ * Resolve a `{ kind: 'ref' }` name against the same source: an entity first,
550
+ * then a type alias. Same-namespace only — v1 has no namespace qualifier.
551
+ */
552
+ declare function resolveRef(source: SourceIR, ref: string): Entity | TypeAlias | undefined;
553
+ declare function resolveTypeAlias(source: SourceIR, name: string): TypeAlias | undefined;
554
+ declare function iterTypeAliases(ir: IR): Iterable<{
555
+ namespace: string;
556
+ alias: TypeAlias;
557
+ }>;
558
+ /** Every `{ kind: 'ref' }` name reachable from a field type (through unions). */
559
+ declare function collectRefNames(type: FieldType, into?: Set<string>): Set<string>;
560
+ /**
561
+ * Names of entities / type aliases that take part in at least one `ref` cycle
562
+ * within `source` — following field-type `ref`, alias `type` `ref` and union
563
+ * variant `ref` edges. Mirrors the informational `union_cycle` pass in
564
+ * `validate.ts`; a generator consumes this (through `GenerateContext.cycles`)
565
+ * to decide which references must be `z.lazy`-wrapped / widened rather than
566
+ * emitted as a bare forward reference.
567
+ */
568
+ declare function refCycleMembers(source: SourceIR): Set<string>;
569
+ /**
570
+ * Flatten nested unions into a single variant list and drop
571
+ * structurally-identical duplicates, preserving first-seen order. The
572
+ * discriminator of a nested union is not carried up.
573
+ */
574
+ declare function flattenUnion(type: Extract<FieldType, {
575
+ kind: 'union';
576
+ }>): FieldType[];
513
577
  /**
514
578
  * A relation is cross-source when its target namespace is anything other than
515
579
  * the entity's own namespace — including the "absent" (empty) namespace, which
@@ -566,11 +630,11 @@ declare function scalarTsType(type: FieldType): string;
566
630
  * (independent versioning): a single string, bumped only on a breaking change
567
631
  * to the format itself.
568
632
  */
569
- declare const IR_VERSION = "1";
633
+ declare const IR_VERSION = "4";
570
634
  /**
571
635
  * Whether an IR produced against `irVersion` can be consumed by this build.
572
636
  * v1 rule: strict equality.
573
637
  */
574
638
  declare function isCompatible(irVersion: string): boolean;
575
639
 
576
- export { type CompositeUnique, CompositeUniqueSchema, type Constraints, ConstraintsSchema, type DefaultValue, DefaultValueSchema, type Entity, type EntityBuilder, EntitySchema, type EnumBuilder, type EnumDef, EnumDefSchema, type EnumValue, EnumValueSchema, type Field, type FieldBuilder, FieldSchema, type FieldType, FieldTypeSchema, type IR, IR_VERSION, type IndexDef, IndexDefSchema, type IndexType, IndexTypeSchema, IrBuildError, type IrIssue, type IrIssueCode, IrSchema, type IrValidation, IrValidationError, type JsonValue, JsonValueSchema, type ReferentialAction, ReferentialActionSchema, type Relation, type RelationBuilder, RelationSchema, type RelationTarget, RelationTargetSchema, type ScalarTsType, type ScalarType, ScalarTypeSchema, type SourceIR, type SourceIrBuilder, SourceIrSchema, type StringFormat, StringFormatSchema, assertIR, assertSourceIR, createFields, createSourceIR, getSource, isCompatible, isCreateOptional, isCrossSource, isDbAssigned, iterEntities, iterFields, parseIR, primaryKeyFields, resolveEntity, resolveEnum, resolveRelationTarget, scalarTsType, updateFields, validateIR, validateSourceIR };
640
+ export { type CompositeUnique, CompositeUniqueSchema, type Constraints, ConstraintsSchema, type DefaultValue, DefaultValueSchema, type Entity, type EntityBuilder, EntitySchema, type EnumBuilder, type EnumDef, EnumDefSchema, type EnumValue, EnumValueSchema, type Field, type FieldBuilder, FieldSchema, type FieldType, FieldTypeSchema, type IR, IR_VERSION, type IndexDef, IndexDefSchema, type IndexType, IndexTypeSchema, IrBuildError, type IrIssue, type IrIssueCode, IrSchema, type IrValidation, IrValidationError, type JsonValue, JsonValueSchema, type ReferentialAction, ReferentialActionSchema, type Relation, type RelationBuilder, RelationSchema, type RelationTarget, RelationTargetSchema, type ScalarTsType, type ScalarType, ScalarTypeSchema, type SourceIR, type SourceIrBuilder, SourceIrSchema, type StringFormat, StringFormatSchema, type TypeAlias, type TypeAliasBuilder, TypeAliasSchema, type TypeVariantBuilder, type UnionBuilder, assertIR, assertSourceIR, collectRefNames, createFields, createSourceIR, flattenUnion, getSource, isCompatible, isCreateOptional, isCrossSource, isDbAssigned, iterEntities, iterFields, iterTypeAliases, parseIR, primaryKeyFields, refCycleMembers, resolveEntity, resolveEnum, resolveRef, resolveRelationTarget, resolveTypeAlias, scalarTsType, updateFields, validateIR, validateSourceIR };