@orkestrel/contract 0.0.3 → 0.0.5
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 +45 -3
- package/dist/src/core/constants.d.ts +20 -0
- package/dist/src/core/helpers.d.ts +48 -1
- package/dist/src/core/index.cjs +478 -36
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +473 -37
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/shapers.d.ts +20 -4
- package/dist/src/core/types.d.ts +96 -6
- package/dist/src/core/validators.d.ts +57 -2
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FromGuards, Guard, GuardsShape, GuardType, IntersectionFromGuards, OptionalFromGuards, TupleFromGuards } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Build a guard that accepts arrays whose every element satisfies `elementGuard`.
|
|
4
4
|
*
|
|
@@ -50,7 +50,7 @@ export declare function literalOf<const Literals extends ReadonlyArray<string |
|
|
|
50
50
|
* isDateValue({}) // false
|
|
51
51
|
* ```
|
|
52
52
|
*/
|
|
53
|
-
export declare function instanceOf<C>(ctor: C): Guard<InstanceType<C
|
|
53
|
+
export declare function instanceOf<C extends abstract new (...args: never) => object>(ctor: C): Guard<InstanceType<C>>;
|
|
54
54
|
/**
|
|
55
55
|
* Build a guard from a native `enum` or any object whose values are strings or
|
|
56
56
|
* numbers.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ContractInterface, ContractShape, Guard, Infer, JSONSchema, Parser, RandomFunction } from './types.js';
|
|
1
|
+
import { ContractInterface, ContractShape, Fault, Guard, Infer, JSONSchema, Parser, RandomFunction } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Validate that a {@link ContractShape} tree is well-formed — a pure recursive
|
|
4
4
|
* prepass run before compilation.
|
|
@@ -125,6 +125,46 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
125
125
|
*/
|
|
126
126
|
export declare function compileGenerator<S extends ContractShape>(shape: S, random?: RandomFunction): Infer<S>;
|
|
127
127
|
export declare function compileGenerator(shape: ContractShape, random?: RandomFunction): unknown;
|
|
128
|
+
/**
|
|
129
|
+
* Compile a {@link ContractShape} into a structured fault report for a value —
|
|
130
|
+
* the diagnostic counterpart of {@link compileGuard} / {@link compileParser}.
|
|
131
|
+
*
|
|
132
|
+
* @remarks
|
|
133
|
+
* MIRROR-PARSE semantics: reuses the exact leaf parsers/guards
|
|
134
|
+
* {@link compileParser} uses (`parseString` / `parseNumber` / `parseBoolean` /
|
|
135
|
+
* `isJSONValue` / …), so the soundness invariant
|
|
136
|
+
* `compileReporter(shape, v).length === 0 ⟺ compileParser(shape)(v) !== undefined`
|
|
137
|
+
* holds structurally — `explain` mirrors `parse`, not the stricter `is` (a
|
|
138
|
+
* coercible value like `'42'` against a `numberShape` reports no fault, the
|
|
139
|
+
* same leniency `parse` grants, even though the strict guard would reject it).
|
|
140
|
+
*
|
|
141
|
+
* Faults are collected in stable pre-order (declared key/index order); every
|
|
142
|
+
* call — object, array, and union alike, including a union's `oneOf` "no
|
|
143
|
+
* match" / "no consensus" summary fault prepended to its closest variant's
|
|
144
|
+
* faults — slices its return to at most {@link FAULT_LIMIT} entries, so the
|
|
145
|
+
* bound holds at every level of nesting, not just the outermost container, on
|
|
146
|
+
* adversarial input (a huge array, a wide record, a wide union of wide
|
|
147
|
+
* records). A closed object's extra keys never
|
|
148
|
+
* fault — `parse` silently drops them, so `explain` mirrors that leniency; an
|
|
149
|
+
* open object with a constraining `additionalProperties` shape recurses extras
|
|
150
|
+
* against it instead. A hostile getter or throwing `Proxy` trap is contained
|
|
151
|
+
* via {@link attempt} and surfaces as a single top-level type fault, never a
|
|
152
|
+
* throw (AGENTS §14).
|
|
153
|
+
*
|
|
154
|
+
* @param shape - The shape to report against
|
|
155
|
+
* @param value - The value to check
|
|
156
|
+
* @param path - The path prefix for faults produced at this call (defaults to the root)
|
|
157
|
+
* @returns The faults found, empty when the value parses successfully
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* const user = objectShape({ name: stringShape({ min: 1 }) })
|
|
162
|
+
* compileReporter(user, { name: '' })
|
|
163
|
+
* // [{ reason: 'constraint', path: ['name'], expected: 'string', constraint: 'min', limit: 1, received: '""' }]
|
|
164
|
+
* compileReporter(user, { name: 'Ada' }) // []
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare function compileReporter(shape: ContractShape, value: unknown, path?: string[]): readonly Fault[];
|
|
128
168
|
/**
|
|
129
169
|
* Compile a {@link ContractShape} into a {@link ContractInterface} — the four
|
|
130
170
|
* lockstep outputs from one declaration.
|
|
@@ -133,10 +173,12 @@ export declare function compileGenerator(shape: ContractShape, random?: RandomFu
|
|
|
133
173
|
* Runs {@link validateShape} first — a malformed shape throws immediately
|
|
134
174
|
* rather than compiling into a silently-wrong contract (AGENTS §12). Then
|
|
135
175
|
* precompiles the schema, guard, and parser once; `generate` walks the shape
|
|
136
|
-
* per call with the supplied random source
|
|
176
|
+
* per call with the supplied random source, and `explain` compiles the
|
|
177
|
+
* diagnostic report via {@link compileReporter} at zero added compile-time cost
|
|
178
|
+
* (it re-walks the shape per call, exactly like `generate`).
|
|
137
179
|
*
|
|
138
180
|
* @param shape - The shape to compile
|
|
139
|
-
* @returns A contract bundling `schema` / `is` / `parse` / `generate`
|
|
181
|
+
* @returns A contract bundling `schema` / `is` / `parse` / `explain` / `generate`
|
|
140
182
|
*
|
|
141
183
|
* @example
|
|
142
184
|
* ```ts
|
|
@@ -18,3 +18,23 @@ import { JSONSchemaType } from './types.js';
|
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
20
|
export declare const JSON_SCHEMA_TYPES: readonly JSONSchemaType[];
|
|
21
|
+
/**
|
|
22
|
+
* The maximum number of {@link Fault} entries a single `explain` report ever
|
|
23
|
+
* returns, frozen.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Bounds the report against adversarial input (a giant array, a wide record) —
|
|
27
|
+
* `compileReporter` collects faults in stable pre-order and stops once this cap
|
|
28
|
+
* is reached, so the report size (and the work to build it) stays finite and
|
|
29
|
+
* deterministic regardless of the input's size.
|
|
30
|
+
*/
|
|
31
|
+
export declare const FAULT_LIMIT = 64;
|
|
32
|
+
/**
|
|
33
|
+
* The maximum character length of a {@link preview}-rendered string, frozen.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* A previewed string longer than this is clipped with a trailing `…` so a
|
|
37
|
+
* {@link Fault}'s `received` field never embeds an unbounded amount of
|
|
38
|
+
* untrusted text.
|
|
39
|
+
*/
|
|
40
|
+
export declare const PREVIEW_LIMIT = 64;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FieldPath, JSONSchema, RandomFunction, Result } from './types.js';
|
|
1
|
+
import { ContractShape, FaultKind, FieldPath, JSONSchema, RandomFunction, Result } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Invoke a callback and capture its outcome as a {@link Result}, never letting
|
|
4
4
|
* a throw escape.
|
|
@@ -110,3 +110,50 @@ export declare function enumerableSymbolCount(value: object): number;
|
|
|
110
110
|
* ```
|
|
111
111
|
*/
|
|
112
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;
|