@orkestrel/contract 0.0.1 → 0.0.3

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.
@@ -1,5 +1,4 @@
1
- import type { Guard, JSONValue } from './types.js';
2
- import type { FieldPath } from './types.js';
1
+ import { Guard, JSONValue, FieldPath } from './types.js';
3
2
  /**
4
3
  * Parse an unknown value to a string.
5
4
  *
@@ -9,6 +8,13 @@ import type { FieldPath } from './types.js';
9
8
  *
10
9
  * @param value - The value to parse
11
10
  * @returns A string, or `undefined`
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * parseString('hi') // 'hi'
15
+ * parseString(42) // '42'
16
+ * parseString(true) // undefined
17
+ * ```
12
18
  */
13
19
  export declare function parseString(value: unknown): string | undefined;
14
20
  /**
@@ -21,6 +27,13 @@ export declare function parseString(value: unknown): string | undefined;
21
27
  *
22
28
  * @param value - The value to parse
23
29
  * @returns A finite number, or `undefined`
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * parseNumber(42) // 42
34
+ * parseNumber('42') // 42
35
+ * parseNumber('abc') // undefined
36
+ * ```
24
37
  */
25
38
  export declare function parseNumber(value: unknown): number | undefined;
26
39
  /**
@@ -32,6 +45,12 @@ export declare function parseNumber(value: unknown): number | undefined;
32
45
  *
33
46
  * @param value - The value to parse
34
47
  * @returns A finite integer, or `undefined`
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * parseInteger(42) // 42
52
+ * parseInteger(3.14) // undefined
53
+ * ```
35
54
  */
36
55
  export declare function parseInteger(value: unknown): number | undefined;
37
56
  /**
@@ -44,6 +63,13 @@ export declare function parseInteger(value: unknown): number | undefined;
44
63
  *
45
64
  * @param value - The value to parse
46
65
  * @returns A boolean, or `undefined`
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * parseBoolean(true) // true
70
+ * parseBoolean('1') // true
71
+ * parseBoolean('nope') // undefined
72
+ * ```
47
73
  */
48
74
  export declare function parseBoolean(value: unknown): boolean | undefined;
49
75
  /**
@@ -56,6 +82,12 @@ export declare function parseBoolean(value: unknown): boolean | undefined;
56
82
  *
57
83
  * @param value - The value to parse
58
84
  * @returns `null` on a successful parse, or `undefined`
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * parseNull(null) // null
89
+ * parseNull(undefined) // undefined
90
+ * ```
59
91
  */
60
92
  export declare function parseNull(value: unknown): null | undefined;
61
93
  /**
@@ -76,6 +108,12 @@ export declare function parseRecord(value: unknown): Record<string, unknown> | u
76
108
  * @param value - The value to parse
77
109
  * @param guard - Optional element guard
78
110
  * @returns The array, or `undefined`
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * parseArray([1, 2]) // [1, 2]
115
+ * parseArray([1, 'x'], isNumber) // undefined
116
+ * ```
79
117
  */
80
118
  export declare function parseArray<T = unknown>(value: unknown, guard?: Guard<T>): readonly T[] | undefined;
81
119
  /**
@@ -92,6 +130,12 @@ export declare function parseArray<T = unknown>(value: unknown, guard?: Guard<T>
92
130
  *
93
131
  * @param value - The value to parse
94
132
  * @returns The value, or `undefined` when it is not a valid JSON value
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * parseJSONValue({ a: 1 }) // { a: 1 }
137
+ * parseJSONValue(Number.NaN) // undefined
138
+ * ```
95
139
  */
96
140
  export declare function parseJSONValue(value: unknown): JSONValue | undefined;
97
141
  /**
@@ -106,6 +150,12 @@ export declare function parseJSONValue(value: unknown): JSONValue | undefined;
106
150
  * @param value - The value to parse
107
151
  * @param allowed - The permitted literal values
108
152
  * @returns The matched literal (by identity), or `undefined`
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * parseEnum('b', ['a', 'b', 'c']) // 'b'
157
+ * parseEnum('z', ['a', 'b', 'c']) // undefined
158
+ * ```
109
159
  */
110
160
  export declare function parseEnum<const T extends string | number | boolean>(value: unknown, allowed: readonly T[]): T | undefined;
111
161
  /**
@@ -122,6 +172,12 @@ export declare function parseStringField(record: Record<string, unknown>, path:
122
172
  * @param record - The source record
123
173
  * @param path - A property key, or a key path descending into nested objects
124
174
  * @returns A finite number, or `undefined`
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * parseNumberField({ age: '42' }, 'age') // 42
179
+ * parseNumberField({}, 'age') // undefined
180
+ * ```
125
181
  */
126
182
  export declare function parseNumberField(record: Record<string, unknown>, path: FieldPath): number | undefined;
127
183
  /**
@@ -138,6 +194,12 @@ export declare function parseIntegerField(record: Record<string, unknown>, path:
138
194
  * @param record - The source record
139
195
  * @param path - A property key, or a key path descending into nested objects
140
196
  * @returns A boolean, or `undefined`
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * parseBooleanField({ on: 'true' }, 'on') // true
201
+ * parseBooleanField({}, 'on') // undefined
202
+ * ```
141
203
  */
142
204
  export declare function parseBooleanField(record: Record<string, unknown>, path: FieldPath): boolean | undefined;
143
205
  /**
@@ -150,6 +212,12 @@ export declare function parseBooleanField(record: Record<string, unknown>, path:
150
212
  * @param record - The source record
151
213
  * @param path - A property key, or a key path descending into nested objects
152
214
  * @returns `null` on a successful parse, or `undefined`
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * parseNullField({ value: null }, 'value') // null
219
+ * parseNullField({}, 'value') // undefined
220
+ * ```
153
221
  */
154
222
  export declare function parseNullField(record: Record<string, unknown>, path: FieldPath): null | undefined;
155
223
  /**
@@ -168,6 +236,12 @@ export declare function parseRecordField(record: Record<string, unknown>, path:
168
236
  * @param path - A property key, or a key path descending into nested objects
169
237
  * @param guard - Optional element guard
170
238
  * @returns An array, or `undefined`
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * parseArrayField({ tags: [1, 2] }, 'tags') // [1, 2]
243
+ * parseArrayField({}, 'tags') // undefined
244
+ * ```
171
245
  */
172
246
  export declare function parseArrayField<T = unknown>(record: Record<string, unknown>, path: FieldPath, guard?: Guard<T>): readonly T[] | undefined;
173
247
  /**
@@ -190,6 +264,12 @@ export declare function parseEnumField<const T extends string | number | boolean
190
264
  * @param record - The source record
191
265
  * @param path - A property key, or a key path descending into nested objects
192
266
  * @returns The value, or `undefined`
267
+ *
268
+ * @example
269
+ * ```ts
270
+ * parseJSONValueField({ data: { a: 1 } }, 'data') // { a: 1 }
271
+ * parseJSONValueField({}, 'data') // undefined
272
+ * ```
193
273
  */
194
274
  export declare function parseJSONValueField(record: Record<string, unknown>, path: FieldPath): JSONValue | undefined;
195
275
  /**
@@ -1,4 +1,4 @@
1
- import type { ArrayShape, ArrayShapeOptions, BooleanShape, BooleanShapeOptions, ContractShape, JSONSchema, JSONShape, JSONShapeOptions, LiteralShape, LiteralShapeOptions, NullableShape, NullShape, NullShapeOptions, NumberShape, NumberShapeOptions, ObjectShape, ObjectShapeOptions, OptionalShape, RawShape, RecordShapeOptions, StringShape, StringShapeOptions, UnionShape } from './types.js';
1
+ import { ArrayShape, ArrayShapeOptions, BooleanShape, BooleanShapeOptions, ContractShape, JSONSchema, JSONShape, JSONShapeOptions, LiteralShape, LiteralShapeOptions, NullableShape, NullShape, NullShapeOptions, NumberShape, NumberShapeOptions, ObjectShape, ObjectShapeOptions, OptionalShape, RawShape, RecordShapeOptions, StringShape, StringShapeOptions, UnionShape } from './types.js';
2
2
  /**
3
3
  * Build a string {@link StringShape}.
4
4
  *
@@ -16,6 +16,11 @@ export declare function stringShape(options?: StringShapeOptions): StringShape;
16
16
  *
17
17
  * @param options - Optional bounds (`min` / `max`), `integer`, and `description`
18
18
  * @returns A number shape
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const age = numberShape({ min: 0, max: 120 })
23
+ * ```
19
24
  */
20
25
  export declare function numberShape(options?: NumberShapeOptions): NumberShape;
21
26
  /**
@@ -34,6 +39,11 @@ export declare function integerShape(options?: Omit<NumberShapeOptions, 'integer
34
39
  *
35
40
  * @param options - Optional `description`
36
41
  * @returns A boolean shape
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const active = booleanShape({ description: 'Whether the record is active' })
46
+ * ```
37
47
  */
38
48
  export declare function booleanShape(options?: BooleanShapeOptions): BooleanShape;
39
49
  /**
@@ -41,6 +51,11 @@ export declare function booleanShape(options?: BooleanShapeOptions): BooleanShap
41
51
  *
42
52
  * @param options - Optional `description`
43
53
  * @returns A null shape
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const empty = nullShape()
58
+ * ```
44
59
  */
45
60
  export declare function nullShape(options?: NullShapeOptions): NullShape;
46
61
  /**
@@ -93,7 +108,7 @@ export declare function arrayShape<S extends ContractShape>(items: S, options?:
93
108
  * })
94
109
  * ```
95
110
  */
96
- export declare function objectShape<P extends Readonly<Record<string, ContractShape>>>(properties: P, options?: ObjectShapeOptions): ObjectShape<P>;
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>;
97
112
  /**
98
113
  * Build an open {@link ObjectShape} with no fixed properties — a dictionary.
99
114
  *
@@ -110,7 +125,7 @@ export declare function objectShape<P extends Readonly<Record<string, ContractSh
110
125
  * const bindings = recordShape(numberShape()) // ~ Record<string, number>
111
126
  * ```
112
127
  */
113
- 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>;
114
129
  /**
115
130
  * Build a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
116
131
  *
@@ -133,6 +148,12 @@ export declare function unionShape<V extends readonly ContractShape[]>(...varian
133
148
  *
134
149
  * @param variants - The candidate shapes
135
150
  * @returns A union shape with `mode: 'oneOf'`
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * const id = oneOfShape(stringShape(), integerShape())
155
+ * // Infer<typeof id> = string | number
156
+ * ```
136
157
  */
137
158
  export declare function oneOfShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<V>;
138
159
  /**
@@ -151,6 +172,12 @@ export declare function optionalShape<S extends ContractShape>(inner: S): Option
151
172
  *
152
173
  * @param inner - The wrapped shape
153
174
  * @returns A nullable shape
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * const bio = nullableShape(stringShape())
179
+ * // Infer<typeof bio> = string | null
180
+ * ```
154
181
  */
155
182
  export declare function nullableShape<S extends ContractShape>(inner: S): NullableShape<S>;
156
183
  /**
@@ -163,6 +190,11 @@ export declare function nullableShape<S extends ContractShape>(inner: S): Nullab
163
190
  *
164
191
  * @param options - Optional `description`
165
192
  * @returns A JSON passthrough shape
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * const payload = jsonShape({ description: 'Arbitrary JSON payload' })
197
+ * ```
166
198
  */
167
199
  export declare function jsonShape(options?: JSONShapeOptions): JSONShape;
168
200
  /**
@@ -174,5 +206,10 @@ export declare function jsonShape(options?: JSONShapeOptions): JSONShape;
174
206
  *
175
207
  * @param schema - The JSON Schema fragment to embed
176
208
  * @returns A raw shape
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * const custom = rawShape({ type: 'string', format: 'uuid' })
213
+ * ```
177
214
  */
178
215
  export declare function rawShape(schema: JSONSchema): RawShape;
@@ -45,13 +45,21 @@ export type FromGuards<G extends GuardsShape> = Readonly<{
45
45
  [K in keyof G]: GuardType<G[K]>;
46
46
  }>;
47
47
  /**
48
- * Like {@link FromGuards}, but every key listed in `K` is made optional.
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.
49
55
  *
50
56
  * @typeParam S - The full guard shape
51
- * @typeParam K - Tuple of keys to widen with `| undefined`
57
+ * @typeParam K - Tuple of keys to make optional
52
58
  */
53
59
  export type OptionalFromGuards<S extends GuardsShape, K extends ReadonlyArray<keyof S>> = Readonly<{
54
- [P in keyof S]: P extends K[number] ? FromGuards<S>[P] | undefined : FromGuards<S>[P];
60
+ [P in Exclude<keyof S, K[number]>]: FromGuards<S>[P];
61
+ } & {
62
+ [P in Extract<keyof S, K[number]>]?: FromGuards<S>[P];
55
63
  }>;
56
64
  /** Map a tuple of element guards to a readonly tuple of their guarded types. */
57
65
  export type TupleFromGuards<Ts extends ReadonlyArray<Guard<unknown>>> = Readonly<{
@@ -201,10 +209,10 @@ export interface ArrayShape<S extends ContractShape = ContractShape> {
201
209
  * `false` rejects them (closed), `true` accepts them as-is, a `ContractShape`
202
210
  * validates them.
203
211
  */
204
- 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> {
205
213
  readonly type: 'object';
206
214
  readonly properties: P;
207
- readonly additionalProperties?: boolean | ContractShape;
215
+ readonly additionalProperties?: A;
208
216
  readonly description?: string;
209
217
  }
210
218
  /**
@@ -277,10 +285,7 @@ export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unkno
277
285
  } ? V extends readonly (infer L)[] ? L : never : S extends {
278
286
  readonly type: 'array';
279
287
  readonly items: infer I;
280
- } ? I extends ContractShape ? readonly Infer<I>[] : never : S extends {
281
- readonly type: 'object';
282
- readonly properties: infer P;
283
- } ? P extends Readonly<Record<string, ContractShape>> ? InferObject<P> : never : S extends {
288
+ } ? 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 {
284
289
  readonly type: 'union';
285
290
  readonly variants: infer V;
286
291
  } ? V extends readonly ContractShape[] ? InferUnion<V> : never : S extends {
@@ -292,9 +297,10 @@ export type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unkno
292
297
  } ? I extends ContractShape ? Infer<I> | null : never : S extends JSONShape ? JSONValue : unknown;
293
298
  /**
294
299
  * {@link Infer} of an object shape's `properties` — the required keys, plus the
295
- * `optional`-wrapped keys as optional members.
300
+ * `optional`-wrapped keys as optional members, plus the index-signature
301
+ * contribution of `additionalProperties` (see {@link InferIndex}).
296
302
  */
297
- export type InferObject<P extends Readonly<Record<string, ContractShape>>> = Readonly<{
303
+ export type InferObject<P extends Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = false> = Readonly<{
298
304
  [K in keyof P as P[K] extends {
299
305
  readonly type: 'optional';
300
306
  } ? never : K]: Infer<P[K]>;
@@ -305,9 +311,49 @@ export type InferObject<P extends Readonly<Record<string, ContractShape>>> = Rea
305
311
  readonly type: 'optional';
306
312
  readonly inner: infer I;
307
313
  } ? I extends ContractShape ? Infer<I> : never : never;
308
- }>;
314
+ }> & ([keyof P] extends [never] ? InferIndex<A> : InferOpenIndex<A>);
315
+ /**
316
+ * The index-signature contribution of a pure record shape's `additionalProperties`
317
+ * — the `recordShape` case, where `properties` is empty.
318
+ *
319
+ * @remarks
320
+ * `false` (closed) contributes `unknown`, which collapses away in an
321
+ * intersection — a closed object's {@link Infer} is unaffected. `true` (open,
322
+ * unconstrained) contributes an `unknown`-valued index signature. A
323
+ * {@link ContractShape} (open, constrained) contributes an index signature
324
+ * typed to that shape's own `Infer` — sound here because there are no fixed
325
+ * properties for the index to collide with.
326
+ */
327
+ export type InferIndex<A extends boolean | ContractShape> = [A] extends [false] ? unknown : [A] extends [true] ? {
328
+ readonly [k: string]: unknown;
329
+ } : A extends ContractShape ? {
330
+ readonly [k: string]: Infer<A>;
331
+ } : unknown;
332
+ /**
333
+ * The index-signature contribution of a MIXED object shape's
334
+ * `additionalProperties` — one with both fixed `properties` and an open tail.
335
+ *
336
+ * @remarks
337
+ * A typed index (`{ readonly [k: string]: Infer<A> }`) collapses any
338
+ * differently-typed fixed property to `never` on intersection and makes the
339
+ * object type unconstructable — TypeScript rejects assigning any property
340
+ * whose type differs from the index value type. So when `A` is a
341
+ * {@link ContractShape} here, the index is deliberately widened to
342
+ * `{ readonly [k: string]: unknown }`: the static type stops over-claiming the
343
+ * extra-key type while the runtime guard still validates extras against `A`.
344
+ * `false` / `true` behave exactly as {@link InferIndex}.
345
+ */
346
+ export type InferOpenIndex<A extends boolean | ContractShape> = [A] extends [false] ? unknown : [A] extends [true] ? {
347
+ readonly [k: string]: unknown;
348
+ } : A extends ContractShape ? {
349
+ readonly [k: string]: unknown;
350
+ } : unknown;
309
351
  /** {@link Infer} of a union shape's `variants` — the union of each variant's inferred type. */
310
352
  export type InferUnion<V extends readonly ContractShape[]> = V extends readonly (infer U)[] ? U extends ContractShape ? Infer<U> : never : never;
353
+ /** {@link Infer} with all `readonly` modifiers stripped — for consumers writing the parsed value. */
354
+ export type InferMutable<S extends ContractShape> = {
355
+ -readonly [K in keyof Infer<S>]: Infer<S>[K];
356
+ };
311
357
  /** Options for {@link StringShape} (via `stringShape`). */
312
358
  export interface StringShapeOptions {
313
359
  readonly min?: number;
@@ -345,8 +391,8 @@ export interface ArrayShapeOptions {
345
391
  readonly description?: string;
346
392
  }
347
393
  /** Options for {@link ObjectShape} (via `objectShape`). */
348
- export interface ObjectShapeOptions {
349
- readonly additionalProperties?: boolean | ContractShape;
394
+ export interface ObjectShapeOptions<A extends boolean | ContractShape = boolean | ContractShape> {
395
+ readonly additionalProperties?: A;
350
396
  readonly description?: string;
351
397
  }
352
398
  /** Options for record shapes (via `recordShape`). */