@orkestrel/contract 0.0.5 → 0.0.7
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/src/core/index.cjs +1075 -0
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +3565 -8
- package/dist/src/core/index.d.ts +3565 -8
- package/dist/src/core/index.js +1050 -1
- package/dist/src/core/index.js.map +1 -1
- package/package.json +7 -2
- package/dist/src/core/combinators.d.ts +0 -384
- package/dist/src/core/compilers.d.ts +0 -192
- package/dist/src/core/constants.d.ts +0 -40
- package/dist/src/core/helpers.d.ts +0 -159
- package/dist/src/core/parsers.d.ts +0 -310
- package/dist/src/core/shapers.d.ts +0 -231
- package/dist/src/core/types.d.ts +0 -508
- package/dist/src/core/validators.d.ts +0 -717
package/dist/src/core/types.d.ts
DELETED
|
@@ -1,508 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Discriminated success branch of a {@link Result}.
|
|
3
|
-
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* Used for operations that can succeed or fail without throwing.
|
|
6
|
-
*/
|
|
7
|
-
export interface Success<T> {
|
|
8
|
-
readonly success: true;
|
|
9
|
-
readonly value: T;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Discriminated failure branch of a {@link Result}.
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* Carries the error value when an operation does not succeed.
|
|
16
|
-
*/
|
|
17
|
-
export interface Failure<E> {
|
|
18
|
-
readonly success: false;
|
|
19
|
-
readonly error: E;
|
|
20
|
-
}
|
|
21
|
-
/** Discriminated union for operations that can succeed or fail without throwing. */
|
|
22
|
-
export type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
23
|
-
/**
|
|
24
|
-
* A field path into a record: a single key, or an ordered list of keys to
|
|
25
|
-
* descend through nested objects.
|
|
26
|
-
*
|
|
27
|
-
* @remarks
|
|
28
|
-
* A single `string` is ONE key — it is never split on `.`, so keys that contain
|
|
29
|
-
* dots stay safe. Use a `readonly string[]` to descend into nested objects.
|
|
30
|
-
*/
|
|
31
|
-
export type FieldPath = string | readonly string[];
|
|
32
|
-
/** A runtime type guard: returns `true` when `value` satisfies `T` and narrows it. */
|
|
33
|
-
export type Guard<T> = (value: unknown) => value is T;
|
|
34
|
-
/** Extract the guarded type `T` from a `Guard<T>`. */
|
|
35
|
-
export type GuardType<G> = G extends Guard<infer T> ? T : never;
|
|
36
|
-
/**
|
|
37
|
-
* A mapping of string keys to guards.
|
|
38
|
-
*
|
|
39
|
-
* @remarks
|
|
40
|
-
* The shape parameter for the `recordOf`, `pickOf`, and `omitOf` combinators.
|
|
41
|
-
*/
|
|
42
|
-
export type GuardsShape = Readonly<Record<string, Guard<unknown>>>;
|
|
43
|
-
/** Resolve a {@link GuardsShape} to a readonly object type of its guarded property types. */
|
|
44
|
-
export type FromGuards<G extends GuardsShape> = Readonly<{
|
|
45
|
-
[K in keyof G]: GuardType<G[K]>;
|
|
46
|
-
}>;
|
|
47
|
-
/**
|
|
48
|
-
* Like {@link FromGuards}, but every key listed in `K` becomes a true optional
|
|
49
|
-
* member (`?`) rather than a required key widened with `| undefined`.
|
|
50
|
-
*
|
|
51
|
-
* @remarks
|
|
52
|
-
* A key present in `K` may be omitted entirely; if present, its value must
|
|
53
|
-
* still satisfy the key's guard — a present key holding `undefined` is not
|
|
54
|
-
* accepted.
|
|
55
|
-
*
|
|
56
|
-
* @typeParam S - The full guard shape
|
|
57
|
-
* @typeParam K - Tuple of keys to make optional
|
|
58
|
-
*/
|
|
59
|
-
export type OptionalFromGuards<S extends GuardsShape, K extends ReadonlyArray<keyof S>> = Readonly<{
|
|
60
|
-
[P in Exclude<keyof S, K[number]>]: FromGuards<S>[P];
|
|
61
|
-
} & {
|
|
62
|
-
[P in Extract<keyof S, K[number]>]?: FromGuards<S>[P];
|
|
63
|
-
}>;
|
|
64
|
-
/** Map a tuple of element guards to a readonly tuple of their guarded types. */
|
|
65
|
-
export type TupleFromGuards<Ts extends ReadonlyArray<Guard<unknown>>> = Readonly<{
|
|
66
|
-
[K in keyof Ts]: GuardType<Ts[K]>;
|
|
67
|
-
}>;
|
|
68
|
-
/** Convert a union type to an intersection type. */
|
|
69
|
-
export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
70
|
-
/** Intersection of the types guarded by a tuple of guards — backs `intersectionOf`. */
|
|
71
|
-
export type IntersectionFromGuards<Gs extends ReadonlyArray<Guard<unknown>>> = UnionToIntersection<GuardType<Gs[number]>>;
|
|
72
|
-
/**
|
|
73
|
-
* A parser: coerces an unknown value to `T`, or returns `undefined`.
|
|
74
|
-
*
|
|
75
|
-
* @remarks
|
|
76
|
-
* The runtime parallel of {@link Guard}. A parser pairs soundly with the guard
|
|
77
|
-
* for its output type: a guard-valid input is returned unchanged, and every
|
|
78
|
-
* non-`undefined` output satisfies that guard.
|
|
79
|
-
*/
|
|
80
|
-
export type Parser<T> = (value: unknown) => T | undefined;
|
|
81
|
-
/**
|
|
82
|
-
* Any constructor signature that produces instances of `T`.
|
|
83
|
-
*
|
|
84
|
-
* @remarks
|
|
85
|
-
* Uses `unknown[]` parameters to stay maximally assignable from specific
|
|
86
|
-
* constructors without resorting to `any`.
|
|
87
|
-
*/
|
|
88
|
-
export type AnyConstructor<T = unknown> = new (...args: unknown[]) => T;
|
|
89
|
-
/** A function accepting any arguments and returning `unknown`. */
|
|
90
|
-
export type AnyFunction = (...args: unknown[]) => unknown;
|
|
91
|
-
/** An async function accepting any arguments and returning a `Promise`. */
|
|
92
|
-
export type AnyAsyncFunction = (...args: unknown[]) => Promise<unknown>;
|
|
93
|
-
/** A function accepting zero arguments and returning `unknown`. */
|
|
94
|
-
export type ZeroArgFunction = () => unknown;
|
|
95
|
-
/** An async function accepting zero arguments and returning a `Promise`. */
|
|
96
|
-
export type ZeroArgAsyncFunction = () => Promise<unknown>;
|
|
97
|
-
/**
|
|
98
|
-
* A primitive JSON value — the flat leaf of any JSON document.
|
|
99
|
-
*
|
|
100
|
-
* @remarks
|
|
101
|
-
* The recursive {@link JSONValue} tree type is shipped for consumers that need a
|
|
102
|
-
* reusable JSON metadata contract. Dedicated `JSONObject` / `JSONArray` aliases
|
|
103
|
-
* remain unshipped; compose those narrower shapes with the combinators, or keep
|
|
104
|
-
* an untrusted parse result as `unknown` and narrow it.
|
|
105
|
-
*/
|
|
106
|
-
export type JSONPrimitive = string | number | boolean | null;
|
|
107
|
-
/**
|
|
108
|
-
* A recursive JSON value — primitives, arrays, and object records.
|
|
109
|
-
*
|
|
110
|
-
* @remarks
|
|
111
|
-
* The static type admits any `number` because TypeScript cannot express
|
|
112
|
-
* finiteness. The {@link isJSONValue} guard rejects `NaN` and `±Infinity` since
|
|
113
|
-
* they have no JSON representation.
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```ts
|
|
117
|
-
* const value: JSONValue = { nested: [1, 'x', null] }
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
export type JSONValue = JSONPrimitive | readonly JSONValue[] | {
|
|
121
|
-
readonly [key: string]: JSONValue;
|
|
122
|
-
};
|
|
123
|
-
/** The seven standard JSON Schema `type` names. */
|
|
124
|
-
export type JSONSchemaType = 'null' | 'boolean' | 'object' | 'array' | 'number' | 'integer' | 'string';
|
|
125
|
-
/**
|
|
126
|
-
* A JSON Schema fragment — the subset of keywords the contract compiler emits
|
|
127
|
-
* and {@link RawShape} embeds.
|
|
128
|
-
*
|
|
129
|
-
* @remarks
|
|
130
|
-
* Intentionally lean (not the full ~50-keyword vocabulary): it carries only the
|
|
131
|
-
* keywords {@link Infer}-driven `compileSchema` produces. Recursive via `items` /
|
|
132
|
-
* `properties` / `additionalProperties` / `anyOf` / `oneOf`, but every walk is
|
|
133
|
-
* over a finite, developer-authored shape — there is no cycle/depth risk.
|
|
134
|
-
*/
|
|
135
|
-
export interface JSONSchema {
|
|
136
|
-
readonly type?: JSONSchemaType;
|
|
137
|
-
readonly description?: string;
|
|
138
|
-
readonly enum?: readonly (string | number | boolean)[];
|
|
139
|
-
readonly minLength?: number;
|
|
140
|
-
readonly maxLength?: number;
|
|
141
|
-
readonly pattern?: string;
|
|
142
|
-
readonly minimum?: number;
|
|
143
|
-
readonly maximum?: number;
|
|
144
|
-
readonly minItems?: number;
|
|
145
|
-
readonly maxItems?: number;
|
|
146
|
-
readonly items?: JSONSchema;
|
|
147
|
-
readonly properties?: Readonly<Record<string, JSONSchema>>;
|
|
148
|
-
readonly required?: readonly string[];
|
|
149
|
-
readonly additionalProperties?: boolean | JSONSchema;
|
|
150
|
-
readonly anyOf?: readonly JSONSchema[];
|
|
151
|
-
readonly oneOf?: readonly JSONSchema[];
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* A contract shape — a declarative description of a value, built with the shape
|
|
155
|
-
* builders and compiled into a guard, a parser, a JSON Schema, and a generator.
|
|
156
|
-
*
|
|
157
|
-
* @remarks
|
|
158
|
-
* A discriminated union keyed on `type`. Shapes nest (an `ArrayShape` holds an
|
|
159
|
-
* element shape, an `ObjectShape` a map of them), so a contract is a finite,
|
|
160
|
-
* developer-authored tree — never cyclic.
|
|
161
|
-
*/
|
|
162
|
-
export type ContractShape = StringShape | NumberShape | BooleanShape | NullShape | LiteralShape | ArrayShape | ObjectShape | UnionShape | OptionalShape | NullableShape | JSONShape | RawShape;
|
|
163
|
-
/** A string shape with optional length and pattern constraints. */
|
|
164
|
-
export interface StringShape {
|
|
165
|
-
readonly type: 'string';
|
|
166
|
-
readonly min?: number;
|
|
167
|
-
readonly max?: number;
|
|
168
|
-
readonly pattern?: RegExp;
|
|
169
|
-
readonly description?: string;
|
|
170
|
-
}
|
|
171
|
-
/** A numeric shape with optional bounds; `integer` restricts to whole numbers. */
|
|
172
|
-
export interface NumberShape {
|
|
173
|
-
readonly type: 'number';
|
|
174
|
-
readonly min?: number;
|
|
175
|
-
readonly max?: number;
|
|
176
|
-
readonly integer?: boolean;
|
|
177
|
-
readonly description?: string;
|
|
178
|
-
}
|
|
179
|
-
/** A boolean shape — accepts only `true` or `false`. */
|
|
180
|
-
export interface BooleanShape {
|
|
181
|
-
readonly type: 'boolean';
|
|
182
|
-
readonly description?: string;
|
|
183
|
-
}
|
|
184
|
-
/** A null shape — accepts only `null`. */
|
|
185
|
-
export interface NullShape {
|
|
186
|
-
readonly type: 'null';
|
|
187
|
-
readonly description?: string;
|
|
188
|
-
}
|
|
189
|
-
/** A literal shape — accepts exactly one of a fixed set of primitive values. */
|
|
190
|
-
export interface LiteralShape<T extends readonly (string | number | boolean)[] = readonly (string | number | boolean)[]> {
|
|
191
|
-
readonly type: 'literal';
|
|
192
|
-
readonly values: T;
|
|
193
|
-
readonly description?: string;
|
|
194
|
-
}
|
|
195
|
-
/** An array shape with an element shape and optional length bounds. */
|
|
196
|
-
export interface ArrayShape<S extends ContractShape = ContractShape> {
|
|
197
|
-
readonly type: 'array';
|
|
198
|
-
readonly items: S;
|
|
199
|
-
readonly min?: number;
|
|
200
|
-
readonly max?: number;
|
|
201
|
-
readonly description?: string;
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* An object shape — a map of property names to child shapes.
|
|
205
|
-
*
|
|
206
|
-
* @remarks
|
|
207
|
-
* A property whose shape is an {@link OptionalShape} may be absent; all others
|
|
208
|
-
* are required. `additionalProperties` controls unknown keys: `undefined` /
|
|
209
|
-
* `false` rejects them (closed), `true` accepts them as-is, a `ContractShape`
|
|
210
|
-
* validates them.
|
|
211
|
-
*/
|
|
212
|
-
export interface ObjectShape<P extends Readonly<Record<string, ContractShape>> = Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = boolean | ContractShape> {
|
|
213
|
-
readonly type: 'object';
|
|
214
|
-
readonly properties: P;
|
|
215
|
-
readonly additionalProperties?: A;
|
|
216
|
-
readonly description?: string;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* A union shape — accepts a value matching any one variant (first match wins).
|
|
220
|
-
*
|
|
221
|
-
* @remarks
|
|
222
|
-
* `mode` selects the emitted JSON Schema keyword: `'anyOf'` (default) or
|
|
223
|
-
* `'oneOf'`. Runtime behavior is identical.
|
|
224
|
-
*/
|
|
225
|
-
export interface UnionShape<V extends readonly ContractShape[] = readonly ContractShape[]> {
|
|
226
|
-
readonly type: 'union';
|
|
227
|
-
readonly variants: V;
|
|
228
|
-
readonly mode?: 'anyOf' | 'oneOf';
|
|
229
|
-
readonly description?: string;
|
|
230
|
-
}
|
|
231
|
-
/** An optional wrapper — the inner shape may be absent (`undefined`). */
|
|
232
|
-
export interface OptionalShape<S extends ContractShape = ContractShape> {
|
|
233
|
-
readonly type: 'optional';
|
|
234
|
-
readonly inner: S;
|
|
235
|
-
}
|
|
236
|
-
/** A nullable wrapper — the inner shape may be `null`. */
|
|
237
|
-
export interface NullableShape<S extends ContractShape = ContractShape> {
|
|
238
|
-
readonly type: 'nullable';
|
|
239
|
-
readonly inner: S;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* A JSON passthrough shape — accepts any JSON value.
|
|
243
|
-
*
|
|
244
|
-
* @remarks
|
|
245
|
-
* The compiled guard is a sound {@link isJSONValue} check (rejecting cycles,
|
|
246
|
-
* functions, `NaN`, and `±Infinity`); the parser gates through that guard; the
|
|
247
|
-
* schema is the empty schema `{}` (matches any JSON instance); the generator
|
|
248
|
-
* emits a small deterministic {@link JSONValue}. Unlike {@link RawShape}, whose
|
|
249
|
-
* guard accepts anything, this shape validates that a value is real JSON.
|
|
250
|
-
*/
|
|
251
|
-
export interface JSONShape {
|
|
252
|
-
readonly type: 'json';
|
|
253
|
-
readonly description?: string;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* A raw JSON Schema passthrough — embeds a schema fragment directly.
|
|
257
|
-
*
|
|
258
|
-
* @remarks
|
|
259
|
-
* For values the shape DSL can't express. The compiled guard accepts any value
|
|
260
|
-
* and the parser passes it through unchanged; the schema is emitted verbatim.
|
|
261
|
-
*/
|
|
262
|
-
export interface RawShape {
|
|
263
|
-
readonly type: 'raw';
|
|
264
|
-
readonly schema: JSONSchema;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Infer the static TypeScript type a {@link ContractShape} describes.
|
|
268
|
-
*
|
|
269
|
-
* @remarks
|
|
270
|
-
* Structural and recursive: optional object fields surface as optional
|
|
271
|
-
* properties, nullable wrappers add `| null`, and a literal tuple becomes a
|
|
272
|
-
* string/number/boolean-literal union.
|
|
273
|
-
*
|
|
274
|
-
* The first, non-distributive branch bails out to `unknown` when `S` is the
|
|
275
|
-
* full widened {@link ContractShape} union. Five members of that union recurse
|
|
276
|
-
* back into the whole union through their defaulted generics, so inferring the
|
|
277
|
-
* full union is a fixed point that can never shrink — the compiler would fan
|
|
278
|
-
* out until it aborts with TS2589. Bailing out lazily short-circuits that
|
|
279
|
-
* fixed point (the untaken branch is never instantiated) while every narrow
|
|
280
|
-
* shape and every partial union still flows through the exact chain below.
|
|
281
|
-
*
|
|
282
|
-
* The `ObjectShape` branch's `additionalProperties` guard (`[A] extends
|
|
283
|
-
* [boolean | ContractShape]`) is likewise wrapped in a tuple to stay
|
|
284
|
-
* non-distributive: a naked `A extends boolean | ContractShape` distributes
|
|
285
|
-
* over a union `A`, fanning a wide `additionalProperties` type into one
|
|
286
|
-
* {@link InferObject} instantiation per union member instead of one
|
|
287
|
-
* instantiation over the whole union — the same TS2589 risk under repeated
|
|
288
|
-
* nesting. {@link InferIndex} and {@link InferOpenIndex} apply the identical
|
|
289
|
-
* tuple guard to their own `A` parameter for the same reason.
|
|
290
|
-
*/
|
|
291
|
-
export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unknown : S extends StringShape ? string : S extends NumberShape ? number : S extends BooleanShape ? boolean : S extends NullShape ? null : S extends {
|
|
292
|
-
readonly type: 'literal';
|
|
293
|
-
readonly values: infer V;
|
|
294
|
-
} ? V extends readonly (infer L)[] ? L : never : S extends {
|
|
295
|
-
readonly type: 'array';
|
|
296
|
-
readonly items: infer I;
|
|
297
|
-
} ? I extends ContractShape ? readonly Infer<I>[] : never : S extends ObjectShape<infer P, infer A> ? P extends Readonly<Record<string, ContractShape>> ? [A] extends [boolean | ContractShape] ? InferObject<P, A> : never : never : S extends {
|
|
298
|
-
readonly type: 'union';
|
|
299
|
-
readonly variants: infer V;
|
|
300
|
-
} ? V extends readonly ContractShape[] ? InferUnion<V> : never : S extends {
|
|
301
|
-
readonly type: 'optional';
|
|
302
|
-
readonly inner: infer I;
|
|
303
|
-
} ? I extends ContractShape ? Infer<I> | undefined : never : S extends {
|
|
304
|
-
readonly type: 'nullable';
|
|
305
|
-
readonly inner: infer I;
|
|
306
|
-
} ? I extends ContractShape ? Infer<I> | null : never : S extends JSONShape ? JSONValue : unknown;
|
|
307
|
-
/**
|
|
308
|
-
* {@link Infer} of an object shape's `properties` — the required keys, plus the
|
|
309
|
-
* `optional`-wrapped keys as optional members, plus the index-signature
|
|
310
|
-
* contribution of `additionalProperties` (see {@link InferIndex}).
|
|
311
|
-
*
|
|
312
|
-
* @remarks
|
|
313
|
-
* The `[keyof P] extends [never]` split is hoisted to the front (rather than
|
|
314
|
-
* folded into the intersection's second operand) so a pure record shape
|
|
315
|
-
* (`P` empty) short-circuits straight to {@link InferIndex} without ever
|
|
316
|
-
* building the `Readonly<{} & {}>` intersection shell — the clean
|
|
317
|
-
* `Readonly<Record<string, V>>` {@link InferIndex} already returns. A closed
|
|
318
|
-
* empty object (`P` empty, `A` `false`/absent) still resolves through
|
|
319
|
-
* {@link InferIndex}'s own `[A] extends [false]` branch to
|
|
320
|
-
* `Readonly<Record<never, never>>`, preserving the empty-closed-object result.
|
|
321
|
-
* A shape with fixed properties always routes through {@link InferOpenIndex}.
|
|
322
|
-
*/
|
|
323
|
-
export type InferObject<P extends Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = false> = [keyof P] extends [never] ? [A] extends [false] ? Readonly<Record<never, never>> : InferIndex<A> : Readonly<{
|
|
324
|
-
[K in keyof P as P[K] extends {
|
|
325
|
-
readonly type: 'optional';
|
|
326
|
-
} ? never : K]: Infer<P[K]>;
|
|
327
|
-
} & {
|
|
328
|
-
[K in keyof P as P[K] extends {
|
|
329
|
-
readonly type: 'optional';
|
|
330
|
-
} ? K : never]?: P[K] extends {
|
|
331
|
-
readonly type: 'optional';
|
|
332
|
-
readonly inner: infer I;
|
|
333
|
-
} ? I extends ContractShape ? Infer<I> : never : never;
|
|
334
|
-
}> & InferOpenIndex<A>;
|
|
335
|
-
/**
|
|
336
|
-
* The index-signature contribution of a pure record shape's `additionalProperties`
|
|
337
|
-
* — the `recordShape` case, where `properties` is empty.
|
|
338
|
-
*
|
|
339
|
-
* @remarks
|
|
340
|
-
* `false` (closed) contributes `unknown`, which collapses away in an
|
|
341
|
-
* intersection — a closed object's {@link Infer} is unaffected. `true` (open,
|
|
342
|
-
* unconstrained) contributes an `unknown`-valued index signature. A
|
|
343
|
-
* {@link ContractShape} (open, constrained) contributes an index signature
|
|
344
|
-
* typed to that shape's own `Infer` — sound here because there are no fixed
|
|
345
|
-
* properties for the index to collide with.
|
|
346
|
-
*
|
|
347
|
-
* @remarks
|
|
348
|
-
* The final `[A] extends [ContractShape]` guard is tuple-wrapped to stay
|
|
349
|
-
* non-distributive, matching {@link Infer}'s own object-branch guard — see
|
|
350
|
-
* that type's remarks for why a naked `extends` here would fan a wide `A`
|
|
351
|
-
* into a union of `InferIndex` instantiations instead of one.
|
|
352
|
-
*/
|
|
353
|
-
export type InferIndex<A extends boolean | ContractShape> = [A] extends [false] ? unknown : [A] extends [true] ? {
|
|
354
|
-
readonly [k: string]: unknown;
|
|
355
|
-
} : [A] extends [ContractShape] ? {
|
|
356
|
-
readonly [k: string]: Infer<A>;
|
|
357
|
-
} : unknown;
|
|
358
|
-
/**
|
|
359
|
-
* The index-signature contribution of a MIXED object shape's
|
|
360
|
-
* `additionalProperties` — one with both fixed `properties` and an open tail.
|
|
361
|
-
*
|
|
362
|
-
* @remarks
|
|
363
|
-
* A typed index (`{ readonly [k: string]: Infer<A> }`) collapses any
|
|
364
|
-
* differently-typed fixed property to `never` on intersection and makes the
|
|
365
|
-
* object type unconstructable — TypeScript rejects assigning any property
|
|
366
|
-
* whose type differs from the index value type. So when `A` is a
|
|
367
|
-
* {@link ContractShape} here, the index is deliberately widened to
|
|
368
|
-
* `{ readonly [k: string]: unknown }`: the static type stops over-claiming the
|
|
369
|
-
* extra-key type while the runtime guard still validates extras against `A`.
|
|
370
|
-
* `false` / `true` behave exactly as {@link InferIndex}.
|
|
371
|
-
*
|
|
372
|
-
* @remarks
|
|
373
|
-
* The final `[A] extends [ContractShape]` guard is tuple-wrapped to stay
|
|
374
|
-
* non-distributive, matching {@link Infer}'s own object-branch guard and
|
|
375
|
-
* {@link InferIndex}'s tail — see {@link Infer}'s remarks for why a naked
|
|
376
|
-
* `extends` here would fan a wide `A` into a union of instantiations.
|
|
377
|
-
*/
|
|
378
|
-
export type InferOpenIndex<A extends boolean | ContractShape> = [A] extends [false] ? unknown : [A] extends [true] ? {
|
|
379
|
-
readonly [k: string]: unknown;
|
|
380
|
-
} : [A] extends [ContractShape] ? {
|
|
381
|
-
readonly [k: string]: unknown;
|
|
382
|
-
} : unknown;
|
|
383
|
-
/** {@link Infer} of a union shape's `variants` — the union of each variant's inferred type. */
|
|
384
|
-
export type InferUnion<V extends readonly ContractShape[]> = V extends readonly (infer U)[] ? U extends ContractShape ? Infer<U> : never : never;
|
|
385
|
-
/** {@link Infer} with its TOP-LEVEL `readonly` modifiers stripped (a shallow strip — nested object/array properties stay readonly) — for consumers writing the parsed value's own fields. */
|
|
386
|
-
export type InferMutable<S extends ContractShape> = {
|
|
387
|
-
-readonly [K in keyof Infer<S>]: Infer<S>[K];
|
|
388
|
-
};
|
|
389
|
-
/** Options for {@link StringShape} (via `stringShape`). */
|
|
390
|
-
export interface StringShapeOptions {
|
|
391
|
-
readonly min?: number;
|
|
392
|
-
readonly max?: number;
|
|
393
|
-
readonly pattern?: RegExp;
|
|
394
|
-
readonly description?: string;
|
|
395
|
-
}
|
|
396
|
-
/** Options for {@link NumberShape} (via `numberShape` / `integerShape`). */
|
|
397
|
-
export interface NumberShapeOptions {
|
|
398
|
-
readonly min?: number;
|
|
399
|
-
readonly max?: number;
|
|
400
|
-
readonly integer?: boolean;
|
|
401
|
-
readonly description?: string;
|
|
402
|
-
}
|
|
403
|
-
/** Options for {@link BooleanShape} (via `booleanShape`). */
|
|
404
|
-
export interface BooleanShapeOptions {
|
|
405
|
-
readonly description?: string;
|
|
406
|
-
}
|
|
407
|
-
/** Options for {@link NullShape} (via `nullShape`). */
|
|
408
|
-
export interface NullShapeOptions {
|
|
409
|
-
readonly description?: string;
|
|
410
|
-
}
|
|
411
|
-
/** Options for {@link JSONShape} (via `jsonShape`). */
|
|
412
|
-
export interface JSONShapeOptions {
|
|
413
|
-
readonly description?: string;
|
|
414
|
-
}
|
|
415
|
-
/** Options for {@link LiteralShape} (via `literalShape`). */
|
|
416
|
-
export interface LiteralShapeOptions {
|
|
417
|
-
readonly description?: string;
|
|
418
|
-
}
|
|
419
|
-
/** Options for {@link ArrayShape} (via `arrayShape`). */
|
|
420
|
-
export interface ArrayShapeOptions {
|
|
421
|
-
readonly min?: number;
|
|
422
|
-
readonly max?: number;
|
|
423
|
-
readonly description?: string;
|
|
424
|
-
}
|
|
425
|
-
/** Options for {@link ObjectShape} (via `objectShape`). */
|
|
426
|
-
export interface ObjectShapeOptions<A extends boolean | ContractShape = boolean | ContractShape> {
|
|
427
|
-
readonly additionalProperties?: A;
|
|
428
|
-
readonly description?: string;
|
|
429
|
-
}
|
|
430
|
-
/** Options for record shapes (via `recordShape`). */
|
|
431
|
-
export interface RecordShapeOptions {
|
|
432
|
-
readonly description?: string;
|
|
433
|
-
}
|
|
434
|
-
/** The kind of value a {@link Fault} expected — the shape-projected counterpart of a `ContractShape`'s `type`. */
|
|
435
|
-
export type FaultKind = 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'literal' | 'array' | 'object' | 'union' | 'json';
|
|
436
|
-
/** The refinement a {@link Fault} of reason `'constraint'` violates. */
|
|
437
|
-
export type FaultConstraint = 'min' | 'max' | 'pattern' | 'integer';
|
|
438
|
-
/**
|
|
439
|
-
* A single structured parse-failure diagnostic — one entry of an
|
|
440
|
-
* {@link ContractInterface.explain} report.
|
|
441
|
-
*
|
|
442
|
-
* @remarks
|
|
443
|
-
* A discriminated union on `reason`:
|
|
444
|
-
* - `'type'` — the value could not coerce to `expected` at all.
|
|
445
|
-
* - `'missing'` — a required object property was absent.
|
|
446
|
-
* - `'constraint'` — the value coerced to `expected` but violated one
|
|
447
|
-
* refinement (`min` / `max` / `pattern` / `integer`); `limit` carries the
|
|
448
|
-
* violated bound/pattern when applicable.
|
|
449
|
-
* - `'variant'` — an `anyOf`-mode union matched no variant; `variants` is the
|
|
450
|
-
* variant count, followed (in the report) by the closest variant's own faults.
|
|
451
|
-
* - `'oneOf'` — a `oneOf`-mode union matched zero or two-or-more variants;
|
|
452
|
-
* `matched` is the raw match count.
|
|
453
|
-
*/
|
|
454
|
-
export type Fault = {
|
|
455
|
-
readonly reason: 'type';
|
|
456
|
-
readonly path: FieldPath;
|
|
457
|
-
readonly expected: FaultKind;
|
|
458
|
-
readonly received: string;
|
|
459
|
-
} | {
|
|
460
|
-
readonly reason: 'missing';
|
|
461
|
-
readonly path: FieldPath;
|
|
462
|
-
readonly expected: FaultKind;
|
|
463
|
-
} | {
|
|
464
|
-
readonly reason: 'constraint';
|
|
465
|
-
readonly path: FieldPath;
|
|
466
|
-
readonly expected: FaultKind;
|
|
467
|
-
readonly constraint: FaultConstraint;
|
|
468
|
-
readonly limit?: number | string;
|
|
469
|
-
readonly received: string;
|
|
470
|
-
} | {
|
|
471
|
-
readonly reason: 'variant';
|
|
472
|
-
readonly path: FieldPath;
|
|
473
|
-
readonly variants: number;
|
|
474
|
-
} | {
|
|
475
|
-
readonly reason: 'oneOf';
|
|
476
|
-
readonly path: FieldPath;
|
|
477
|
-
readonly matched: number;
|
|
478
|
-
};
|
|
479
|
-
/** A deterministic random source returning a value in `[0, 1)`. */
|
|
480
|
-
export type RandomFunction = () => number;
|
|
481
|
-
/**
|
|
482
|
-
* A compiled contract — the four lockstep outputs derived from one shape.
|
|
483
|
-
*
|
|
484
|
-
* @remarks
|
|
485
|
-
* Built by `createContract`: `is` narrows, `parse` coerces (returning the typed
|
|
486
|
-
* value or `undefined`), `schema` is the emitted JSON Schema, and `generate`
|
|
487
|
-
* produces deterministic seed data from a {@link RandomFunction} (defaulting to
|
|
488
|
-
* a wall-clock-seeded source when none is supplied).
|
|
489
|
-
*/
|
|
490
|
-
export interface ContractInterface<T> {
|
|
491
|
-
readonly schema: JSONSchema;
|
|
492
|
-
readonly is: Guard<T>;
|
|
493
|
-
parse(value: unknown): T | undefined;
|
|
494
|
-
/**
|
|
495
|
-
* Report every structured parse fault a value has against this contract.
|
|
496
|
-
*
|
|
497
|
-
* @remarks
|
|
498
|
-
* An empty report means the value is valid. Soundness invariant:
|
|
499
|
-
* `explain(v).length === 0` if and only if `parse(v) !== undefined` — explain
|
|
500
|
-
* mirrors `parse`'s coercion, not the stricter `is`. Faults are listed in
|
|
501
|
-
* stable pre-order (declared key/index order).
|
|
502
|
-
*
|
|
503
|
-
* @param value - The value to check
|
|
504
|
-
* @returns The faults found, empty when the value parses successfully
|
|
505
|
-
*/
|
|
506
|
-
explain(value: unknown): readonly Fault[];
|
|
507
|
-
generate(random?: RandomFunction): T;
|
|
508
|
-
}
|