@orkestrel/contract 0.0.2 → 0.0.4
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/combinators.d.ts +2 -2
- package/dist/src/core/compilers.d.ts +3 -0
- package/dist/src/core/index.cjs +123 -103
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +123 -104
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/shapers.d.ts +22 -6
- package/dist/src/core/types.d.ts +81 -11
- package/dist/src/core/validators.d.ts +57 -2
- package/package.json +2 -2
|
@@ -108,7 +108,7 @@ export declare function arrayShape<S extends ContractShape>(items: S, options?:
|
|
|
108
108
|
* })
|
|
109
109
|
* ```
|
|
110
110
|
*/
|
|
111
|
-
export declare function objectShape<P extends Readonly<Record<string, ContractShape
|
|
111
|
+
export declare function objectShape<P extends Readonly<Record<string, ContractShape>>, const A extends boolean | ContractShape = false>(properties: P, options?: ObjectShapeOptions<A>): ObjectShape<P, A>;
|
|
112
112
|
/**
|
|
113
113
|
* Build an open {@link ObjectShape} with no fixed properties — a dictionary.
|
|
114
114
|
*
|
|
@@ -125,7 +125,7 @@ export declare function objectShape<P extends Readonly<Record<string, ContractSh
|
|
|
125
125
|
* const bindings = recordShape(numberShape()) // ~ Record<string, number>
|
|
126
126
|
* ```
|
|
127
127
|
*/
|
|
128
|
-
export declare function recordShape<S extends ContractShape>(values: S, options?: RecordShapeOptions): ObjectShape
|
|
128
|
+
export declare function recordShape<S extends ContractShape>(values: S, options?: RecordShapeOptions): ObjectShape<Record<never, never>, S>;
|
|
129
129
|
/**
|
|
130
130
|
* Build a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
|
|
131
131
|
*
|
|
@@ -143,16 +143,32 @@ export declare function unionShape<V extends readonly ContractShape[]>(...varian
|
|
|
143
143
|
* Build a {@link UnionShape} that emits `oneOf` (exactly one match) in JSON Schema.
|
|
144
144
|
*
|
|
145
145
|
* @remarks
|
|
146
|
-
*
|
|
147
|
-
*
|
|
146
|
+
* Unlike {@link unionShape} (`anyOf` — at least one variant matches),
|
|
147
|
+
* `oneOfShape`'s compiled guard and parser enforce EXACTLY one match:
|
|
148
|
+
*
|
|
149
|
+
* - **Guard**: accepts the value only when exactly one variant's guard
|
|
150
|
+
* accepts it. A value matching two-or-more variants — which would violate
|
|
151
|
+
* the emitted `oneOf` schema — is rejected, even though it would pass
|
|
152
|
+
* {@link unionShape}'s guard.
|
|
153
|
+
* - **Parser**: judged on the RAW input's guard matches only, with NO
|
|
154
|
+
* coercion fallback for an ambiguous input. When exactly one variant's
|
|
155
|
+
* guard accepts the raw value, that variant's parser runs. Zero matches or
|
|
156
|
+
* two-or-more matches both parse to `undefined` — a value ambiguous
|
|
157
|
+
* between variants has no well-defined coercion target.
|
|
158
|
+
*
|
|
159
|
+
* Prefer {@link unionShape} when a value may legitimately satisfy more than
|
|
160
|
+
* one variant (e.g. overlapping shapes) and any match is acceptable. Prefer
|
|
161
|
+
* `oneOfShape` when overlap between variants indicates malformed input that
|
|
162
|
+
* must be rejected.
|
|
148
163
|
*
|
|
149
164
|
* @param variants - The candidate shapes
|
|
150
165
|
* @returns A union shape with `mode: 'oneOf'`
|
|
151
166
|
*
|
|
152
167
|
* @example
|
|
153
168
|
* ```ts
|
|
154
|
-
* const id = oneOfShape(
|
|
155
|
-
* //
|
|
169
|
+
* const id = oneOfShape(numberShape(), integerShape())
|
|
170
|
+
* // 3 fails — matches both numberShape and integerShape
|
|
171
|
+
* // 3.5 passes — matches numberShape only
|
|
156
172
|
* ```
|
|
157
173
|
*/
|
|
158
174
|
export declare function oneOfShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<V>;
|
package/dist/src/core/types.d.ts
CHANGED
|
@@ -209,10 +209,10 @@ export interface ArrayShape<S extends ContractShape = ContractShape> {
|
|
|
209
209
|
* `false` rejects them (closed), `true` accepts them as-is, a `ContractShape`
|
|
210
210
|
* validates them.
|
|
211
211
|
*/
|
|
212
|
-
export interface ObjectShape<P extends Readonly<Record<string, ContractShape>> = Readonly<Record<string, ContractShape
|
|
212
|
+
export interface ObjectShape<P extends Readonly<Record<string, ContractShape>> = Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = boolean | ContractShape> {
|
|
213
213
|
readonly type: 'object';
|
|
214
214
|
readonly properties: P;
|
|
215
|
-
readonly additionalProperties?:
|
|
215
|
+
readonly additionalProperties?: A;
|
|
216
216
|
readonly description?: string;
|
|
217
217
|
}
|
|
218
218
|
/**
|
|
@@ -278,6 +278,15 @@ export interface RawShape {
|
|
|
278
278
|
* out until it aborts with TS2589. Bailing out lazily short-circuits that
|
|
279
279
|
* fixed point (the untaken branch is never instantiated) while every narrow
|
|
280
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.
|
|
281
290
|
*/
|
|
282
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 {
|
|
283
292
|
readonly type: 'literal';
|
|
@@ -285,10 +294,7 @@ export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unkno
|
|
|
285
294
|
} ? V extends readonly (infer L)[] ? L : never : S extends {
|
|
286
295
|
readonly type: 'array';
|
|
287
296
|
readonly items: infer I;
|
|
288
|
-
} ? I extends ContractShape ? readonly Infer<I>[] : never : S extends {
|
|
289
|
-
readonly type: 'object';
|
|
290
|
-
readonly properties: infer P;
|
|
291
|
-
} ? P extends Readonly<Record<string, ContractShape>> ? InferObject<P> : never : S extends {
|
|
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 {
|
|
292
298
|
readonly type: 'union';
|
|
293
299
|
readonly variants: infer V;
|
|
294
300
|
} ? V extends readonly ContractShape[] ? InferUnion<V> : never : S extends {
|
|
@@ -300,9 +306,21 @@ export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unkno
|
|
|
300
306
|
} ? I extends ContractShape ? Infer<I> | null : never : S extends JSONShape ? JSONValue : unknown;
|
|
301
307
|
/**
|
|
302
308
|
* {@link Infer} of an object shape's `properties` — the required keys, plus the
|
|
303
|
-
* `optional`-wrapped keys as optional members
|
|
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}.
|
|
304
322
|
*/
|
|
305
|
-
export type InferObject<P extends Readonly<Record<string, ContractShape
|
|
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<{
|
|
306
324
|
[K in keyof P as P[K] extends {
|
|
307
325
|
readonly type: 'optional';
|
|
308
326
|
} ? never : K]: Infer<P[K]>;
|
|
@@ -313,9 +331,61 @@ export type InferObject<P extends Readonly<Record<string, ContractShape>>> = Rea
|
|
|
313
331
|
readonly type: 'optional';
|
|
314
332
|
readonly inner: infer I;
|
|
315
333
|
} ? I extends ContractShape ? Infer<I> : never : never;
|
|
316
|
-
}>;
|
|
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;
|
|
317
383
|
/** {@link Infer} of a union shape's `variants` — the union of each variant's inferred type. */
|
|
318
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
|
+
};
|
|
319
389
|
/** Options for {@link StringShape} (via `stringShape`). */
|
|
320
390
|
export interface StringShapeOptions {
|
|
321
391
|
readonly min?: number;
|
|
@@ -353,8 +423,8 @@ export interface ArrayShapeOptions {
|
|
|
353
423
|
readonly description?: string;
|
|
354
424
|
}
|
|
355
425
|
/** Options for {@link ObjectShape} (via `objectShape`). */
|
|
356
|
-
export interface ObjectShapeOptions {
|
|
357
|
-
readonly additionalProperties?:
|
|
426
|
+
export interface ObjectShapeOptions<A extends boolean | ContractShape = boolean | ContractShape> {
|
|
427
|
+
readonly additionalProperties?: A;
|
|
358
428
|
readonly description?: string;
|
|
359
429
|
}
|
|
360
430
|
/** Options for record shapes (via `recordShape`). */
|
|
@@ -27,16 +27,38 @@ export declare function isUndefined(value: unknown): value is undefined;
|
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
29
|
export declare function isDefined<T>(value: T | null | undefined): value is T;
|
|
30
|
-
/** Determine whether a value is a string.
|
|
30
|
+
/** Determine whether a value is a string.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* isString('hi') // true
|
|
35
|
+
* isString(42) // false
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
31
38
|
export declare function isString(value: unknown): value is string;
|
|
32
39
|
/**
|
|
33
40
|
* Determine whether a value is a number.
|
|
34
41
|
*
|
|
35
42
|
* @remarks
|
|
36
43
|
* Includes `NaN` and `±Infinity` — use {@link isFiniteNumber} to exclude them.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* isNumber(42) // true
|
|
48
|
+
* isNumber(Number.NaN) // true — NaN is still a number
|
|
49
|
+
* isNumber('42') // false
|
|
50
|
+
* ```
|
|
37
51
|
*/
|
|
38
52
|
export declare function isNumber(value: unknown): value is number;
|
|
39
|
-
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
53
|
+
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* isFiniteNumber(42) // true
|
|
58
|
+
* isFiniteNumber(Number.NaN) // false
|
|
59
|
+
* isFiniteNumber(Infinity) // false
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
40
62
|
export declare function isFiniteNumber(value: unknown): value is number;
|
|
41
63
|
/** Determine whether a value is a finite integer (excludes `NaN`, `±Infinity`, and fractional numbers).
|
|
42
64
|
*
|
|
@@ -131,6 +153,31 @@ export declare function isNullableNumber(value: unknown): value is number | null
|
|
|
131
153
|
* ```
|
|
132
154
|
*/
|
|
133
155
|
export declare function isNullableBoolean(value: unknown): value is boolean | null;
|
|
156
|
+
/**
|
|
157
|
+
* Determine whether a value is an instance of a constructor, contained against
|
|
158
|
+
* a throwing `instanceof` check.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
* The low-level total helper every `instanceof`-based guard in this file (and
|
|
162
|
+
* the `instanceOf` combinator) routes through. A bare `value instanceof X` is
|
|
163
|
+
* NOT total (AGENTS §14): it invokes `getPrototypeOf` on `value` — which a
|
|
164
|
+
* revoked `Proxy` or a `getPrototypeOf`-trap `Proxy` throws from — and, when
|
|
165
|
+
* `X[Symbol.hasInstance]` is user-defined, can throw from arbitrary code. This
|
|
166
|
+
* wraps the check in {@link attempt} (see ./helpers.js) so any such throw
|
|
167
|
+
* yields `false` instead of escaping.
|
|
168
|
+
*
|
|
169
|
+
* @param value - The value to test
|
|
170
|
+
* @param ctor - The constructor to test against
|
|
171
|
+
* @returns `true` when `value instanceof ctor`, `false` on a non-match or a
|
|
172
|
+
* contained throw
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* isInstance(new Date(), Date) // true
|
|
177
|
+
* isInstance({}, Date) // false
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare function isInstance<C>(value: unknown, ctor: C): value is InstanceType<C & AnyConstructor<object>>;
|
|
134
181
|
/** Determine whether a value is a `Date`.
|
|
135
182
|
*
|
|
136
183
|
* @example
|
|
@@ -265,6 +312,14 @@ export declare function isObject(value: unknown): value is object;
|
|
|
265
312
|
* instance's runs through the class's own prototype. The whole body runs
|
|
266
313
|
* inside `attempt` (AGENTS §14) so a revoked `Proxy` or a hostile
|
|
267
314
|
* `getPrototypeOf` trap cannot escape as a thrown error.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```ts
|
|
318
|
+
* isRecord({ a: 1 }) // true
|
|
319
|
+
* isRecord(Object.create(null)) // true
|
|
320
|
+
* isRecord([]) // false
|
|
321
|
+
* isRecord(new Date()) // false
|
|
322
|
+
* ```
|
|
268
323
|
*/
|
|
269
324
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
270
325
|
/** Determine whether a value is a `Map`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orkestrel/contract",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Zero-dependency contract toolkit — type guards, combinators, parsers, and a shape DSL that compiles one contract shape into a guard, parser, JSON Schema, and generator. The foundation package of the @orkestrel line.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contract",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"prepublishOnly": "npm run format:check && npm run lint:check && npm run check && npm run build && npm test"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@orkestrel/guide": "^0.0.
|
|
67
|
+
"@orkestrel/guide": "^0.0.3",
|
|
68
68
|
"@types/node": "^26.1.1",
|
|
69
69
|
"oxfmt": "^0.59.0",
|
|
70
70
|
"oxlint": "^1.74.0",
|