@orkestrel/contract 0.0.1 → 0.0.2

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
  /**
@@ -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<{