@atscript/typescript 0.1.109 → 0.2.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/cli.cjs +3349 -2548
- package/dist/index.cjs +8 -3
- package/dist/index.d.ts +74 -2
- package/dist/index.mjs +3 -3
- package/dist/plugin-DhesoKce.mjs +3717 -0
- package/dist/plugin-oUSsb3Ri.cjs +3780 -0
- package/dist/realm-runtime.js +6328 -0
- package/dist/test-utils.cjs +79 -5
- package/dist/test-utils.d.ts +366 -1
- package/dist/test-utils.mjs +76 -5
- package/dist/unique-name-hg1CyPIs.mjs +3451 -0
- package/dist/unique-name-xWd9uO-k.cjs +3703 -0
- package/dist/utils.cjs +1303 -250
- package/dist/utils.d.ts +392 -20
- package/dist/utils.mjs +1262 -228
- package/package.json +13 -3
- package/dist/json-schema-9OBvrg9D.cjs +0 -1964
- package/dist/json-schema-CioBVi6F.mjs +0 -1880
- package/dist/plugin-6r27w6rG.cjs +0 -1683
- package/dist/plugin-D3_BnRrI.mjs +0 -1644
package/dist/utils.d.ts
CHANGED
|
@@ -22,6 +22,12 @@ interface TValidatorOptions {
|
|
|
22
22
|
/** Context exposed to {@link TValidatorPlugin} functions. */
|
|
23
23
|
interface TValidatorPluginContext {
|
|
24
24
|
opts: Validator<any>['opts'];
|
|
25
|
+
/**
|
|
26
|
+
* Validates `value` against `def` at the current path (errors go to the current level). With
|
|
27
|
+
* `unknownProps: 'strip'`, a call that returns `false` strips nothing: the keys it recorded are
|
|
28
|
+
* dropped, as for a rejected union branch, so trying alternatives (`a || b`) strips what the
|
|
29
|
+
* union `a | b` would.
|
|
30
|
+
*/
|
|
25
31
|
validateAnnotatedType: Validator<any>['validateAnnotatedType'];
|
|
26
32
|
error: Validator<any>['error'];
|
|
27
33
|
path: Validator<any>['path'];
|
|
@@ -67,11 +73,37 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
67
73
|
protected depth: number;
|
|
68
74
|
protected limitExceeded: boolean;
|
|
69
75
|
protected context: unknown;
|
|
76
|
+
/**
|
|
77
|
+
* The unions over objects this call decided inside union branches (value → outcomes). A union
|
|
78
|
+
* nested in its own branches, many branches each, is reached again with the same value and path
|
|
79
|
+
* from every branch of every union above it — (branches)^(depth) work. The memo is made once
|
|
80
|
+
* the call has wasted {@link WASTE_BEFORE_MEMO} union branches (failed after branches of nested
|
|
81
|
+
* unions over objects failed) since the outermost union, as in the pre-check — ordinary walks
|
|
82
|
+
* never make it. It decides each such union once, with the same result and the same error object
|
|
83
|
+
* (both depend only on these, the options and the input, which the walk never changes), and
|
|
84
|
+
* records the same strips again.
|
|
85
|
+
*/
|
|
86
|
+
private unionMemo?;
|
|
87
|
+
/** Union branches being checked (`> 0`: inside a union branch). */
|
|
88
|
+
private unionDepth;
|
|
89
|
+
/** Failed branches of unions over objects inside union branches (see `unionMemo`). */
|
|
90
|
+
private nestedFailures;
|
|
91
|
+
/** Wasted union branches since the outermost union was entered (see `unionMemo`). */
|
|
92
|
+
private wasted;
|
|
93
|
+
/**
|
|
94
|
+
* `unknownProps: 'strip'`: the unknown keys of the objects that passed (`object, key, …`), in the
|
|
95
|
+
* order the objects completed, then in key order. A rejected union branch, pattern property
|
|
96
|
+
* attempt or plugin sub-validation drops what it recorded; the keys are deleted once the whole
|
|
97
|
+
* call passes (`performStrips`).
|
|
98
|
+
*/
|
|
99
|
+
private strips;
|
|
70
100
|
protected buildPath(): string;
|
|
71
101
|
protected push(name: string): void;
|
|
72
102
|
protected pop(saveErrors: boolean): TError[] | null | undefined;
|
|
73
103
|
protected clear(): void;
|
|
74
|
-
protected error(message: string, path?: string, details?: TError[]):
|
|
104
|
+
protected error(message: string, path?: string, details?: TError[]): TError;
|
|
105
|
+
/** Adds `error`, already made, to the errors of the current level (as {@link error} does). */
|
|
106
|
+
private report;
|
|
75
107
|
protected throw(): void;
|
|
76
108
|
/**
|
|
77
109
|
* Validates a value against the type definition.
|
|
@@ -92,36 +124,68 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
92
124
|
* often built per call, so construction stays as cheap as before.)
|
|
93
125
|
*/
|
|
94
126
|
private get shortcuts();
|
|
127
|
+
/** Validates `value` against `def` (after `replace`, `optional` and the plugins). */
|
|
95
128
|
protected validateSafe(def: TAtscriptAnnotatedType, value: any): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* What {@link validateSafe} decides of `def` (replaced) before its own validation: an
|
|
131
|
+
* optional `def` passes `undefined` / `null`, a plugin may decide — `undefined` to go on.
|
|
132
|
+
*/
|
|
133
|
+
private preCheck;
|
|
134
|
+
/** The context the plugins get (see {@link pluginContext}), made on the first plugin call. */
|
|
135
|
+
private pluginCtx?;
|
|
136
|
+
/**
|
|
137
|
+
* The plugins' view of this validator: its state read live; `validateAnnotatedType` drops the
|
|
138
|
+
* strips a failed call recorded (a failed validation at any level strips nothing).
|
|
139
|
+
*/
|
|
140
|
+
private pluginContext;
|
|
141
|
+
/** `def` as the `replace` option replaces it at the current path (memoized per node). */
|
|
142
|
+
private replaced;
|
|
96
143
|
protected get path(): string;
|
|
97
144
|
/** The type this validator was created for (see {@link TValidatorPluginContext.root}). */
|
|
98
145
|
protected get root(): T;
|
|
99
146
|
protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
|
|
100
147
|
protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
|
|
148
|
+
/** A union branch failed after branches of nested unions over objects did (see `unionMemo`). */
|
|
149
|
+
private wasteBranch;
|
|
150
|
+
/** The outcome this call recorded for union `def` over `value` at `path` (see `unionMemo`). */
|
|
151
|
+
private decided;
|
|
152
|
+
/**
|
|
153
|
+
* Records the outcome of union `def` over `value` at `path`, entered with `kept` strips (the
|
|
154
|
+
* ones after them are its own).
|
|
155
|
+
*/
|
|
156
|
+
private decide;
|
|
101
157
|
/**
|
|
102
158
|
* Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
|
|
103
159
|
* apply to the non-null value that matched `branch`, on top of the branch's own.
|
|
104
160
|
* A literal branch (an enum value) is exempt: its value is fixed.
|
|
105
161
|
*/
|
|
106
162
|
protected validateUnionConstraints(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, branch: TAtscriptAnnotatedType, value: any): boolean;
|
|
163
|
+
/** Every member must pass, each on its own (JSON Schema `allOf`). */
|
|
107
164
|
protected validateIntersection(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* An unknown key (`unknownProps`): 'error' reports it, 'strip' adds it to the keys to strip
|
|
167
|
+
* (returned; recorded in `strips` once the object passed).
|
|
168
|
+
*/
|
|
169
|
+
private unknownKey;
|
|
170
|
+
/** The `skipList` keys of the object at the current path. */
|
|
171
|
+
private skipListHere;
|
|
108
172
|
protected validateTuple(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
|
|
109
173
|
protected validateArray(def: TAtscriptAnnotatedType<TAtscriptTypeArray>, value: any): boolean;
|
|
110
174
|
/** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */
|
|
111
|
-
protected validateArrayConstraints(
|
|
175
|
+
protected validateArrayConstraints(meta: TMetadataMap<AtscriptMetadata>, value: any[], of?: TAtscriptAnnotatedType): boolean;
|
|
112
176
|
protected validateObject(def: TAtscriptAnnotatedType<TAtscriptTypeObject>, value: any): boolean;
|
|
113
177
|
protected validatePrimitive(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: any): boolean;
|
|
114
178
|
protected validateString(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: string): boolean;
|
|
115
179
|
/** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */
|
|
116
|
-
protected validateStringConstraints(
|
|
180
|
+
protected validateStringConstraints(meta: TMetadataMap<AtscriptMetadata>, value: string): boolean;
|
|
117
181
|
protected validateNumber(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: number): boolean;
|
|
118
182
|
/** `@expect.int` / `min` / `max` / `exclusiveMin` / `exclusiveMax` checks for a number value. */
|
|
119
|
-
protected validateNumberConstraints(
|
|
183
|
+
protected validateNumberConstraints(meta: TMetadataMap<AtscriptMetadata>, value: number): boolean;
|
|
120
184
|
/** Reports a failed number bound: its custom message, else `Expected <label> <bound>, got <value>`. */
|
|
121
185
|
private numberBoundFailed;
|
|
122
186
|
protected validateBoolean(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: boolean): boolean;
|
|
123
187
|
/** `@meta.required` check for a boolean value (must be `true`). */
|
|
124
|
-
protected validateBooleanConstraints(
|
|
188
|
+
protected validateBooleanConstraints(meta: TMetadataMap<AtscriptMetadata>, value: boolean): boolean;
|
|
125
189
|
}
|
|
126
190
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */
|
|
127
191
|
declare class ValidatorError extends Error {
|
|
@@ -133,8 +197,13 @@ declare class ValidatorError extends Error {
|
|
|
133
197
|
/**
|
|
134
198
|
* Creates a minimal annotated type node from a type def and metadata map.
|
|
135
199
|
* Centralises the shape so callers never duplicate the boilerplate.
|
|
200
|
+
*
|
|
201
|
+
* `metadata` is used as-is (the node keeps the caller's map, so later writes through it
|
|
202
|
+
* apply). When omitted, the node gets a new, tracked metadata map. Compiled validator
|
|
203
|
+
* checks only bind to nodes whose metadata is tracked; nodes built over a caller-supplied
|
|
204
|
+
* `Map` are always validated by the interpreted validator.
|
|
136
205
|
*/
|
|
137
|
-
declare function createAnnotatedTypeNode(type: TAtscriptTypeDef, metadata
|
|
206
|
+
declare function createAnnotatedTypeNode(type: TAtscriptTypeDef, metadata?: Map<string, unknown> | TMetadataMap<AtscriptMetadata>, opts?: {
|
|
138
207
|
id?: string;
|
|
139
208
|
optional?: boolean;
|
|
140
209
|
ref?: {
|
|
@@ -243,13 +312,11 @@ declare function isAnnotatedType(type: any): type is TAtscriptAnnotatedType;
|
|
|
243
312
|
/**
|
|
244
313
|
* Standalone annotate function that handles both replace and append (array) strategies.
|
|
245
314
|
* Used by the handle's .annotate() method and by generated mutation statements.
|
|
315
|
+
*
|
|
316
|
+
* A layered map (a reference, an override, a merged field: see `layers.ts`) appends after the
|
|
317
|
+
* values its bases give.
|
|
246
318
|
*/
|
|
247
319
|
declare function annotate<K extends keyof AtscriptMetadata>(metadata: TMetadataMap<AtscriptMetadata> | undefined, key: K, value: AtscriptMetadata[K] extends Array<infer E> ? E : AtscriptMetadata[K], asArray?: boolean): void;
|
|
248
|
-
/**
|
|
249
|
-
* Clones a property's type tree in-place so mutations don't leak to shared refs.
|
|
250
|
-
* Used by mutating annotate codegen when paths cross ref boundaries.
|
|
251
|
-
*/
|
|
252
|
-
declare function cloneRefProp(parentType: TAtscriptTypeDef, propName: string): void;
|
|
253
320
|
type TKind = '' | 'array' | 'object' | 'union' | 'intersection' | 'tuple';
|
|
254
321
|
/**
|
|
255
322
|
* Creates a builder handle for constructing a {@link TAtscriptAnnotatedType} at runtime.
|
|
@@ -296,13 +363,19 @@ interface TAnnotatedTypeHandle {
|
|
|
296
363
|
prop(name: string, value: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
|
|
297
364
|
propPattern(pattern: RegExp, value: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
|
|
298
365
|
optional(value?: boolean): TAnnotatedTypeHandle;
|
|
299
|
-
copyMetadata(fromMetadata: TMetadataMap<AtscriptMetadata>): TAnnotatedTypeHandle;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
366
|
+
copyMetadata(fromMetadata: TMetadataMap<AtscriptMetadata>, ignore?: Set<string>): TAnnotatedTypeHandle;
|
|
367
|
+
/**
|
|
368
|
+
* A reference node: `target`'s def (at `chain`: a chain ref), metadata
|
|
369
|
+
* layered over it (a reference layer: `passedWhenReferred: false` keys are not read through).
|
|
370
|
+
*/
|
|
371
|
+
ref(target: TAtscriptAnnotatedType, chain?: string[]): TAnnotatedTypeHandle;
|
|
372
|
+
/** The node's metadata layered over `bases` (priority order), its own entries kept. */
|
|
373
|
+
over(bases: TAtscriptAnnotatedType[], reference?: boolean): TAnnotatedTypeHandle;
|
|
374
|
+
/** The node's def is `of`'s (a merged field typed by a hidden class), with no layer over it. */
|
|
375
|
+
type(of: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
|
|
305
376
|
annotate(key: keyof AtscriptMetadata, value: any, asArray?: boolean): TAnnotatedTypeHandle;
|
|
377
|
+
/** Marks keys of this node intrinsic: a primitive extension's own annotations. */
|
|
378
|
+
intrinsic(...keys: string[]): TAnnotatedTypeHandle;
|
|
306
379
|
id(value: string): TAnnotatedTypeHandle;
|
|
307
380
|
}
|
|
308
381
|
/**
|
|
@@ -421,7 +494,10 @@ declare function fromJsonSchema(schema: TJsonSchema): TAtscriptAnnotatedType;
|
|
|
421
494
|
*
|
|
422
495
|
* Each type must have an `id`. The returned `schemas` object contains individual
|
|
423
496
|
* schemas keyed by type id, and `$defs` contains all shared type definitions
|
|
424
|
-
* deduplicated across schemas
|
|
497
|
+
* deduplicated across schemas: a definition equal to one already merged (the same schema,
|
|
498
|
+
* its references leading to equal definitions) is shared; a different one asking for a name
|
|
499
|
+
* already taken (two types named alike) gets the next free `<name>_<n>`, and the references
|
|
500
|
+
* to it are renamed.
|
|
425
501
|
*
|
|
426
502
|
* @param types - Array of annotated types, each with an `id`.
|
|
427
503
|
* @returns An object with `schemas` (keyed by id) and shared `$defs`.
|
|
@@ -497,6 +573,122 @@ declare function createDataFromAnnotatedType(type: TAtscriptAnnotatedType, opts?
|
|
|
497
573
|
*/
|
|
498
574
|
declare function throwFeatureDisabled(feature: string, option: string, annotation: string): never;
|
|
499
575
|
|
|
576
|
+
/**
|
|
577
|
+
* The JSON schema of `type` built by the runtime (`jsonSchema: 'lazy'`): built once, again after
|
|
578
|
+
* a metadata write (a block, a runtime edit: see `metadataWrites`). Called by generated code
|
|
579
|
+
* (`static toJsonSchema()`).
|
|
580
|
+
*/
|
|
581
|
+
declare function lazyJsonSchema(type: TAtscriptAnnotatedType): TJsonSchema;
|
|
582
|
+
/**
|
|
583
|
+
* The JSON schema of `type` (`jsonSchema: 'bundle'`): the
|
|
584
|
+
* bundled `literal` while the modules whose blocks applied are exactly those the build accounted
|
|
585
|
+
* (`modules`, the realm's applied block modules), else the runtime's (`buildJsonSchema`). Raw
|
|
586
|
+
* metadata writes are not seen (as in 0.1.110). Called by generated code.
|
|
587
|
+
*/
|
|
588
|
+
declare function bundledSchemaOf(type: TAtscriptAnnotatedType, literal: () => TJsonSchema, modules?: readonly string[]): TJsonSchema;
|
|
589
|
+
|
|
590
|
+
type TSite = [optional: number, mask: number, def: number, shape: string, slot: number];
|
|
591
|
+
type TDef = Array<string | number | boolean | string[] | TSite | TSite[]>;
|
|
592
|
+
type TLayout = [defs: TDef[], root: TSite, slotCount: number];
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Runtime registry and binder of build-time compiled validator checks.
|
|
596
|
+
*
|
|
597
|
+
* Generated `.as.js` modules (built with `tsPlugin({ compileValidators: true })`) call
|
|
598
|
+
* {@link registerCompiledCheck} once per class with the class's layout and a factory of
|
|
599
|
+
* its check. On first use, and again after each mutation epoch (see tracking.ts), the
|
|
600
|
+
* entry is bound: `bindLayout` verifies the live type tree against the layout and collects
|
|
601
|
+
* the constraint values into `K`; the factory turns `K` into the check function. A tree
|
|
602
|
+
* that no longer matches leaves the entry unbound for that epoch, so the interpreted
|
|
603
|
+
* pre-check runs instead — always correct, never stale.
|
|
604
|
+
*
|
|
605
|
+
* A registered entry is also a template: a node referencing a class of another module
|
|
606
|
+
* (an `EXT` site) binds that class's layout against itself — its own annotations feed `K`
|
|
607
|
+
* — so cross-module references, aliases of imported types and their derived props run
|
|
608
|
+
* compiled code whenever their tree matches the referenced type's structure.
|
|
609
|
+
*
|
|
610
|
+
* The registry is module-local: classes registered with another copy of this runtime
|
|
611
|
+
* (dual ESM / CJS loads, duplicate installs) and subclasses of registered classes are
|
|
612
|
+
* simply validated by the interpreted pre-check.
|
|
613
|
+
*/
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* A compiled check: `(value, flags, opts) → 0 FAIL | 1 PASS | 2 BAIL` (the pre-check
|
|
617
|
+
* contract; `flags` carries the TOP / IN_UNION bits and the option bits, see
|
|
618
|
+
* {@link optionFlags}).
|
|
619
|
+
*/
|
|
620
|
+
type TCompiledCheck = (value: unknown, flags: number, opts: TValidatorOptions) => number;
|
|
621
|
+
/** Factory emitted per class: builds the check from the bound annotation values. */
|
|
622
|
+
type TCompiledFactory = (K: unknown[], H: TCompiledHelpers) => TCompiledCheck;
|
|
623
|
+
/** Runtime helpers the emitted factories use. */
|
|
624
|
+
interface TCompiledHelpers {
|
|
625
|
+
/** Pending strips, `object, key, …` (shared with the interpreted pre-check). */
|
|
626
|
+
pk: unknown[];
|
|
627
|
+
/** `Object.prototype.hasOwnProperty`. */
|
|
628
|
+
ho: (this: object, key: PropertyKey) => boolean;
|
|
629
|
+
/** Decimal-string regex. */
|
|
630
|
+
dec: RegExp;
|
|
631
|
+
/** `rejectsNullAsRequired` (read live: it depends on the node's whole subtree). */
|
|
632
|
+
rn: (def: TAtscriptAnnotatedType) => boolean;
|
|
633
|
+
/**
|
|
634
|
+
* The union memo of the current pre-check call (see `TUnionMemo` in validator-fast-check.ts):
|
|
635
|
+
* `m` the memo (made once the call wastes nested union work), `n` the failed branches of unions
|
|
636
|
+
* over objects inside union branches, `w` the wasted branches since the outermost union.
|
|
637
|
+
*/
|
|
638
|
+
m: TUnionMemo | undefined;
|
|
639
|
+
n: number;
|
|
640
|
+
w: number;
|
|
641
|
+
/** A union branch failed after branches of nested unions over objects did (see `unionWaste`). */
|
|
642
|
+
wa: () => void;
|
|
643
|
+
/** The memoized outcome of union `key` over `value` with `flags`, or -1 (records its strips). */
|
|
644
|
+
mg: (key: object, value: object, flags: number, opts: TValidatorOptions) => number;
|
|
645
|
+
/** Records the outcome of union `key` over `value` with `flags`, entered with `kept` strips. */
|
|
646
|
+
ms: (key: object, value: object, flags: number, opts: TValidatorOptions, result: number, kept: number) => void;
|
|
647
|
+
}
|
|
648
|
+
/** Union outcomes of one pre-check call: value → [key, flags, options, result, strips]… */
|
|
649
|
+
type TUnionMemo = Map<object, unknown[]>;
|
|
650
|
+
/**
|
|
651
|
+
* Registers the build-time compiled validator check of a generated class (emitted into
|
|
652
|
+
* `.as.js` by `tsPlugin({ compileValidators: true })`; not meant to be called by hand).
|
|
653
|
+
* A layout of another format version is ignored, so the class stays interpreted.
|
|
654
|
+
*/
|
|
655
|
+
declare function registerCompiledCheck(cls: TAtscriptAnnotatedType, version: number, layout: TLayout, factory: TCompiledFactory): void;
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Makes every compiled validator check re-verify the type tree it was compiled for, and every
|
|
659
|
+
* node that reads metadata through other nodes re-read them, before its next use.
|
|
660
|
+
*
|
|
661
|
+
* Validators (compiled checks emitted with `tsPlugin({ compileValidators: true })` and the
|
|
662
|
+
* interpreted walk alike) see mutations made through `annotate()`, the metadata / props `Map`
|
|
663
|
+
* methods and the `defineAnnotatedType()` handle methods automatically. Call this after
|
|
664
|
+
* changing a type tree any other way:
|
|
665
|
+
* - a raw property write: `node.optional = …`, `node.type = …`, `node.metadata = …`,
|
|
666
|
+
* `type.items.push(…)`, `type.of = …`, `type.propsPatterns.push(…)`;
|
|
667
|
+
* - an in-place edit of an annotation value: mutating a constraint object
|
|
668
|
+
* (`metadata.get('expect.min').minValue = 5`), editing or pushing onto an
|
|
669
|
+
* `@expect.pattern` array directly (use `annotate(metadata, key, value, true)` instead).
|
|
670
|
+
*
|
|
671
|
+
* Without it a compiled check may keep validating against the old shape or values, and a
|
|
672
|
+
* node that reads metadata through other nodes (a reference, a merged field, a path override)
|
|
673
|
+
* may keep reading the old values: the rule is the same for both engines.
|
|
674
|
+
*
|
|
675
|
+
* Any mutation of a type a compiled check has bound to (tracked or invalidated) makes every
|
|
676
|
+
* compiled check re-bind once on its next use: cheap during startup / configuration, but
|
|
677
|
+
* avoid mutating validated types per request.
|
|
678
|
+
*/
|
|
679
|
+
declare function invalidateCompiledValidators(): void;
|
|
680
|
+
/**
|
|
681
|
+
* Metadata map of runtime type nodes: a `Map` whose writes to validator annotation keys
|
|
682
|
+
* are tracked (see {@link invalidateCompiledValidators}). Reads are the builtin ones.
|
|
683
|
+
*/
|
|
684
|
+
declare class AnnotationMap<K = unknown, V = unknown> extends Map<K, V> {
|
|
685
|
+
/** A new map holding `entries`: no write (a new map is in no compiled check nor schema). */
|
|
686
|
+
constructor(entries?: Iterable<readonly [K, V]> | null);
|
|
687
|
+
set(key: K, value: V): this;
|
|
688
|
+
delete(key: K): boolean;
|
|
689
|
+
clear(): void;
|
|
690
|
+
}
|
|
691
|
+
|
|
500
692
|
/**
|
|
501
693
|
* Options for controlling the flattening process.
|
|
502
694
|
*/
|
|
@@ -519,6 +711,25 @@ interface TFlattenOptions {
|
|
|
519
711
|
*/
|
|
520
712
|
excludePhantomTypes?: boolean;
|
|
521
713
|
}
|
|
714
|
+
/**
|
|
715
|
+
* Flattens an annotated object type into a map of dot-separated paths to their annotated types.
|
|
716
|
+
*
|
|
717
|
+
* For an object like `{ address: { street: string, city: string } }`, the result contains:
|
|
718
|
+
* - `''` → the root object type
|
|
719
|
+
* - `'address'` → the address object type
|
|
720
|
+
* - `'address.street'` → string type
|
|
721
|
+
* - `'address.city'` → string type
|
|
722
|
+
*
|
|
723
|
+
* Arrays are recursed into (their element types are flattened with the same prefix). Unions,
|
|
724
|
+
* tuples and intersections are recursed into (each item contributes to the same path, merging
|
|
725
|
+
* via synthetic union types when paths collide). A recursive type is listed where it is reached
|
|
726
|
+
* again, not expanded (the readers' recursion cut, see `cut.ts`). `onField` runs for a path once
|
|
727
|
+
* its sub-paths are flattened.
|
|
728
|
+
*
|
|
729
|
+
* @param type - The root annotated type (must be an object type).
|
|
730
|
+
* @param options - Optional hooks for domain-specific processing.
|
|
731
|
+
* @returns A map of dot-separated field paths to their annotated types.
|
|
732
|
+
*/
|
|
522
733
|
declare function flattenAnnotatedType(type: TAtscriptAnnotatedType<TAtscriptTypeObject>, options?: TFlattenOptions): Map<string, TAtscriptAnnotatedType>;
|
|
523
734
|
|
|
524
735
|
/** Runtime shape of a ref annotation argument (lazy type reference with optional chain). */
|
|
@@ -578,6 +789,24 @@ interface TSerializedAnnotatedTypeInner {
|
|
|
578
789
|
metadata: Record<string, unknown>;
|
|
579
790
|
optional?: boolean;
|
|
580
791
|
id?: string;
|
|
792
|
+
/**
|
|
793
|
+
* The type's own id when the payload `id` differs from it: distinct types sharing a name
|
|
794
|
+
* (from different modules) get unique payload ids (`Name`, `Name@1`, `Name@2`, …), which
|
|
795
|
+
* `$ref` entries use; `deserializeAnnotatedType` restores this one as the type's `id`.
|
|
796
|
+
*/
|
|
797
|
+
origId?: string;
|
|
798
|
+
/**
|
|
799
|
+
* The node has no id of its own: `id` is a payload id given to a reference without one that
|
|
800
|
+
* reaches itself (a reference to a prop of a type, inside that prop: `d?: { x?: T.d }`), so
|
|
801
|
+
* the `$ref` entries below it can point to it. `deserializeAnnotatedType` gives it no id.
|
|
802
|
+
*/
|
|
803
|
+
anon?: true;
|
|
804
|
+
/**
|
|
805
|
+
* The readers' recursion cut (see `cut.ts`) of a node without an id whose def stands for the
|
|
806
|
+
* same type as other defs of the payload (a path override's copy of a def and that def): one
|
|
807
|
+
* tag per type, restored on the deserialized defs.
|
|
808
|
+
*/
|
|
809
|
+
cut?: number;
|
|
581
810
|
/** Reference to another named type (chain/FK or plain); shallow `{ id, metadata }` target when `refDepth` has a `.5` fractional part. */
|
|
582
811
|
ref?: {
|
|
583
812
|
type: TSerializedAnnotatedTypeInner | TSerializedShallowRefTarget;
|
|
@@ -710,6 +939,137 @@ declare function serializeAnnotatedType(type: TAtscriptAnnotatedType, options?:
|
|
|
710
939
|
*/
|
|
711
940
|
declare function deserializeAnnotatedType(data: TSerializedAnnotatedType): TAtscriptAnnotatedType;
|
|
712
941
|
|
|
942
|
+
/**
|
|
943
|
+
* Metadata layers: the runtime's one metadata mechanism.
|
|
944
|
+
*
|
|
945
|
+
* A node's metadata is its own map plus an ordered list of base nodes (`b`, priority order:
|
|
946
|
+
* `b[0]` first). A read returns the own value, else the first base that has the key; an append
|
|
947
|
+
* key returns the bases' values (lowest priority first), then the own ones. Writes (`set`,
|
|
948
|
+
* `delete`, `clear`) are the own map's: `delete` lets a base's value show again, and a write on
|
|
949
|
+
* a base shows through every layer over it.
|
|
950
|
+
*
|
|
951
|
+
* Reads go through a flattened view of the effective entries, rebuilt on the first read after any
|
|
952
|
+
* metadata write (one global write counter: `metadataWriteCount`). The view is used only while
|
|
953
|
+
* every map below is tracked (an `AnnotationMap` or a `LayerMap`, whose writes the counter
|
|
954
|
+
* sees); otherwise the reads are live. A raw write the counter does not see (a node's map swapped,
|
|
955
|
+
* `node.metadata = X`, or edited in place) must be followed by `invalidateCompiledValidators()`,
|
|
956
|
+
* as compiled checks require (PERF-2).
|
|
957
|
+
*/
|
|
958
|
+
|
|
959
|
+
/** A node as layers read it. */
|
|
960
|
+
interface TLayerNode {
|
|
961
|
+
metadata: Map<unknown, unknown>;
|
|
962
|
+
}
|
|
963
|
+
/** Registers append keys (`mergeStrategy: 'append'`). */
|
|
964
|
+
declare function $ak(...keys: string[]): void;
|
|
965
|
+
/** Registers `passedWhenReferred: false` keys. */
|
|
966
|
+
declare function $nr(...keys: string[]): void;
|
|
967
|
+
/**
|
|
968
|
+
* A metadata map that reads through base nodes (see the file comment). `r`: the bases are
|
|
969
|
+
* reference layers (a prop's / element's type, a chain ref, `extends`, merge operands), so
|
|
970
|
+
* `passedWhenReferred: false` keys are not read through them.
|
|
971
|
+
*/
|
|
972
|
+
declare class LayerMap<K = unknown, V = unknown> extends AnnotationMap<K, V> {
|
|
973
|
+
b: TLayerNode[];
|
|
974
|
+
r: boolean;
|
|
975
|
+
/**
|
|
976
|
+
* The flattened view: every effective entry (as the layered reads give it, in their order),
|
|
977
|
+
* built on the first read after a metadata write (`w` ≠ `metadataWrites`); `undefined` while a
|
|
978
|
+
* map below is not tracked (a plain `Map` or a subclass: its writes are not counted, read live).
|
|
979
|
+
*/
|
|
980
|
+
private f;
|
|
981
|
+
/** `metadataWrites` when {@link f} was built (`-1`: not built). */
|
|
982
|
+
private w;
|
|
983
|
+
constructor(bases: TLayerNode[], reference?: boolean, own?: Iterable<readonly [K, V]> | null);
|
|
984
|
+
/**
|
|
985
|
+
* The flattened view (built again after a metadata write), `undefined`: read live. Internal:
|
|
986
|
+
* this module's readers and the validators read it (`readable`, `filledMeta`), never write.
|
|
987
|
+
*/
|
|
988
|
+
private view;
|
|
989
|
+
private flatten;
|
|
990
|
+
/** The value of `key` (not concatenated), or `MISS`; `nr`: a `passedWhenReferred:false` key. */
|
|
991
|
+
lookup(key: unknown, nr: boolean): unknown;
|
|
992
|
+
get(key: K): V | undefined;
|
|
993
|
+
/** `get` read through the layers (see the file comment). */
|
|
994
|
+
private liveGet;
|
|
995
|
+
has(key: K): boolean;
|
|
996
|
+
/** An append key: the bases' values (lowest priority first), then the own ones. */
|
|
997
|
+
private append;
|
|
998
|
+
/** The effective keys: own first, then the bases' in priority order. */
|
|
999
|
+
private effectiveKeys;
|
|
1000
|
+
private liveKeys;
|
|
1001
|
+
keys(): MapIterator<K>;
|
|
1002
|
+
values(): MapIterator<V>;
|
|
1003
|
+
entries(): MapIterator<[K, V]>;
|
|
1004
|
+
[Symbol.iterator](): MapIterator<[K, V]>;
|
|
1005
|
+
forEach(cb: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void;
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* `m` has no entry (own or through its bases): the validators' `metadata.size === 0` fast path,
|
|
1009
|
+
* without computing a layered map's effective size. May answer `false` for a map whose only base
|
|
1010
|
+
* entries are filtered out (`passedWhenReferred: false`): the slow path then finds nothing.
|
|
1011
|
+
*/
|
|
1012
|
+
declare function metaEmpty(m: Map<unknown, unknown>): boolean;
|
|
1013
|
+
/**
|
|
1014
|
+
* Watches `m` and every base (node and map, transitively) for a compiled check's bind (see
|
|
1015
|
+
* `compiled-check/tracking.ts`): a tracked write to any of them re-binds the check. `false` when
|
|
1016
|
+
* one of the maps is untracked (a plain `Map`): such a tree stays interpreted.
|
|
1017
|
+
*/
|
|
1018
|
+
declare function watchLayer(m: Map<unknown, unknown>, seen?: Set<object>): boolean;
|
|
1019
|
+
|
|
1020
|
+
type TNode = TAtscriptAnnotatedType;
|
|
1021
|
+
/**
|
|
1022
|
+
* The node at `path` below `root` (see {@link child}); `NOT_READY` while it is not built, or
|
|
1023
|
+
* while a pending shell (an import cycle's, see {@link $ps}) would replace it.
|
|
1024
|
+
*/
|
|
1025
|
+
declare function $f(root: unknown, ...path: string[]): TNode;
|
|
1026
|
+
/** {@link $f} for a reader of the node's def (`.ref(T, chain)`, `$alias`): its final def. */
|
|
1027
|
+
declare function $ft(root: unknown, ...path: string[]): TNode;
|
|
1028
|
+
/** {@link $f} for a shell that clones the node's def: once every shell put into it (a guard). */
|
|
1029
|
+
declare function $fc(root: unknown, ...path: string[]): TNode;
|
|
1030
|
+
/**
|
|
1031
|
+
* Class `P` is `T` with annotations of its own (an annotated alias, an `annotate T as P` copy;
|
|
1032
|
+
* `chain`: `type P = T.a.b`, a chain ref): T's def (at `chain`), and metadata layered over it
|
|
1033
|
+
* (P's own entries kept; a chain ref is a reference layer).
|
|
1034
|
+
*/
|
|
1035
|
+
declare function $alias(P: TNode, T: unknown, chain?: string[]): TNode;
|
|
1036
|
+
/** A step of a shell path: the step and its kind (from the build's structure model). */
|
|
1037
|
+
type TStep = [string, 0 | 1 | 2];
|
|
1038
|
+
/** Ensures T's shells along each path (see {@link ensure}). */
|
|
1039
|
+
declare function $sh(T: unknown, paths: TStep[][]): void;
|
|
1040
|
+
/** Registers class `T`'s shells as pending (see {@link $sh}): first statement of its module. */
|
|
1041
|
+
declare function $ps(T: unknown, paths: TStep[][]): void;
|
|
1042
|
+
/**
|
|
1043
|
+
* One entry's annotations: `[key, value]`, or `[key, value, 1]` for a list key (`multiple`):
|
|
1044
|
+
* its values in order. Written as one value per key.
|
|
1045
|
+
*/
|
|
1046
|
+
type TAnnotation = [string, unknown] | [string, unknown, 1];
|
|
1047
|
+
/** A block entry: its annotations, then the paths it writes (several: a union's branches). */
|
|
1048
|
+
type TEntry = [TAnnotation[], ...TStep[][]];
|
|
1049
|
+
/** The module ids of every block applied so far. */
|
|
1050
|
+
declare function appliedBlockModules(): ReadonlySet<string>;
|
|
1051
|
+
/**
|
|
1052
|
+
* Block `index` of a module of rank `R` (`[import depth, module id]`; `null`: an `as` copy's
|
|
1053
|
+
* entries, below every block) on `T`: ensures each entry's shells, then writes its annotations
|
|
1054
|
+
* with rank (R, index, entry index). Re-applying a block is idempotent.
|
|
1055
|
+
*/
|
|
1056
|
+
declare function $blk(R: [number, string] | null, index: number, T: unknown, entries: TEntry[]): void;
|
|
1057
|
+
/**
|
|
1058
|
+
* Runs `fn` (a statement reading a class of another module of its import cycle), or queues it
|
|
1059
|
+
* when that class is not built yet (ESM: its temporal dead zone; CommonJS: `undefined`). `own`:
|
|
1060
|
+
* the module's own list of queued statements (a module in no import cycle, see {@link $end}).
|
|
1061
|
+
*/
|
|
1062
|
+
declare function $q(fn: () => unknown, own?: Array<() => unknown>): void;
|
|
1063
|
+
/**
|
|
1064
|
+
* The last statement of a module: runs the queued statements that can run now, in order. A module
|
|
1065
|
+
* in no import cycle passes its own queued statements (`own`, see {@link $q}) and throws when one
|
|
1066
|
+
* of them still waits: nothing it waits for can come later. (A module of an import cycle is
|
|
1067
|
+
* checked when the build loads the cycle from each of its modules.)
|
|
1068
|
+
*/
|
|
1069
|
+
declare function $end(own?: Array<() => unknown>): void;
|
|
1070
|
+
/** How many statements wait for a class of an import cycle (0 once every module loaded). */
|
|
1071
|
+
declare function pendingStatements(): number;
|
|
1072
|
+
|
|
713
1073
|
/**
|
|
714
1074
|
* Extracts the flat dot-notation type map from an Atscript annotated type.
|
|
715
1075
|
* If the type has a `__flat` static property (emitted for `@db.table` interfaces),
|
|
@@ -720,6 +1080,18 @@ declare function deserializeAnnotatedType(data: TSerializedAnnotatedType): TAtsc
|
|
|
720
1080
|
type FlatOf<T> = T extends {
|
|
721
1081
|
__flat: infer F;
|
|
722
1082
|
} ? F : T extends TAtscriptAnnotatedType ? TAtscriptDataType<T> : unknown;
|
|
1083
|
+
/**
|
|
1084
|
+
* The type of field `K` of `T`: one step of a type path (`T.a.b`) in generated `.d.ts` files.
|
|
1085
|
+
* Each object branch of `T` that declares `K` gives its field (an optional field:
|
|
1086
|
+
* `| undefined`); other branches (`null`, scalars, arrays, objects without `K` — a key only
|
|
1087
|
+
* pattern props match is not a field) give nothing. A path through a union names the union of
|
|
1088
|
+
* the fields, as in the `.as` file.
|
|
1089
|
+
*/
|
|
1090
|
+
type TAtscriptField<T, K extends PropertyKey> = T extends readonly unknown[] ? never : T extends object ? string extends keyof T ? TDeclaredField<T, K> : number extends keyof T ? TDeclaredField<T, K> : K extends keyof T ? T[K] : never : never;
|
|
1091
|
+
/** {@link TAtscriptField} of `T` with an index signature: its declared keys only. */
|
|
1092
|
+
type TDeclaredField<T, K extends PropertyKey> = K extends keyof {
|
|
1093
|
+
[P in keyof T as string extends P ? never : number extends P ? never : P]: 0;
|
|
1094
|
+
} ? T[K & keyof T] : never;
|
|
723
1095
|
/**
|
|
724
1096
|
* Extracts the primary key type from an Atscript annotated type.
|
|
725
1097
|
* If the type has a `__pk` static property (emitted for `@db.table` interfaces
|
|
@@ -749,5 +1121,5 @@ type NavPropsOf<T> = T extends {
|
|
|
749
1121
|
__navProps: infer N extends Record<string, unknown>;
|
|
750
1122
|
} ? N : Record<string, never>;
|
|
751
1123
|
|
|
752
|
-
export { SERIALIZE_VERSION, Validator, ValidatorError, annotate, buildJsonSchema,
|
|
753
|
-
export type { AtscriptExprNode, AtscriptOrderItem, AtscriptQueryComparison, AtscriptQueryFieldRef, AtscriptQueryNode, AtscriptRef, FlatOf, InferDataType, NavPropsOf, OwnPropsOf, PrimaryKeyOf, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptDataType, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TFlattenOptions, TJsonSchema, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TUnionDiscriminator, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValueResolver };
|
|
1124
|
+
export { $ak, $alias, $blk, $end, $f, $fc, $ft, $nr, $ps, $q, $sh, LayerMap, SERIALIZE_VERSION, Validator, ValidatorError, annotate, appliedBlockModules, buildJsonSchema, bundledSchemaOf, coerceForType, coerceScalar, createAnnotatedTypeNode, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, detectDiscriminator, flattenAnnotatedType, forAnnotatedType, fromJsonSchema, invalidateCompiledValidators, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, lazyJsonSchema, mergeJsonSchemas, metaEmpty, pendingStatements, registerCompiledCheck, serializeAnnotatedType, throwFeatureDisabled, watchLayer };
|
|
1125
|
+
export type { AtscriptExprNode, AtscriptOrderItem, AtscriptQueryComparison, AtscriptQueryFieldRef, AtscriptQueryNode, AtscriptRef, FlatOf, InferDataType, NavPropsOf, OwnPropsOf, PrimaryKeyOf, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptDataType, TAtscriptField, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TFlattenOptions, TJsonSchema, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TUnionDiscriminator, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValueResolver };
|