@ghostry/fabricator 0.0.9 → 0.0.11
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/esm/Distribution/index.js +43 -11
- package/dist/esm/Primitive/null/Registry.js +2 -3
- package/dist/esm/Primitive/undefined/Registry.js +2 -3
- package/dist/esm/index.js +1 -0
- package/dist/esm/internal.js +2 -1
- package/dist/types/Adapter/Types.d.ts +3 -2
- package/dist/types/Distribution/index.d.ts +32 -2
- package/dist/types/Enumeration/Types.d.ts +3 -3
- package/dist/types/Fabricator/Types.d.ts +8 -8
- package/dist/types/Primitive/always/Types.d.ts +2 -2
- package/dist/types/Primitive/array/Types.d.ts +4 -4
- package/dist/types/Primitive/bigint/Types.d.ts +2 -2
- package/dist/types/Primitive/boolean/Types.d.ts +2 -2
- package/dist/types/Primitive/choice/Registry.d.ts +3 -3
- package/dist/types/Primitive/choice/Types.d.ts +4 -4
- package/dist/types/Primitive/date/Types.d.ts +2 -2
- package/dist/types/Primitive/derive/Types.d.ts +2 -2
- package/dist/types/Primitive/enum/Registry.d.ts +3 -3
- package/dist/types/Primitive/enum/Types.d.ts +2 -2
- package/dist/types/Primitive/null/Types.d.ts +2 -2
- package/dist/types/Primitive/nullable/Types.d.ts +4 -4
- package/dist/types/Primitive/nullish/Types.d.ts +4 -4
- package/dist/types/Primitive/number/Types.d.ts +2 -2
- package/dist/types/Primitive/object/Types.d.ts +9 -9
- package/dist/types/Primitive/object/compute/Types.d.ts +4 -4
- package/dist/types/Primitive/object/omittable/Types.d.ts +4 -4
- package/dist/types/Primitive/object/optional/Types.d.ts +4 -4
- package/dist/types/Primitive/opaque/Types.d.ts +2 -2
- package/dist/types/Primitive/record/Types.d.ts +11 -6
- package/dist/types/Primitive/recursive/Types.d.ts +12 -8
- package/dist/types/Primitive/recursive/self/Schema.d.ts +4 -4
- package/dist/types/Primitive/recursive/self/Types.d.ts +7 -5
- package/dist/types/Primitive/string/Constants.d.ts +7 -31
- package/dist/types/Primitive/string/Types.d.ts +2 -2
- package/dist/types/Primitive/symbol/Types.d.ts +2 -2
- package/dist/types/Primitive/tuple/Types.d.ts +7 -7
- package/dist/types/Primitive/undefinable/Types.d.ts +4 -4
- package/dist/types/Primitive/undefined/Types.d.ts +2 -2
- package/dist/types/Schema/Core.d.ts +10 -6
- package/dist/types/Schema/Satisfies.d.ts +31 -23
- package/dist/types/Schema/Types.d.ts +31 -18
- package/dist/types/Types.d.ts +24 -0
- package/dist/types/index.d.ts +24 -10
- package/dist/types/internal.d.ts +31 -4
- package/package.json +3 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Adaptations } from "../../../Adapter/Types";
|
|
2
2
|
import type { Produce } from "../../../Random/Types";
|
|
3
|
-
import type { AnySchema,
|
|
4
|
-
import { Kind, Meta, Omitted, type Adaptation, type Produces } from "../../../Types";
|
|
3
|
+
import type { AnySchema, Fabricated as FabricatedBy } from "../../../Schema/Types";
|
|
4
|
+
import { Kind, Meta, Omitted, type Adaptation, type Produced, type Produces } from "../../../Types";
|
|
5
5
|
/**
|
|
6
6
|
* The wrapped field's Schema — the value type when the roll is
|
|
7
7
|
* present-with-a-value.
|
|
@@ -15,7 +15,7 @@ export type Definition = AnySchema;
|
|
|
15
15
|
* `{ a?: T | undefined }` — a real `?:` (from `OmittableKeys`, which also
|
|
16
16
|
* covers this kind) _and_ a real `| undefined`.
|
|
17
17
|
*/
|
|
18
|
-
export type Fabricated<$Definition extends Definition, $Bindings extends unknown[] = []> =
|
|
18
|
+
export type Fabricated<$Definition extends Definition, $Bindings extends unknown[] = []> = FabricatedBy<$Definition, $Bindings> | undefined;
|
|
19
19
|
/**
|
|
20
20
|
* What an optional field's Fabricator returns: the wrapped value, `undefined`,
|
|
21
21
|
* or `Omitted` when the key should not appear — whether from the built-in
|
|
@@ -67,6 +67,6 @@ export interface Core<$Definition extends Definition = Definition, $Adaptations
|
|
|
67
67
|
[Kind]: "object.optional";
|
|
68
68
|
[Meta]: Meta<$Definition>;
|
|
69
69
|
bindings?: unknown[];
|
|
70
|
-
readonly [Produces]?: Fabricated<$Definition, NonNullable<this["bindings"]
|
|
70
|
+
readonly [Produces]?: Produced<Fabricated<$Definition, NonNullable<this["bindings"]>>>;
|
|
71
71
|
readonly [Adaptation]?: $Adaptations;
|
|
72
72
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
2
|
import type { Produce } from "../../Random/Types";
|
|
3
|
-
import { Produces, type Adaptation, type Kind, type Meta } from "../../Types";
|
|
3
|
+
import { Produces, type Adaptation, type Kind, type Meta, type Produced } from "../../Types";
|
|
4
4
|
export type Fabricated<$T> = $T;
|
|
5
5
|
/**
|
|
6
6
|
* A single required member, unlike the two-arm unions kinds with a `whereby`
|
|
@@ -13,6 +13,6 @@ export type Meta<$T = unknown> = {
|
|
|
13
13
|
export type Core<$T = unknown, $Adaptations extends Adaptations = {}> = {
|
|
14
14
|
[Kind]: "opaque";
|
|
15
15
|
[Meta]: Meta<$T>;
|
|
16
|
-
readonly [Produces]?: Fabricated<$T
|
|
16
|
+
readonly [Produces]?: Produced<Fabricated<$T>>;
|
|
17
17
|
readonly [Adaptation]?: $Adaptations;
|
|
18
18
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
2
|
import type { Produce } from "../../Random/Types";
|
|
3
|
-
import type { AnySchema,
|
|
4
|
-
import { Produces, type Adaptation, type Kind, type Meta } from "../../Types";
|
|
3
|
+
import type { AnySchema, Fabricated as FabricatedBy } from "../../Schema/Types";
|
|
4
|
+
import { Produces, type Adaptation, type Kind, type Meta, type Produced } from "../../Types";
|
|
5
5
|
/**
|
|
6
6
|
* A Schema usable as a record's keys: any Schema whose fabricated value is a
|
|
7
7
|
* legal JS property key. Constrained through the phantom `[Produces]` marker
|
|
@@ -13,9 +13,14 @@ import { Produces, type Adaptation, type Kind, type Meta } from "../../Types";
|
|
|
13
13
|
* `symbol` is included deliberately: symbols are legal property keys. Excluding
|
|
14
14
|
* them would arbitrarily narrow what a record can describe. They do not survive
|
|
15
15
|
* `Adapter/TypeBox` — see its `record` case.
|
|
16
|
+
*
|
|
17
|
+
* Matched through the box (`Produced<…>`, `Types.ts`) so that a key schema
|
|
18
|
+
* which may produce `undefined` is rejected whatever the caller's
|
|
19
|
+
* `exactOptionalPropertyTypes` — unboxed and with the flag off, the optional
|
|
20
|
+
* slot already admits `undefined`.
|
|
16
21
|
*/
|
|
17
22
|
export type Key = AnySchema & {
|
|
18
|
-
readonly [Produces]?: string | symbol
|
|
23
|
+
readonly [Produces]?: Produced<string | symbol>;
|
|
19
24
|
};
|
|
20
25
|
export type Value = AnySchema;
|
|
21
26
|
/**
|
|
@@ -29,7 +34,7 @@ type IsKeyspaceOpen<$Key> = string extends $Key ? true : symbol extends $Key ? t
|
|
|
29
34
|
* An open key (`string`, `symbol`, or both) is a plain index signature: no
|
|
30
35
|
* `Partial` — an index signature never guarantees a key is present, and
|
|
31
36
|
* wrapping one would only add a spurious `| undefined`. Carrying
|
|
32
|
-
* `
|
|
37
|
+
* `FabricatedBy<$Key>` rather than hardcoding `string` keeps a mixed `string |
|
|
33
38
|
* symbol` key as `Record<string | symbol, ...>` instead of silently dropping
|
|
34
39
|
* the symbol half.
|
|
35
40
|
*
|
|
@@ -38,7 +43,7 @@ type IsKeyspaceOpen<$Key> = string extends $Key ? true : symbol extends $Key ? t
|
|
|
38
43
|
* a two-member key schema may produce only one. Every key present wants
|
|
39
44
|
* `T.object({ ... })` — what a record over a finite key set degenerates to.
|
|
40
45
|
*/
|
|
41
|
-
export type Fabricated<$Key extends Key = Key, $Value extends Value = Value, $Bindings extends unknown[] = []> = IsKeyspaceOpen<
|
|
46
|
+
export type Fabricated<$Key extends Key = Key, $Value extends Value = Value, $Bindings extends unknown[] = []> = IsKeyspaceOpen<FabricatedBy<$Key, $Bindings>> extends true ? Record<FabricatedBy<$Key, $Bindings> & PropertyKey, FabricatedBy<$Value, $Bindings>> : Partial<Record<FabricatedBy<$Key, $Bindings> & PropertyKey, FabricatedBy<$Value, $Bindings>>>;
|
|
42
47
|
/**
|
|
43
48
|
* How many entries to attempt, uniformly across `[minTried, max]`.
|
|
44
49
|
*
|
|
@@ -79,7 +84,7 @@ export interface Core<$Key extends Key = Key, $Value extends Value = Value, $Ada
|
|
|
79
84
|
[Kind]: "record";
|
|
80
85
|
[Meta]: Meta<$Key, $Value>;
|
|
81
86
|
bindings?: unknown[];
|
|
82
|
-
readonly [Produces]?: Fabricated<$Key, $Value, NonNullable<this["bindings"]
|
|
87
|
+
readonly [Produces]?: Produced<Fabricated<$Key, $Value, NonNullable<this["bindings"]>>>;
|
|
83
88
|
readonly [Adaptation]?: $Adaptations;
|
|
84
89
|
}
|
|
85
90
|
export {};
|
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
|
-
import type { AnySchema,
|
|
3
|
-
import { Produces, type Adaptation, type Kind, type Meta } from "../../Types";
|
|
2
|
+
import type { AnySchema, Fabricated as FabricatedBy } from "../../Schema/Types";
|
|
3
|
+
import { Produces, type Adaptation, type Kind, type Meta, type Produced } from "../../Types";
|
|
4
4
|
/**
|
|
5
5
|
* Fixed point of a recursive schema. A self-referential alias, normally
|
|
6
6
|
* `TS2456` ("`RecursiveValue` circularly references itself") — legal _only_
|
|
7
|
-
* because `
|
|
7
|
+
* because `FabricatedBy`'s second argument is read through an interface member
|
|
8
8
|
* (`this["bindings"]` on every composite `Core`), which TypeScript defers. Do
|
|
9
9
|
* not rewrite as a conditional; that reintroduces the error. See `AGENTS.md`'s
|
|
10
|
-
* "`
|
|
10
|
+
* "`Fabricated<$Schema>`'s `$Bindings`".
|
|
11
11
|
*
|
|
12
12
|
* Wherever `self` sits in `$Body` (nested through `array`/`object`/`tuple`/
|
|
13
13
|
* etc., each forwarding `this["bindings"]`), it reads `bindings[0]` —
|
|
14
14
|
* `RecursiveValue<$Body>` itself, closing the loop.
|
|
15
15
|
*/
|
|
16
|
-
export type RecursiveValue<$Body> =
|
|
16
|
+
export type RecursiveValue<$Body> = FabricatedBy<$Body, [
|
|
17
|
+
RecursiveValue<$Body>
|
|
18
|
+
]>;
|
|
17
19
|
export type Fabricated<$Body> = RecursiveValue<$Body>;
|
|
18
20
|
/**
|
|
19
21
|
* `terminal` is what `self` expands into at `depth.max`, in place of `body`.
|
|
@@ -24,7 +26,9 @@ export type Fabricated<$Body> = RecursiveValue<$Body>;
|
|
|
24
26
|
* already rejects a wrong terminal (e.g. missing a required field `body` has),
|
|
25
27
|
* and nothing downstream needs `terminal`'s precise type (`Core`'s `[Produces]`
|
|
26
28
|
* depends only on `$Body`). A bare-vs-generic comparison rejects the same wrong
|
|
27
|
-
* terminal either way.
|
|
29
|
+
* terminal either way. The constraint matches through the box (`Produced<…>`,
|
|
30
|
+
* `Types.ts`): unboxed, a caller compiling without `exactOptionalPropertyTypes`
|
|
31
|
+
* could pass a terminal producing `undefined` for any body at all.
|
|
28
32
|
*
|
|
29
33
|
* Omitted, `terminal` is derived from `body` (`Terminate.ts`): every `self`
|
|
30
34
|
* behind a declining kind is rewritten into a stop (empty array/record,
|
|
@@ -52,7 +56,7 @@ export type Whereby<$Body> = {
|
|
|
52
56
|
max: number;
|
|
53
57
|
};
|
|
54
58
|
terminal?: (AnySchema & {
|
|
55
|
-
readonly [Produces]?: RecursiveValue<$Body
|
|
59
|
+
readonly [Produces]?: Produced<RecursiveValue<$Body>>;
|
|
56
60
|
}) | undefined;
|
|
57
61
|
};
|
|
58
62
|
/**
|
|
@@ -77,6 +81,6 @@ export type Meta<$Body = unknown> = {
|
|
|
77
81
|
export type Core<$Body = unknown, $Adaptations extends Adaptations = {}> = {
|
|
78
82
|
[Kind]: "recursive";
|
|
79
83
|
[Meta]: Meta<$Body>;
|
|
80
|
-
readonly [Produces]?: Fabricated<$Body
|
|
84
|
+
readonly [Produces]?: Produced<Fabricated<$Body>>;
|
|
81
85
|
readonly [Adaptation]?: $Adaptations;
|
|
82
86
|
};
|
|
@@ -19,10 +19,10 @@ export interface Schema<$Adaptations extends Adaptations = {}> extends Core<$Ada
|
|
|
19
19
|
/**
|
|
20
20
|
* Check that this schema's fabricated value type is assignable to `$Target`.
|
|
21
21
|
* Identity at runtime; the target is not carried into later chained calls —
|
|
22
|
-
* see `object/Schema.ts`'s `satisfies`. Outside `
|
|
23
|
-
* `Fabricated` is `unknown` — a `self` has no value
|
|
24
|
-
* `.satisfies` on the enclosing
|
|
25
|
-
* tree.
|
|
22
|
+
* see `object/Schema.ts`'s `satisfies`. Outside `Schema/Types.ts`'s
|
|
23
|
+
* `$Bindings`, this kind's `Fabricated` is `unknown` — a `self` has no value
|
|
24
|
+
* of its own. Put `.satisfies` on the enclosing
|
|
25
|
+
* `T.recursive(...).whereby(...)` to check the tree.
|
|
26
26
|
*/
|
|
27
27
|
satisfies<$Target>(this: this & SatisfiesThis<Fabricated, $Target>): Schema<$Adaptations>;
|
|
28
28
|
adapt: <const $Adapter extends Adapter, $Returnable extends ReturnType<$Adapter["convert"]>>(adapter: $Adapter, produce: (adapting: Adapting<Schema<$Adaptations>>) => $Returnable) => Schema<WithAdaptations<$Adaptations, AdaptationEntry<$Adapter, $Returnable>>>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { Adaptations } from "../../../Adapter/Types";
|
|
2
|
-
import { Produces, type Adaptation, type Kind, type Meta } from "../../../Types";
|
|
2
|
+
import { Produces, type Adaptation, type Produced, type Kind, type Meta } from "../../../Types";
|
|
3
3
|
/**
|
|
4
4
|
* The value a `self` reference resolves to at this position — whatever the
|
|
5
5
|
* enclosing `T.recursive`'s current expansion binds it to. Reads `bindings[0]`,
|
|
6
6
|
* the analogue of TypeBox's `TThis` reading `this['params'][0]`; see
|
|
7
|
-
* `Schema/Types.ts`'s `
|
|
7
|
+
* `Schema/Types.ts`'s `Fabricated` for what `bindings` is and why it's a
|
|
8
|
+
* tuple.
|
|
8
9
|
*
|
|
9
10
|
* Unlike every other kind, `self` has no type parameter of its own — its whole
|
|
10
11
|
* value comes from outside, threaded in structurally by whatever composite
|
|
@@ -15,13 +16,14 @@ export type Fabricated<$Bindings extends unknown[] = []> = $Bindings[0];
|
|
|
15
16
|
export type Meta = Record<string, never>;
|
|
16
17
|
/**
|
|
17
18
|
* An `interface`, not a `type` alias, so `this["bindings"]` resolves — the
|
|
18
|
-
* entire mechanism this kind exists for. See `AGENTS.md`'s
|
|
19
|
-
* `$Bindings`" section for why threading requires
|
|
19
|
+
* entire mechanism this kind exists for. See `AGENTS.md`'s
|
|
20
|
+
* "`Fabricated<$Schema>`'s `$Bindings`" section for why threading requires
|
|
21
|
+
* this.
|
|
20
22
|
*/
|
|
21
23
|
export interface Core<$Adaptations extends Adaptations = {}> {
|
|
22
24
|
[Kind]: "recursive.self";
|
|
23
25
|
[Meta]: Meta;
|
|
24
26
|
bindings?: unknown[];
|
|
25
|
-
readonly [Produces]?: Fabricated<NonNullable<this["bindings"]
|
|
27
|
+
readonly [Produces]?: Produced<Fabricated<NonNullable<this["bindings"]>>>;
|
|
26
28
|
readonly [Adaptation]?: $Adaptations;
|
|
27
29
|
}
|
|
@@ -8,39 +8,15 @@ import type { CodepointRange } from "./Types";
|
|
|
8
8
|
* export alongside `export type Fabricated = string` makes the module a value
|
|
9
9
|
* module, and `export * as string` in `Primitive/namespace.ts` then resolves
|
|
10
10
|
* `string` to the namespace — a cycle that drops `[Kind]`/`[Meta]` off `Core`.
|
|
11
|
+
*
|
|
12
|
+
* Annotated member-by-member, like `classes`, because it is public API and JSR
|
|
13
|
+
* rejects an inferred `const` type there.
|
|
11
14
|
*/
|
|
12
15
|
export declare const unicode: {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
* This is the default composition for strings.
|
|
19
|
-
*/
|
|
20
|
-
scalars: {
|
|
21
|
-
from: number;
|
|
22
|
-
to: number;
|
|
23
|
-
}[];
|
|
24
|
-
/**
|
|
25
|
-
* The entire Unicode codespace, U+0000–U+10FFFF, including the surrogate
|
|
26
|
-
* block — so it can yield lone surrogates and thus ill-formed UTF-16. Opt in
|
|
27
|
-
* for lone surrogates and other ill-formed UTF-16 when fuzzing.
|
|
28
|
-
*/
|
|
29
|
-
codespace: {
|
|
30
|
-
from: number;
|
|
31
|
-
to: number;
|
|
32
|
-
};
|
|
33
|
-
/** The Basic Multilingual Plane, U+0000–U+FFFF. */
|
|
34
|
-
bmp: {
|
|
35
|
-
from: number;
|
|
36
|
-
to: number;
|
|
37
|
-
};
|
|
38
|
-
/** Printable ASCII, U+0020–U+007E. */
|
|
39
|
-
ascii: {
|
|
40
|
-
from: number;
|
|
41
|
-
to: number;
|
|
42
|
-
};
|
|
43
|
-
/** An arbitrary inclusive code point range. */
|
|
16
|
+
scalars: ReadonlyArray<CodepointRange>;
|
|
17
|
+
codespace: CodepointRange;
|
|
18
|
+
bmp: CodepointRange;
|
|
19
|
+
ascii: CodepointRange;
|
|
44
20
|
range: (from: number, to: number) => CodepointRange;
|
|
45
21
|
};
|
|
46
22
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
2
|
import type { InputLength, Length } from "../../Bound";
|
|
3
3
|
import type { Produce } from "../../Random/Types";
|
|
4
|
-
import type { Adaptation, Kind, Meta, Produces } from "../../Types";
|
|
4
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../../Types";
|
|
5
5
|
import type { classes } from "./Constants";
|
|
6
6
|
/** An inclusive range of Unicode code points. */
|
|
7
7
|
export type CodepointRange = {
|
|
@@ -68,6 +68,6 @@ export type Meta = {
|
|
|
68
68
|
export type Core<$Meta extends Meta = Meta, $Adaptations extends Adaptations = {}> = {
|
|
69
69
|
[Kind]: "string";
|
|
70
70
|
[Meta]: $Meta;
|
|
71
|
-
readonly [Produces]?: Fabricated
|
|
71
|
+
readonly [Produces]?: Produced<Fabricated>;
|
|
72
72
|
readonly [Adaptation]?: $Adaptations;
|
|
73
73
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
2
|
import type { Produce } from "../../Random/Types";
|
|
3
|
-
import type { Adaptation, Kind, Meta, Produces } from "../../Types";
|
|
3
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../../Types";
|
|
4
4
|
export type Fabricated = symbol;
|
|
5
5
|
/**
|
|
6
6
|
* A symbol keyed via `keyed`, or unkeyed if `key` is absent, optionally
|
|
@@ -14,6 +14,6 @@ export type Meta = {
|
|
|
14
14
|
export type Core<$Meta extends Meta = Meta, $Adaptations extends Adaptations = {}> = {
|
|
15
15
|
[Kind]: "symbol";
|
|
16
16
|
[Meta]: $Meta;
|
|
17
|
-
readonly [Produces]?: Fabricated
|
|
17
|
+
readonly [Produces]?: Produced<Fabricated>;
|
|
18
18
|
readonly [Adaptation]?: $Adaptations;
|
|
19
19
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
2
|
import type { Produce } from "../../Random/Types";
|
|
3
|
-
import type { AnySchema,
|
|
4
|
-
import type { Adaptation, Kind, Meta, Produces } from "../../Types";
|
|
3
|
+
import type { AnySchema, Fabricated as FabricatedBy } from "../../Schema/Types";
|
|
4
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../../Types";
|
|
5
5
|
/** The schema occupying a single tuple slot. */
|
|
6
6
|
export type Item = AnySchema;
|
|
7
7
|
/**
|
|
@@ -19,9 +19,9 @@ export type Items = ReadonlyArray<Item>;
|
|
|
19
19
|
* `keyof` a still-abstract `ReadonlyArray<Item>` (this type's default, and what
|
|
20
20
|
* a kind-local `Fabrication<$Fabricator>` resolves against for the _bare_,
|
|
21
21
|
* unparameterized `Fabricator`) pulls in `"length"`/method keys that don't
|
|
22
|
-
* resolve to a sensible `
|
|
23
|
-
* concrete tuple but silently produces a malformed bare type, which then
|
|
24
|
-
* `.schema.as`'s contravariant parameter check against any concrete
|
|
22
|
+
* resolve to a sensible `FabricatedBy`. A homomorphic version type-checks for
|
|
23
|
+
* any concrete tuple but silently produces a malformed bare type, which then
|
|
24
|
+
* fails `.schema.as`'s contravariant parameter check against any concrete
|
|
25
25
|
* instantiation (`Fabrication.types.test.ts`). Matching a fixed-length tuple
|
|
26
26
|
* pattern at every step avoids it, as `TupleItems` does for `ToTypeBox`. The
|
|
27
27
|
* fallback (a genuinely widened, arity-less array) is a plain `Array<...>`,
|
|
@@ -30,7 +30,7 @@ export type Items = ReadonlyArray<Item>;
|
|
|
30
30
|
export type Fabricated<$Items extends Items = Items, $Bindings extends unknown[] = []> = $Items extends readonly [] ? [] : $Items extends readonly [
|
|
31
31
|
infer $Head extends Item,
|
|
32
32
|
...infer $Rest extends Items
|
|
33
|
-
] ? [
|
|
33
|
+
] ? [FabricatedBy<$Head, $Bindings>, ...Fabricated<$Rest, $Bindings>] : Array<FabricatedBy<$Items[number], $Bindings>>;
|
|
34
34
|
/**
|
|
35
35
|
* No `whereby`: arity is fixed at `T.tuple([...])` and there is nothing left to
|
|
36
36
|
* configure. Unlike `array`'s two-variant union this needs no "config or
|
|
@@ -47,6 +47,6 @@ export interface Core<$Items extends Items = Items, $Adaptations extends Adaptat
|
|
|
47
47
|
[Kind]: "tuple";
|
|
48
48
|
[Meta]: Meta<$Items>;
|
|
49
49
|
bindings?: unknown[];
|
|
50
|
-
readonly [Produces]?: Fabricated<$Items, NonNullable<this["bindings"]
|
|
50
|
+
readonly [Produces]?: Produced<Fabricated<$Items, NonNullable<this["bindings"]>>>;
|
|
51
51
|
readonly [Adaptation]?: $Adaptations;
|
|
52
52
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
2
|
import type { Produce } from "../../Random/Types";
|
|
3
|
-
import type { AnySchema,
|
|
4
|
-
import type { Adaptation, Kind, Meta, Produces } from "../../Types";
|
|
3
|
+
import type { AnySchema, Fabricated as FabricatedBy } from "../../Schema/Types";
|
|
4
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../../Types";
|
|
5
5
|
/** The wrapped schema — the value type produced when the roll isn't `undefined`. */
|
|
6
6
|
export type Definition = AnySchema;
|
|
7
|
-
export type Fabricated<$Definition extends Definition, $Bindings extends unknown[] = []> =
|
|
7
|
+
export type Fabricated<$Definition extends Definition, $Bindings extends unknown[] = []> = FabricatedBy<$Definition, $Bindings> | undefined;
|
|
8
8
|
export type { Outcome } from "./Outcomes";
|
|
9
9
|
/**
|
|
10
10
|
* Relative weights for `.weighted(...)`, keyed by `Outcome`. Keys optional — an
|
|
@@ -43,6 +43,6 @@ export interface Core<$Definition extends Definition = Definition, $Adaptations
|
|
|
43
43
|
[Kind]: "undefinable";
|
|
44
44
|
[Meta]: Meta<$Definition>;
|
|
45
45
|
bindings?: unknown[];
|
|
46
|
-
readonly [Produces]?: Fabricated<$Definition, NonNullable<this["bindings"]
|
|
46
|
+
readonly [Produces]?: Produced<Fabricated<$Definition, NonNullable<this["bindings"]>>>;
|
|
47
47
|
readonly [Adaptation]?: $Adaptations;
|
|
48
48
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types";
|
|
2
|
-
import type { Adaptation, Kind, Meta, Produces } from "../../Types";
|
|
2
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../../Types";
|
|
3
3
|
export type Fabricated = undefined;
|
|
4
4
|
/**
|
|
5
5
|
* Nothing to configure: `undefined` has exactly one possible value, so there is
|
|
@@ -10,6 +10,6 @@ export type Meta = Record<string, never>;
|
|
|
10
10
|
export type Core<$Meta extends Meta = Meta, $Adaptations extends Adaptations = {}> = {
|
|
11
11
|
[Kind]: "undefined";
|
|
12
12
|
[Meta]: $Meta;
|
|
13
|
-
readonly [Produces]?: Fabricated
|
|
13
|
+
readonly [Produces]?: Produced<Fabricated>;
|
|
14
14
|
readonly [Adaptation]?: $Adaptations;
|
|
15
15
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Adaptations } from "../Adapter/Types";
|
|
2
2
|
import type { Kind as SchemaKind } from "../Primitive";
|
|
3
|
-
import { Adaptation, Kind, Meta, type Produces } from "../Types";
|
|
3
|
+
import { Adaptation, Kind, Meta, type Produced, type Produces } from "../Types";
|
|
4
4
|
import { type PlainObject } from "../Utility/Types";
|
|
5
5
|
/**
|
|
6
6
|
* Normalize a Schema, a builder namespace, or an already-built Fabricator into
|
|
@@ -16,20 +16,24 @@ import { type PlainObject } from "../Utility/Types";
|
|
|
16
16
|
* enclosing `object`/`array`, which is where every adapter actually reads it
|
|
17
17
|
* from (see `Adapter/TypeBox`'s `build`, which walks `[Meta].definition`).
|
|
18
18
|
*
|
|
19
|
-
* `$
|
|
20
|
-
*
|
|
19
|
+
* `$Produces` is threaded through in the type only (never read or written at
|
|
20
|
+
* runtime) so `Fabricated` stays precise across a `toSchema` call — dropping it
|
|
21
21
|
* would make every normalized field (e.g. every `object`/`array` element) fall
|
|
22
|
-
* back to `unknown`.
|
|
22
|
+
* back to `unknown`. It is inferred through the box (`Produced<$Produces>`,
|
|
23
|
+
* `Types.ts`), not as the brand's whole type: inferred against a bare
|
|
24
|
+
* `[Produces]?: $Produces`, a value type's own `undefined` is absorbed into the
|
|
25
|
+
* optional slot whenever the caller compiles without
|
|
26
|
+
* `exactOptionalPropertyTypes`.
|
|
23
27
|
*/
|
|
24
28
|
export declare function toSchema<$Kind extends string, $Meta extends PlainObject, $Produces = unknown, $Adaptations extends Adaptations = Adaptations>(value: {
|
|
25
29
|
[Kind]: $Kind;
|
|
26
30
|
[Meta]: $Meta;
|
|
27
|
-
[Produces]?:
|
|
31
|
+
[Produces]?: Produced<$Produces>;
|
|
28
32
|
[Adaptation]?: $Adaptations;
|
|
29
33
|
}): {
|
|
30
34
|
[Kind]: $Kind;
|
|
31
35
|
[Meta]: $Meta;
|
|
32
|
-
readonly [Produces]?:
|
|
36
|
+
readonly [Produces]?: Produced<$Produces>;
|
|
33
37
|
readonly [Adaptation]?: $Adaptations;
|
|
34
38
|
};
|
|
35
39
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { NaiveFabricator } from "../Fabricator/Types";
|
|
2
|
-
import type { Buildable, Produces } from "../Types";
|
|
3
|
-
import type {
|
|
2
|
+
import type { Buildable, Produced, Produces } from "../Types";
|
|
3
|
+
import type { Fabricated } from "./Types";
|
|
4
4
|
/**
|
|
5
5
|
* The `this`-parameter constraint every kind's `.satisfies<$Target>()`
|
|
6
6
|
* intersects onto the receiver, so a mismatch names both sides on the call that
|
|
@@ -8,13 +8,14 @@ import type { ValueOf } from "./Types";
|
|
|
8
8
|
* where the error lands on `inner` rather than the outer object.
|
|
9
9
|
*
|
|
10
10
|
* `$Produced` is each kind's own `Fabricated<…>` (or `Resolved<…>` for `derive`
|
|
11
|
-
* / `object.compute`, which have no `Fabricated` alias). A shared
|
|
12
|
-
* `
|
|
13
|
-
* any`, which is why each kind still names its own produced type.
|
|
14
|
-
* wrapped in a 1-tuple so it does not distribute over unions: a
|
|
15
|
-
* extends $Target ? unknown : Error` on `"x" | null` becomes
|
|
16
|
-
* which is `unknown`, and
|
|
17
|
-
*
|
|
11
|
+
* / `object.compute`, which have no `Fabricated` alias). A shared schema-level
|
|
12
|
+
* `Fabricated<this>` form errors in the same place but reports `"schema
|
|
13
|
+
* produces": any`, which is why each kind still names its own produced type.
|
|
14
|
+
* The check is wrapped in a 1-tuple so it does not distribute over unions: a
|
|
15
|
+
* bare `$Produced extends $Target ? unknown : Error` on `"x" | null` becomes
|
|
16
|
+
* `unknown | Error`, which is `unknown`, and
|
|
17
|
+
* `T.nullable(…).satisfies<string>()` would pass. Other signatures that were
|
|
18
|
+
* considered, and why they are not this:
|
|
18
19
|
*
|
|
19
20
|
* - A rest tuple `...check: D extends T ? [] : [error]` errors in the right place
|
|
20
21
|
* but says "Expected 1 arguments, but got 0".
|
|
@@ -26,11 +27,12 @@ import type { ValueOf } from "./Types";
|
|
|
26
27
|
* that changes a field's type is not re-checked. Put `.satisfies` last to check
|
|
27
28
|
* the final shape. Carrying `$Target` on every `Schema` interface would add a
|
|
28
29
|
* type parameter to all 25 kinds, and `.as`'s contravariant parameter check is
|
|
29
|
-
* where a variance shift on those interfaces surfaces — `Types.ts`'s
|
|
30
|
-
* note records that same check breaking from a constraint on
|
|
31
|
-
* return is `Schema`, not polymorphic `this`, because
|
|
32
|
-
* `T.number.integer` / `T.symbol` are `Schema & { whereby, … }`
|
|
33
|
-
* return is not assignable into those extra members — same as
|
|
30
|
+
* where a variance shift on those interfaces surfaces — `Types.ts`'s
|
|
31
|
+
* `Fabricated` note records that same check breaking from a constraint on
|
|
32
|
+
* `$Schema`. The return is `Schema`, not polymorphic `this`, because
|
|
33
|
+
* `T.date.past` / `T.number.integer` / `T.symbol` are `Schema & { whereby, … }`
|
|
34
|
+
* and a `this` return is not assignable into those extra members — same as
|
|
35
|
+
* `.as()`.
|
|
34
36
|
*/
|
|
35
37
|
export type SatisfiesThis<$Produced, $Target> = [$Produced] extends [$Target] ? unknown : {
|
|
36
38
|
"schema produces": $Produced;
|
|
@@ -60,8 +62,8 @@ export declare function schemaSatisfies<$Receiver>(this: $Receiver): $Receiver;
|
|
|
60
62
|
* `note?: string | undefined` (present, present-as-`undefined`, or omitted).
|
|
61
63
|
* `T.omittable` produces `note?: string` and does.
|
|
62
64
|
*/
|
|
63
|
-
export type SatisfiedBy<$Target, $Buildable> =
|
|
64
|
-
produces:
|
|
65
|
+
export type SatisfiedBy<$Target, $Buildable> = Fabricated<$Buildable> extends $Target ? true : {
|
|
66
|
+
produces: Fabricated<$Buildable>;
|
|
65
67
|
required: $Target;
|
|
66
68
|
};
|
|
67
69
|
/**
|
|
@@ -71,14 +73,20 @@ export type SatisfiedBy<$Target, $Buildable> = ValueOf<$Buildable> extends $Targ
|
|
|
71
73
|
*
|
|
72
74
|
* `fabricate?: never` on the Schema arm is required. A built Fabricator has no
|
|
73
75
|
* `[Produces]`, and a type that lacks an optional property still matches `{
|
|
74
|
-
* readonly [Produces]?:
|
|
75
|
-
* `Types.ts`. Without the exclusion, a non-conforming
|
|
76
|
-
* Schema arm unchecked. Ordered overloads do not
|
|
77
|
-
* resolution falls through a failing overload onto the
|
|
78
|
-
* Fabricator rejected by `NaiveFabricator<$Target>` would still
|
|
79
|
-
* the Schema arm.
|
|
76
|
+
* readonly [Produces]?: Produced<$Target> }` — the same trap `Types.ts`'s
|
|
77
|
+
* `Fabricated` documents at `Types.ts`. Without the exclusion, a non-conforming
|
|
78
|
+
* Fabricator would pass the Schema arm unchecked. Ordered overloads do not
|
|
79
|
+
* replace this: overload resolution falls through a failing overload onto the
|
|
80
|
+
* next one, so a Fabricator rejected by `NaiveFabricator<$Target>` would still
|
|
81
|
+
* be accepted by the Schema arm.
|
|
82
|
+
*
|
|
83
|
+
* The Schema arm matches through the box (`Produced<$Target>`, `../Types.ts`),
|
|
84
|
+
* never `[Produces]?: $Target`: a caller compiling without
|
|
85
|
+
* `exactOptionalPropertyTypes` reads that optional slot as `$Target |
|
|
86
|
+
* undefined`, so a schema that may produce `undefined` would pass a target that
|
|
87
|
+
* excludes it.
|
|
80
88
|
*/
|
|
81
89
|
export declare function satisfies<$Target>(buildable: NaiveFabricator<$Target> | (Buildable & {
|
|
82
90
|
fabricate?: never;
|
|
83
|
-
readonly [Produces]?:
|
|
91
|
+
readonly [Produces]?: Produced<$Target>;
|
|
84
92
|
})): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Adaptations } from "../Adapter/Types";
|
|
2
2
|
import type { NaiveFabricator } from "../Fabricator/Types";
|
|
3
3
|
import type { Kind as SchemaKind } from "../Primitive";
|
|
4
|
-
import type { Adaptation, Kind, Meta, Produces } from "../Types";
|
|
4
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../Types";
|
|
5
5
|
import type { PlainObject } from "../Utility/Types";
|
|
6
6
|
/**
|
|
7
7
|
* The broadest Schema constraint: some kind, some config. Used by compound
|
|
@@ -25,7 +25,8 @@ export type AnySchema = {
|
|
|
25
25
|
* `fabricate: () => $T` already plays for `NaiveFabricator<infer $T>`, one step
|
|
26
26
|
* earlier, before a Schema has been built. `[Produces]` is always optional
|
|
27
27
|
* (never assigned at runtime — see `DeepMerge`'s handling of optional-only keys
|
|
28
|
-
* for why that matters)
|
|
28
|
+
* for why that matters), and what it holds is the value type boxed,
|
|
29
|
+
* `Produced<…>` (`Types.ts`), never the value type itself.
|
|
29
30
|
*
|
|
30
31
|
* A built Fabricator carries no `[Produces]` — its value type lives on
|
|
31
32
|
* `fabricate: () => $T` (`Fabricator/Types.ts`) — yet `toSchema` lets one stand
|
|
@@ -55,25 +56,37 @@ export type AnySchema = {
|
|
|
55
56
|
* this is closer to a de-Bruijn-style resolution environment — kept as a tuple,
|
|
56
57
|
* as TypeBox's is, so a second bound self-reference could occupy index 1 later
|
|
57
58
|
* without restructuring, though nothing today defines what that would mean.
|
|
58
|
-
* `test/
|
|
59
|
+
* `test/FabricatedBindings.types.test.ts` is the only thing asserting any of
|
|
60
|
+
* it.
|
|
59
61
|
*
|
|
60
62
|
* Three details here are load-bearing, and each looks simplifiable:
|
|
61
63
|
*
|
|
62
64
|
* - **Indexed access, not a conditional `infer`.** Writing this as `($Schema & {
|
|
63
|
-
* bindings: $Bindings }) extends { [Produces]?: infer $T } ? ...`
|
|
64
|
-
* `TS2615` — the bindings do not collapse, and the error surfaces
|
|
65
|
-
* `unknown[] & [...]`.
|
|
66
|
-
* - **`
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* `
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
65
|
+
* bindings: $Bindings }) extends { [Produces]?: Produced<infer $T> } ? ...`
|
|
66
|
+
* fails with `TS2615` — the bindings do not collapse, and the error surfaces
|
|
67
|
+
* as `unknown[] & [...]`.
|
|
68
|
+
* - **`Exclude<…, undefined>` is applied to the box, never to a value type.**
|
|
69
|
+
* Indexing an optional property adds `| undefined`, and `Exclude` strips
|
|
70
|
+
* every `undefined` in the union it is handed. That is exact only because the
|
|
71
|
+
* union is `Produced<…> | undefined`: the box is never `undefined`, so the
|
|
72
|
+
* one stripped is the modifier's, and the _legitimate_ `undefined` in
|
|
73
|
+
* `nullish`/`undefinable`/`object.optional`/`undefined`'s own value types
|
|
74
|
+
* sits inside the box, out of reach. No reader of an _unboxed_ value type can
|
|
75
|
+
* be exact: `Required<>` spares a declared `undefined` only when the consumer
|
|
76
|
+
* compiles with `exactOptionalPropertyTypes`, and strips it like `Exclude`
|
|
77
|
+
* when they do not. `Required<…>[typeof Produces]["value"]` is not a way to
|
|
78
|
+
* keep `Required<>` either — it fails `TS2536`.
|
|
79
|
+
* `test/Fabricated.types.test.ts`, compiled with the flag off by
|
|
80
|
+
* `tsconfig.inexact.json`, is the guard.
|
|
81
|
+
* - **The `extends` guard names the box, and belongs inside, not on the
|
|
82
|
+
* parameter.** Matched against `{ [Produces]?: unknown }`, the read has no
|
|
83
|
+
* `"value"` to index (`TS2536`). Constraining `$Schema` instead would force
|
|
84
|
+
* `[Produces]` onto `AnySchema` (which deliberately omits it), and that
|
|
85
|
+
* shifts variance enough to break `.as`'s contravariant parameter check in
|
|
86
|
+
* `Fabrication.types.test.ts`.
|
|
74
87
|
*/
|
|
75
|
-
export type
|
|
76
|
-
readonly [Produces]?: unknown
|
|
77
|
-
} ?
|
|
88
|
+
export type Fabricated<$Schema, $Bindings extends unknown[] = []> = $Schema extends NaiveFabricator<infer $T> ? $T : $Schema extends {
|
|
89
|
+
readonly [Produces]?: Produced<unknown>;
|
|
90
|
+
} ? Exclude<($Schema & {
|
|
78
91
|
bindings: $Bindings;
|
|
79
|
-
}
|
|
92
|
+
})[typeof Produces], undefined>["value"] : unknown;
|
package/dist/types/Types.d.ts
CHANGED
|
@@ -5,6 +5,30 @@ export declare const Meta: unique symbol;
|
|
|
5
5
|
export declare const Produces: unique symbol;
|
|
6
6
|
export declare const Replace: unique symbol;
|
|
7
7
|
export declare const Fixed: unique symbol;
|
|
8
|
+
/**
|
|
9
|
+
* What a Schema's phantom `[Produces]` holds: its value type, boxed.
|
|
10
|
+
*
|
|
11
|
+
* `[Produces]` is optional, and reading an optional property always yields `|
|
|
12
|
+
* undefined`. Whether that `undefined` can be told apart from one the value
|
|
13
|
+
* type itself contains depends on the _reader's_ `exactOptionalPropertyTypes`.
|
|
14
|
+
* With the flag on, the modifier contributes a distinct "missing" marker that
|
|
15
|
+
* `Required<>` strips on its own. With it off, the property is a plain `X |
|
|
16
|
+
* undefined`, and nothing recovers whether `X` held an `undefined` of its own —
|
|
17
|
+
* `Required<>` strips it exactly as `Exclude<…, undefined>` does, and inferring
|
|
18
|
+
* `$T` against `{ [Produces]?: $T }` absorbs it into the optional slot. A
|
|
19
|
+
* consumer compiles this library's declarations under their own tsconfig, so
|
|
20
|
+
* the flag is not this package's to choose.
|
|
21
|
+
*
|
|
22
|
+
* Boxed, the modifier's `undefined` sits beside the box and the value type's
|
|
23
|
+
* sits inside it, so the two never share a union. Stripping `undefined` off
|
|
24
|
+
* `Produced<X> | undefined` is exact under either setting — for `Fabricated`
|
|
25
|
+
* (`Schema/Types.ts`), and for anything that matches or infers through the
|
|
26
|
+
* brand, which has to name the box too: `{ readonly [Produces]?: Produced<$T>
|
|
27
|
+
* }`, never `{ readonly [Produces]?: $T }`.
|
|
28
|
+
*/
|
|
29
|
+
export type Produced<$T> = {
|
|
30
|
+
readonly value: $T;
|
|
31
|
+
};
|
|
8
32
|
/**
|
|
9
33
|
* Tags a `Salt` as composing onto whatever base is in effect, rather than
|
|
10
34
|
* replacing it — the reading a bare `salt` has everywhere else in this library.
|