@orkestrel/contract 0.0.5 → 0.0.6
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 +727 -0
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +3320 -8
- package/dist/src/core/index.d.ts +3320 -8
- package/dist/src/core/index.js +708 -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
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { ContractShape, FaultKind, FieldPath, JSONSchema, RandomFunction, Result } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Invoke a callback and capture its outcome as a {@link Result}, never letting
|
|
4
|
-
* a throw escape.
|
|
5
|
-
*
|
|
6
|
-
* @remarks
|
|
7
|
-
* The single sanctioned never-throw boundary for the guards (AGENTS §14). The
|
|
8
|
-
* `whereOf`, `lazyOf`, and `transformOf` combinators invoke caller-supplied
|
|
9
|
-
* callbacks *inside* a guard body, yet a guard must NEVER throw — it returns a
|
|
10
|
-
* `boolean`. This converts a throwing callback into a `Failure` so the
|
|
11
|
-
* surrounding guard can treat it as a non-match instead of propagating the
|
|
12
|
-
* exception, written once and shared rather than copy-pasted as ad-hoc
|
|
13
|
-
* `try`/`catch`.
|
|
14
|
-
*
|
|
15
|
-
* @param callback - The callback to invoke with no arguments
|
|
16
|
-
* @returns A `Success` carrying the return value, or a `Failure` carrying the
|
|
17
|
-
* thrown reason normalised to an `Error`
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* const outcome = attempt(() => predicate(value))
|
|
22
|
-
* return outcome.success && outcome.value
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export declare function attempt<T>(callback: () => T): Result<T>;
|
|
26
|
-
/**
|
|
27
|
-
* Resolve a (possibly nested) field value from a record by a key or key path.
|
|
28
|
-
*
|
|
29
|
-
* @remarks
|
|
30
|
-
* A single `string` is ONE key (never split on `.`, so dotted keys are safe); a
|
|
31
|
-
* string array descends left-to-right through nested objects. Intermediates may
|
|
32
|
-
* be any object — records, class instances, or arrays indexed by string. Returns
|
|
33
|
-
* `undefined` the moment a segment is missing or lands on a non-object, so the
|
|
34
|
-
* lookup is total — even against a hostile getter or Proxy trap that throws on
|
|
35
|
-
* read, contained via {@link attempt} so the throw never escapes.
|
|
36
|
-
*
|
|
37
|
-
* @param record - The source record
|
|
38
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
39
|
-
* @returns The resolved value, or `undefined`
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```ts
|
|
43
|
-
* resolveField({ user: { name: 'Ada' } }, ['user', 'name']) // 'Ada'
|
|
44
|
-
* resolveField({ 'a.b': 1 }, 'a.b') // 1 (one key)
|
|
45
|
-
* resolveField({ a: 1 }, ['a', 'b']) // undefined
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export declare function resolveField(record: Readonly<Record<string, unknown>>, path: FieldPath): unknown;
|
|
49
|
-
/**
|
|
50
|
-
* Build a deterministic pseudo-random source seeded from a single number.
|
|
51
|
-
*
|
|
52
|
-
* @remarks
|
|
53
|
-
* A mulberry32 generator — the same seed always yields the same sequence, so
|
|
54
|
-
* generated seed data is reproducible across runs. Used as the default random
|
|
55
|
-
* source for {@link compileGenerator}, seeded from the wall clock so casual
|
|
56
|
-
* callers still get varied output without passing a source themselves.
|
|
57
|
-
*
|
|
58
|
-
* @param seed - The seed for the sequence
|
|
59
|
-
* @returns A {@link RandomFunction} returning values in `[0, 1)`
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```ts
|
|
63
|
-
* const random = seededRandom(42)
|
|
64
|
-
* random() // always the same first value for seed 42
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export declare function seededRandom(seed: number): RandomFunction;
|
|
68
|
-
/**
|
|
69
|
-
* Count the enumerable own-symbol keys on a value.
|
|
70
|
-
*
|
|
71
|
-
* @remarks
|
|
72
|
-
* String keys are ignored — only `Object.getOwnPropertySymbols` entries whose
|
|
73
|
-
* descriptor is `enumerable` are counted. Backs the object-emptiness guards
|
|
74
|
-
* (`isEmptyObject` / `isNonEmptyObject`) so a record keyed only by an
|
|
75
|
-
* enumerable symbol is not mistaken for empty.
|
|
76
|
-
*
|
|
77
|
-
* @param value - The object to inspect
|
|
78
|
-
* @returns The number of enumerable own-symbol keys
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```ts
|
|
82
|
-
* const flag = Symbol('flag')
|
|
83
|
-
* enumerableSymbolCount(Object.defineProperty({}, flag, { value: 1, enumerable: true })) // 1
|
|
84
|
-
* enumerableSymbolCount({}) // 0
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
export declare function enumerableSymbolCount(value: object): number;
|
|
88
|
-
/**
|
|
89
|
-
* Narrow a compiled {@link JSONSchema} down to the open `Readonly<Record<string, unknown>>` shape
|
|
90
|
-
* tool definitions advertise as `parameters` — through the {@link isRecord} boundary guard, never
|
|
91
|
-
* an assertion (AGENTS §14).
|
|
92
|
-
*
|
|
93
|
-
* @remarks
|
|
94
|
-
* A `JSONSchema` is the closed contract-compiler fragment (it has no index signature), whereas a
|
|
95
|
-
* tool advertises its `parameters` as an open record. The two are structurally compatible but not
|
|
96
|
-
* assignable, so the schema crosses that boundary through `isRecord` — a compiled contract schema
|
|
97
|
-
* is always a record, so the guard passes; the `undefined` fallback only satisfies the type's
|
|
98
|
-
* optionality. This is the single sanctioned narrowing from a compiled contract schema to the open
|
|
99
|
-
* tool-parameters record, so the crossing lives once rather than being copy-pasted per call site.
|
|
100
|
-
*
|
|
101
|
-
* @param schema - The compiled JSON Schema (a contract's `schema`)
|
|
102
|
-
* @returns The schema as the open tool-parameters record, or `undefined` when it is not a record
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* import { createContract, schemaToParameters } from '@src/core'
|
|
107
|
-
*
|
|
108
|
-
* const contract = createContract(shape)
|
|
109
|
-
* const parameters = schemaToParameters(contract.schema) // the open record a tool advertises
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<string, unknown>> | undefined;
|
|
113
|
-
/**
|
|
114
|
-
* Render a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
|
|
115
|
-
* `received` field.
|
|
116
|
-
*
|
|
117
|
-
* @remarks
|
|
118
|
-
* A primitive renders as its literal: a string is `JSON.stringify`-escaped and
|
|
119
|
-
* clipped to {@link PREVIEW_LIMIT} characters (with a trailing `…` when
|
|
120
|
-
* clipped); a number / boolean / bigint / symbol renders via `String`; `null`
|
|
121
|
-
* and `undefined` render as their own name. Everything else — a plain object,
|
|
122
|
-
* an array, a function, a class instance, a `Map` — is NEVER traversed or
|
|
123
|
-
* stringified; it renders as its bare `typeof` tag (`'object'` / `'function'`),
|
|
124
|
-
* so a hostile or enormous structure can never blow up the preview.
|
|
125
|
-
*
|
|
126
|
-
* @param value - The value to preview
|
|
127
|
-
* @returns A short descriptive string, always safe to embed in a diagnostic
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* preview('hi') // '"hi"'
|
|
132
|
-
* preview(42) // '42'
|
|
133
|
-
* preview(null) // 'null'
|
|
134
|
-
* preview({ a: 1 }) // 'object'
|
|
135
|
-
* preview([1, 2, 3]) // 'object'
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
export declare function preview(value: unknown): string;
|
|
139
|
-
/**
|
|
140
|
-
* Project a {@link ContractShape} to the {@link FaultKind} it describes.
|
|
141
|
-
*
|
|
142
|
-
* @remarks
|
|
143
|
-
* A structural mapping used by {@link compileReporter} to fill a `Fault`'s
|
|
144
|
-
* `expected` field: most shapes map to their own `type` (`numberShape` maps to
|
|
145
|
-
* `'integer'` when `integer: true`, else `'number'`); `optionalShape` /
|
|
146
|
-
* `nullableShape` project through to their inner shape's kind, and `rawShape`
|
|
147
|
-
* (an arbitrary embedded schema with no fixed kind) projects to `'json'`.
|
|
148
|
-
*
|
|
149
|
-
* @param shape - The shape to project
|
|
150
|
-
* @returns The shape's {@link FaultKind}
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```ts
|
|
154
|
-
* shapeToKind(stringShape()) // 'string'
|
|
155
|
-
* shapeToKind(integerShape()) // 'integer'
|
|
156
|
-
* shapeToKind(optionalShape(nullShape())) // 'null'
|
|
157
|
-
* ```
|
|
158
|
-
*/
|
|
159
|
-
export declare function shapeToKind(shape: ContractShape): FaultKind;
|
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
import { Guard, JSONValue, FieldPath } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Parse an unknown value to a string.
|
|
4
|
-
*
|
|
5
|
-
* @remarks
|
|
6
|
-
* A string is returned unchanged; a finite number is coerced to its decimal
|
|
7
|
-
* string (`42` → `'42'`). `NaN`, `±Infinity`, and every other type → `undefined`.
|
|
8
|
-
*
|
|
9
|
-
* @param value - The value to parse
|
|
10
|
-
* @returns A string, or `undefined`
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* parseString('hi') // 'hi'
|
|
15
|
-
* parseString(42) // '42'
|
|
16
|
-
* parseString(true) // undefined
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export declare function parseString(value: unknown): string | undefined;
|
|
20
|
-
/**
|
|
21
|
-
* Parse an unknown value to a finite number.
|
|
22
|
-
*
|
|
23
|
-
* @remarks
|
|
24
|
-
* A finite number is returned unchanged; a non-blank numeric string is parsed
|
|
25
|
-
* via `Number(...)`. `NaN`, `±Infinity`, blank/non-numeric strings, and every
|
|
26
|
-
* other type → `undefined`.
|
|
27
|
-
*
|
|
28
|
-
* @param value - The value to parse
|
|
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
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export declare function parseNumber(value: unknown): number | undefined;
|
|
39
|
-
/**
|
|
40
|
-
* Parse an unknown value to a finite integer.
|
|
41
|
-
*
|
|
42
|
-
* @remarks
|
|
43
|
-
* Accepts whatever {@link parseNumber} accepts, then requires the result to have
|
|
44
|
-
* no fractional part. `3.14` / `'3.14'` → `undefined`.
|
|
45
|
-
*
|
|
46
|
-
* @param value - The value to parse
|
|
47
|
-
* @returns A finite integer, or `undefined`
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* parseInteger(42) // 42
|
|
52
|
-
* parseInteger(3.14) // undefined
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export declare function parseInteger(value: unknown): number | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* Parse an unknown value to a boolean.
|
|
58
|
-
*
|
|
59
|
-
* @remarks
|
|
60
|
-
* A boolean is returned unchanged. The strings `'true'` / `'false'` / `'1'` /
|
|
61
|
-
* `'0'` and the numbers `1` / `0` coerce to the matching boolean. Everything
|
|
62
|
-
* else → `undefined`.
|
|
63
|
-
*
|
|
64
|
-
* @param value - The value to parse
|
|
65
|
-
* @returns A boolean, or `undefined`
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* parseBoolean(true) // true
|
|
70
|
-
* parseBoolean('1') // true
|
|
71
|
-
* parseBoolean('nope') // undefined
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
export declare function parseBoolean(value: unknown): boolean | undefined;
|
|
75
|
-
/**
|
|
76
|
-
* Parse an unknown value to `null`.
|
|
77
|
-
*
|
|
78
|
-
* @remarks
|
|
79
|
-
* A successful parse returns `null` itself — distinct from the `undefined`
|
|
80
|
-
* failure sentinel every other parser in this file uses. Only `null` passes;
|
|
81
|
-
* every other value (including `undefined`) → `undefined`.
|
|
82
|
-
*
|
|
83
|
-
* @param value - The value to parse
|
|
84
|
-
* @returns `null` on a successful parse, or `undefined`
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```ts
|
|
88
|
-
* parseNull(null) // null
|
|
89
|
-
* parseNull(undefined) // undefined
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
export declare function parseNull(value: unknown): null | undefined;
|
|
93
|
-
/**
|
|
94
|
-
* Parse an unknown value to a plain record — the input reference, never cloned.
|
|
95
|
-
*
|
|
96
|
-
* @param value - The value to parse
|
|
97
|
-
* @returns The record, or `undefined`
|
|
98
|
-
*/
|
|
99
|
-
export declare function parseRecord(value: unknown): Record<string, unknown> | undefined;
|
|
100
|
-
/**
|
|
101
|
-
* Parse an unknown value to an array — the input reference, never cloned —
|
|
102
|
-
* optionally guarding every element.
|
|
103
|
-
*
|
|
104
|
-
* @remarks
|
|
105
|
-
* Without a `guard`, element types are NOT verified; let `T` default to
|
|
106
|
-
* `unknown` rather than asserting a specific element type.
|
|
107
|
-
*
|
|
108
|
-
* @param value - The value to parse
|
|
109
|
-
* @param guard - Optional element guard
|
|
110
|
-
* @returns The array, or `undefined`
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```ts
|
|
114
|
-
* parseArray([1, 2]) // [1, 2]
|
|
115
|
-
* parseArray([1, 'x'], isNumber) // undefined
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
export declare function parseArray<T = unknown>(value: unknown, guard?: Guard<T>): readonly T[] | undefined;
|
|
119
|
-
/**
|
|
120
|
-
* Parse an unknown value to a cycle-safe JSON value — the input reference,
|
|
121
|
-
* never cloned.
|
|
122
|
-
*
|
|
123
|
-
* @remarks
|
|
124
|
-
* Unlike {@link parseRecord} / {@link parseArray}, this is a DEEP gate: it
|
|
125
|
-
* walks the entire tree via {@link isJSONValue} rather than checking only the
|
|
126
|
-
* top-level shape. That walk is cycle-safe and total (never throws) because
|
|
127
|
-
* `isJSONValue` runs its own probe inside a guard, so an adversarial
|
|
128
|
-
* structure (a cycle, a hostile getter) yields `undefined` instead of hanging
|
|
129
|
-
* or throwing.
|
|
130
|
-
*
|
|
131
|
-
* @param value - The value to parse
|
|
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
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
export declare function parseJSONValue(value: unknown): JSONValue | undefined;
|
|
141
|
-
/**
|
|
142
|
-
* Parse an unknown value as one of the allowed literal primitives.
|
|
143
|
-
*
|
|
144
|
-
* @remarks
|
|
145
|
-
* Pairs with {@link literalOf} — both match by `Object.is`, so the
|
|
146
|
-
* `parseEnum ↔ literalOf(...allowed)` pairing covers every literal primitive
|
|
147
|
-
* (string, number, or boolean), not only strings. Matching is identity, never
|
|
148
|
-
* cross-type coercion: `parseEnum('1', [1])` stays `undefined`.
|
|
149
|
-
*
|
|
150
|
-
* @param value - The value to parse
|
|
151
|
-
* @param allowed - The permitted literal values
|
|
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
|
-
* ```
|
|
159
|
-
*/
|
|
160
|
-
export declare function parseEnum<const T extends string | number | boolean>(value: unknown, allowed: readonly T[]): T | undefined;
|
|
161
|
-
/**
|
|
162
|
-
* Read and parse a string field from a record by key or nested key path.
|
|
163
|
-
*
|
|
164
|
-
* @param record - The source record
|
|
165
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
166
|
-
* @returns A string, or `undefined`
|
|
167
|
-
*/
|
|
168
|
-
export declare function parseStringField(record: Record<string, unknown>, path: FieldPath): string | undefined;
|
|
169
|
-
/**
|
|
170
|
-
* Read and parse a finite-number field from a record by key or nested key path.
|
|
171
|
-
*
|
|
172
|
-
* @param record - The source record
|
|
173
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
174
|
-
* @returns A finite number, or `undefined`
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* ```ts
|
|
178
|
-
* parseNumberField({ age: '42' }, 'age') // 42
|
|
179
|
-
* parseNumberField({}, 'age') // undefined
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
export declare function parseNumberField(record: Record<string, unknown>, path: FieldPath): number | undefined;
|
|
183
|
-
/**
|
|
184
|
-
* Read and parse a finite-integer field from a record by key or nested key path.
|
|
185
|
-
*
|
|
186
|
-
* @param record - The source record
|
|
187
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
188
|
-
* @returns A finite integer, or `undefined`
|
|
189
|
-
*/
|
|
190
|
-
export declare function parseIntegerField(record: Record<string, unknown>, path: FieldPath): number | undefined;
|
|
191
|
-
/**
|
|
192
|
-
* Read and parse a boolean field from a record by key or nested key path.
|
|
193
|
-
*
|
|
194
|
-
* @param record - The source record
|
|
195
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
196
|
-
* @returns A boolean, or `undefined`
|
|
197
|
-
*
|
|
198
|
-
* @example
|
|
199
|
-
* ```ts
|
|
200
|
-
* parseBooleanField({ on: 'true' }, 'on') // true
|
|
201
|
-
* parseBooleanField({}, 'on') // undefined
|
|
202
|
-
* ```
|
|
203
|
-
*/
|
|
204
|
-
export declare function parseBooleanField(record: Record<string, unknown>, path: FieldPath): boolean | undefined;
|
|
205
|
-
/**
|
|
206
|
-
* Read and parse a `null` field from a record by key or nested key path.
|
|
207
|
-
*
|
|
208
|
-
* @remarks
|
|
209
|
-
* A successful parse returns `null` itself — distinct from the `undefined`
|
|
210
|
-
* failure sentinel, which also covers a missing field.
|
|
211
|
-
*
|
|
212
|
-
* @param record - The source record
|
|
213
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
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
|
-
* ```
|
|
221
|
-
*/
|
|
222
|
-
export declare function parseNullField(record: Record<string, unknown>, path: FieldPath): null | undefined;
|
|
223
|
-
/**
|
|
224
|
-
* Read and parse a nested record field from a record by key or nested key path.
|
|
225
|
-
*
|
|
226
|
-
* @param record - The source record
|
|
227
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
228
|
-
* @returns A plain record, or `undefined`
|
|
229
|
-
*/
|
|
230
|
-
export declare function parseRecordField(record: Record<string, unknown>, path: FieldPath): Record<string, unknown> | undefined;
|
|
231
|
-
/**
|
|
232
|
-
* Read and parse an array field from a record by key or nested key path,
|
|
233
|
-
* optionally guarding elements.
|
|
234
|
-
*
|
|
235
|
-
* @param record - The source record
|
|
236
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
237
|
-
* @param guard - Optional element guard
|
|
238
|
-
* @returns An array, or `undefined`
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```ts
|
|
242
|
-
* parseArrayField({ tags: [1, 2] }, 'tags') // [1, 2]
|
|
243
|
-
* parseArrayField({}, 'tags') // undefined
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
export declare function parseArrayField<T = unknown>(record: Record<string, unknown>, path: FieldPath, guard?: Guard<T>): readonly T[] | undefined;
|
|
247
|
-
/**
|
|
248
|
-
* Read and parse an enum field from a record by key or nested key path.
|
|
249
|
-
*
|
|
250
|
-
* @param record - The source record
|
|
251
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
252
|
-
* @param allowed - The permitted literal values
|
|
253
|
-
* @returns The matched literal, or `undefined`
|
|
254
|
-
*/
|
|
255
|
-
export declare function parseEnumField<const T extends string | number | boolean>(record: Record<string, unknown>, path: FieldPath, allowed: readonly T[]): T | undefined;
|
|
256
|
-
/**
|
|
257
|
-
* Read and parse a JSON-value field from a record by key or nested key path.
|
|
258
|
-
*
|
|
259
|
-
* @remarks
|
|
260
|
-
* Deep-gates the field's whole subtree via {@link parseJSONValue} — see that
|
|
261
|
-
* function's remarks for why this differs from the shallow
|
|
262
|
-
* {@link parseRecordField} / {@link parseArrayField}.
|
|
263
|
-
*
|
|
264
|
-
* @param record - The source record
|
|
265
|
-
* @param path - A property key, or a key path descending into nested objects
|
|
266
|
-
* @returns The value, or `undefined`
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```ts
|
|
270
|
-
* parseJSONValueField({ data: { a: 1 } }, 'data') // { a: 1 }
|
|
271
|
-
* parseJSONValueField({}, 'data') // undefined
|
|
272
|
-
* ```
|
|
273
|
-
*/
|
|
274
|
-
export declare function parseJSONValueField(record: Record<string, unknown>, path: FieldPath): JSONValue | undefined;
|
|
275
|
-
/**
|
|
276
|
-
* Parse a JSON string, returning `undefined` instead of throwing.
|
|
277
|
-
*
|
|
278
|
-
* @remarks
|
|
279
|
-
* The safe boundary for untrusted JSON text: a malformed string yields
|
|
280
|
-
* `undefined`, never an exception. Returns `unknown` — a successful parse proves
|
|
281
|
-
* nothing about shape, so narrow the result with a guard (or use
|
|
282
|
-
* {@link parseJSONAs}). A large document is not walked here; parsing is shallow
|
|
283
|
-
* and lazy validation is the caller's to compose.
|
|
284
|
-
*
|
|
285
|
-
* @param value - The JSON string to parse
|
|
286
|
-
* @returns The parsed value, or `undefined` when `value` is not valid JSON
|
|
287
|
-
*/
|
|
288
|
-
export declare function parseJSON(value: string): unknown;
|
|
289
|
-
/**
|
|
290
|
-
* Parse a JSON string and validate the result against a guard.
|
|
291
|
-
*
|
|
292
|
-
* @remarks
|
|
293
|
-
* The lazy, safe path from an untrusted string to a typed `T`: parse, then check
|
|
294
|
-
* the parsed value with the guard you bring — typically one composed from the
|
|
295
|
-
* combinators (`recordOf`, `arrayOf`, …). Only the shape the guard inspects is
|
|
296
|
-
* validated, so a large document is never walked in full unless the guard does.
|
|
297
|
-
*
|
|
298
|
-
* @param value - The JSON string to parse
|
|
299
|
-
* @param guard - The guard for the expected shape
|
|
300
|
-
* @returns The parsed value when it satisfies `guard`, otherwise `undefined`
|
|
301
|
-
*
|
|
302
|
-
* @example
|
|
303
|
-
* ```ts
|
|
304
|
-
* const isConfig = recordOf({ host: isString, tags: arrayOf(isString) })
|
|
305
|
-
* parseJSONAs('{"host":"localhost","tags":["a"]}', isConfig) // { host: 'localhost', tags: ['a'] }
|
|
306
|
-
* parseJSONAs('{"host":"localhost"}', isConfig) // undefined — guard fails
|
|
307
|
-
* parseJSONAs('not json', isConfig) // undefined — never throws
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
310
|
-
export declare function parseJSONAs<T>(value: string, guard: Guard<T>): T | undefined;
|
|
@@ -1,231 +0,0 @@
|
|
|
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
|
-
/**
|
|
3
|
-
* Build a string {@link StringShape}.
|
|
4
|
-
*
|
|
5
|
-
* @param options - Optional length (`min` / `max`), `pattern`, and `description`
|
|
6
|
-
* @returns A string shape
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* const name = stringShape({ min: 1, max: 80, description: 'Display name' })
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
export declare function stringShape(options?: StringShapeOptions): StringShape;
|
|
14
|
-
/**
|
|
15
|
-
* Build a numeric {@link NumberShape}.
|
|
16
|
-
*
|
|
17
|
-
* @param options - Optional bounds (`min` / `max`), `integer`, and `description`
|
|
18
|
-
* @returns A number shape
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* const age = numberShape({ min: 0, max: 120 })
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export declare function numberShape(options?: NumberShapeOptions): NumberShape;
|
|
26
|
-
/**
|
|
27
|
-
* Build an integer {@link NumberShape} — forces `integer: true`.
|
|
28
|
-
*
|
|
29
|
-
* @remarks
|
|
30
|
-
* The emitted JSON Schema uses `"type": "integer"` and the guard rejects
|
|
31
|
-
* fractional numbers.
|
|
32
|
-
*
|
|
33
|
-
* @param options - Optional bounds and `description` (no `integer` key)
|
|
34
|
-
* @returns An integer number shape
|
|
35
|
-
*/
|
|
36
|
-
export declare function integerShape(options?: Omit<NumberShapeOptions, 'integer'>): NumberShape;
|
|
37
|
-
/**
|
|
38
|
-
* Build a {@link BooleanShape}.
|
|
39
|
-
*
|
|
40
|
-
* @param options - Optional `description`
|
|
41
|
-
* @returns A boolean shape
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```ts
|
|
45
|
-
* const active = booleanShape({ description: 'Whether the record is active' })
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export declare function booleanShape(options?: BooleanShapeOptions): BooleanShape;
|
|
49
|
-
/**
|
|
50
|
-
* Build a {@link NullShape}.
|
|
51
|
-
*
|
|
52
|
-
* @param options - Optional `description`
|
|
53
|
-
* @returns A null shape
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* const empty = nullShape()
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
export declare function nullShape(options?: NullShapeOptions): NullShape;
|
|
61
|
-
/**
|
|
62
|
-
* Build a literal shape from a fixed set of primitive values.
|
|
63
|
-
*
|
|
64
|
-
* @param values - The permitted literals
|
|
65
|
-
* @param options - Optional `description`
|
|
66
|
-
* @returns A literal shape whose `Infer` is the union of `values`
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* const role = literalShape(['admin', 'member', 'guest'])
|
|
71
|
-
* // Infer<typeof role> = 'admin' | 'member' | 'guest'
|
|
72
|
-
*
|
|
73
|
-
* const via = literalShape(['function', 'tool', 'agent'], { description: 'How to run the step.' })
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
export declare function literalShape<const T extends readonly (string | number | boolean)[]>(values: T, options?: LiteralShapeOptions): LiteralShape<T>;
|
|
77
|
-
/**
|
|
78
|
-
* Build an {@link ArrayShape} from an element shape.
|
|
79
|
-
*
|
|
80
|
-
* @param items - The element shape
|
|
81
|
-
* @param options - Optional length bounds and `description`
|
|
82
|
-
* @returns An array shape
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* const tags = arrayShape(stringShape(), { max: 10 })
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
export declare function arrayShape<S extends ContractShape>(items: S, options?: ArrayShapeOptions): ArrayShape<S>;
|
|
90
|
-
/**
|
|
91
|
-
* Build an {@link ObjectShape} from a property map.
|
|
92
|
-
*
|
|
93
|
-
* @remarks
|
|
94
|
-
* Wrap any property in {@link optionalShape} to allow its absence. By default
|
|
95
|
-
* the compiled guard rejects unknown keys; pass `additionalProperties` to open
|
|
96
|
-
* the object.
|
|
97
|
-
*
|
|
98
|
-
* @param properties - Map of property names to child shapes
|
|
99
|
-
* @param options - Optional `additionalProperties` and `description`
|
|
100
|
-
* @returns An object shape
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* ```ts
|
|
104
|
-
* const user = objectShape({
|
|
105
|
-
* name: stringShape({ min: 1 }),
|
|
106
|
-
* age: integerShape({ min: 0, max: 120 }),
|
|
107
|
-
* bio: optionalShape(stringShape()),
|
|
108
|
-
* })
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
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
|
-
/**
|
|
113
|
-
* Build an open {@link ObjectShape} with no fixed properties — a dictionary.
|
|
114
|
-
*
|
|
115
|
-
* @remarks
|
|
116
|
-
* Every value is validated against `values`; keys are unconstrained. Equivalent
|
|
117
|
-
* to `objectShape({}, { additionalProperties: values })`.
|
|
118
|
-
*
|
|
119
|
-
* @param values - The shape every value must match
|
|
120
|
-
* @param options - Optional `description`
|
|
121
|
-
* @returns An open object shape
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* const bindings = recordShape(numberShape()) // ~ Record<string, number>
|
|
126
|
-
* ```
|
|
127
|
-
*/
|
|
128
|
-
export declare function recordShape<S extends ContractShape>(values: S, options?: RecordShapeOptions): ObjectShape<Record<never, never>, S>;
|
|
129
|
-
/**
|
|
130
|
-
* Build a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
|
|
131
|
-
*
|
|
132
|
-
* @param variants - The candidate shapes; the first match wins at runtime
|
|
133
|
-
* @returns A union shape whose `Infer` is the union of the variants
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* ```ts
|
|
137
|
-
* const id = unionShape(stringShape(), integerShape())
|
|
138
|
-
* // Infer<typeof id> = string | number
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
export declare function unionShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<V>;
|
|
142
|
-
/**
|
|
143
|
-
* Build a {@link UnionShape} that emits `oneOf` (exactly one match) in JSON Schema.
|
|
144
|
-
*
|
|
145
|
-
* @remarks
|
|
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.
|
|
163
|
-
*
|
|
164
|
-
* @param variants - The candidate shapes
|
|
165
|
-
* @returns A union shape with `mode: 'oneOf'`
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* ```ts
|
|
169
|
-
* const id = oneOfShape(numberShape(), integerShape())
|
|
170
|
-
* // 3 fails — matches both numberShape and integerShape
|
|
171
|
-
* // 3.5 passes — matches numberShape only
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
export declare function oneOfShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<V>;
|
|
175
|
-
/**
|
|
176
|
-
* Wrap a shape so it may be absent (`undefined`).
|
|
177
|
-
*
|
|
178
|
-
* @remarks
|
|
179
|
-
* As an {@link objectShape} property, the field becomes a true optional property
|
|
180
|
-
* in the inferred type.
|
|
181
|
-
*
|
|
182
|
-
* @param inner - The wrapped shape
|
|
183
|
-
* @returns An optional shape
|
|
184
|
-
*/
|
|
185
|
-
export declare function optionalShape<S extends ContractShape>(inner: S): OptionalShape<S>;
|
|
186
|
-
/**
|
|
187
|
-
* Wrap a shape so it may be `null`.
|
|
188
|
-
*
|
|
189
|
-
* @param inner - The wrapped shape
|
|
190
|
-
* @returns A nullable shape
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```ts
|
|
194
|
-
* const bio = nullableShape(stringShape())
|
|
195
|
-
* // Infer<typeof bio> = string | null
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
export declare function nullableShape<S extends ContractShape>(inner: S): NullableShape<S>;
|
|
199
|
-
/**
|
|
200
|
-
* Build a {@link JSONShape}.
|
|
201
|
-
*
|
|
202
|
-
* @remarks
|
|
203
|
-
* The sound counterpart of {@link rawShape}: `rawShape` embeds an arbitrary
|
|
204
|
-
* schema fragment and accepts anything at runtime, while `jsonShape` validates
|
|
205
|
-
* that a value is real JSON (via {@link isJSONValue}).
|
|
206
|
-
*
|
|
207
|
-
* @param options - Optional `description`
|
|
208
|
-
* @returns A JSON passthrough shape
|
|
209
|
-
*
|
|
210
|
-
* @example
|
|
211
|
-
* ```ts
|
|
212
|
-
* const payload = jsonShape({ description: 'Arbitrary JSON payload' })
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
export declare function jsonShape(options?: JSONShapeOptions): JSONShape;
|
|
216
|
-
/**
|
|
217
|
-
* Build a {@link RawShape} from a JSON Schema fragment.
|
|
218
|
-
*
|
|
219
|
-
* @remarks
|
|
220
|
-
* For values the shape DSL can't express. The compiled guard accepts any value;
|
|
221
|
-
* the parser passes it through; the schema is emitted verbatim.
|
|
222
|
-
*
|
|
223
|
-
* @param schema - The JSON Schema fragment to embed
|
|
224
|
-
* @returns A raw shape
|
|
225
|
-
*
|
|
226
|
-
* @example
|
|
227
|
-
* ```ts
|
|
228
|
-
* const custom = rawShape({ type: 'string', format: 'uuid' })
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
export declare function rawShape(schema: JSONSchema): RawShape;
|