@orkestrel/contract 0.0.4 → 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/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 +355 -0
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +351 -1
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/types.d.ts +58 -0
- package/package.json +1 -1
|
@@ -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;
|
package/dist/src/core/index.cjs
CHANGED
|
@@ -27,6 +27,26 @@ var JSON_SCHEMA_TYPES = Object.freeze([
|
|
|
27
27
|
"integer",
|
|
28
28
|
"string"
|
|
29
29
|
]);
|
|
30
|
+
/**
|
|
31
|
+
* The maximum number of {@link Fault} entries a single `explain` report ever
|
|
32
|
+
* returns, frozen.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Bounds the report against adversarial input (a giant array, a wide record) —
|
|
36
|
+
* `compileReporter` collects faults in stable pre-order and stops once this cap
|
|
37
|
+
* is reached, so the report size (and the work to build it) stays finite and
|
|
38
|
+
* deterministic regardless of the input's size.
|
|
39
|
+
*/
|
|
40
|
+
var FAULT_LIMIT = 64;
|
|
41
|
+
/**
|
|
42
|
+
* The maximum character length of a {@link preview}-rendered string, frozen.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* A previewed string longer than this is clipped with a trailing `…` so a
|
|
46
|
+
* {@link Fault}'s `received` field never embeds an unbounded amount of
|
|
47
|
+
* untrusted text.
|
|
48
|
+
*/
|
|
49
|
+
var PREVIEW_LIMIT = 64;
|
|
30
50
|
//#endregion
|
|
31
51
|
//#region src/core/validators.ts
|
|
32
52
|
/** Determine whether a value is `null`.
|
|
@@ -1080,6 +1100,79 @@ function enumerableSymbolCount(value) {
|
|
|
1080
1100
|
function schemaToParameters(schema) {
|
|
1081
1101
|
return isRecord(schema) ? schema : void 0;
|
|
1082
1102
|
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Render a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
|
|
1105
|
+
* `received` field.
|
|
1106
|
+
*
|
|
1107
|
+
* @remarks
|
|
1108
|
+
* A primitive renders as its literal: a string is `JSON.stringify`-escaped and
|
|
1109
|
+
* clipped to {@link PREVIEW_LIMIT} characters (with a trailing `…` when
|
|
1110
|
+
* clipped); a number / boolean / bigint / symbol renders via `String`; `null`
|
|
1111
|
+
* and `undefined` render as their own name. Everything else — a plain object,
|
|
1112
|
+
* an array, a function, a class instance, a `Map` — is NEVER traversed or
|
|
1113
|
+
* stringified; it renders as its bare `typeof` tag (`'object'` / `'function'`),
|
|
1114
|
+
* so a hostile or enormous structure can never blow up the preview.
|
|
1115
|
+
*
|
|
1116
|
+
* @param value - The value to preview
|
|
1117
|
+
* @returns A short descriptive string, always safe to embed in a diagnostic
|
|
1118
|
+
*
|
|
1119
|
+
* @example
|
|
1120
|
+
* ```ts
|
|
1121
|
+
* preview('hi') // '"hi"'
|
|
1122
|
+
* preview(42) // '42'
|
|
1123
|
+
* preview(null) // 'null'
|
|
1124
|
+
* preview({ a: 1 }) // 'object'
|
|
1125
|
+
* preview([1, 2, 3]) // 'object'
|
|
1126
|
+
* ```
|
|
1127
|
+
*/
|
|
1128
|
+
function preview(value) {
|
|
1129
|
+
if (value === null) return "null";
|
|
1130
|
+
if (value === void 0) return "undefined";
|
|
1131
|
+
if (isString(value)) {
|
|
1132
|
+
const text = JSON.stringify(value);
|
|
1133
|
+
return text.length > 64 ? `${text.slice(0, 64)}…` : text;
|
|
1134
|
+
}
|
|
1135
|
+
if (isNumber(value) || isBoolean(value)) return String(value);
|
|
1136
|
+
if (isBigInt(value)) return `${value}n`;
|
|
1137
|
+
if (isSymbol(value)) return value.toString();
|
|
1138
|
+
return typeof value;
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Project a {@link ContractShape} to the {@link FaultKind} it describes.
|
|
1142
|
+
*
|
|
1143
|
+
* @remarks
|
|
1144
|
+
* A structural mapping used by {@link compileReporter} to fill a `Fault`'s
|
|
1145
|
+
* `expected` field: most shapes map to their own `type` (`numberShape` maps to
|
|
1146
|
+
* `'integer'` when `integer: true`, else `'number'`); `optionalShape` /
|
|
1147
|
+
* `nullableShape` project through to their inner shape's kind, and `rawShape`
|
|
1148
|
+
* (an arbitrary embedded schema with no fixed kind) projects to `'json'`.
|
|
1149
|
+
*
|
|
1150
|
+
* @param shape - The shape to project
|
|
1151
|
+
* @returns The shape's {@link FaultKind}
|
|
1152
|
+
*
|
|
1153
|
+
* @example
|
|
1154
|
+
* ```ts
|
|
1155
|
+
* shapeToKind(stringShape()) // 'string'
|
|
1156
|
+
* shapeToKind(integerShape()) // 'integer'
|
|
1157
|
+
* shapeToKind(optionalShape(nullShape())) // 'null'
|
|
1158
|
+
* ```
|
|
1159
|
+
*/
|
|
1160
|
+
function shapeToKind(shape) {
|
|
1161
|
+
switch (shape.type) {
|
|
1162
|
+
case "string": return "string";
|
|
1163
|
+
case "number": return shape.integer === true ? "integer" : "number";
|
|
1164
|
+
case "boolean": return "boolean";
|
|
1165
|
+
case "null": return "null";
|
|
1166
|
+
case "literal": return "literal";
|
|
1167
|
+
case "array": return "array";
|
|
1168
|
+
case "object": return "object";
|
|
1169
|
+
case "union": return "union";
|
|
1170
|
+
case "json": return "json";
|
|
1171
|
+
case "optional": return shapeToKind(shape.inner);
|
|
1172
|
+
case "nullable": return shapeToKind(shape.inner);
|
|
1173
|
+
case "raw": return "json";
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1083
1176
|
//#endregion
|
|
1084
1177
|
//#region src/core/combinators.ts
|
|
1085
1178
|
function arrayOf(elementGuard) {
|
|
@@ -2257,6 +2350,260 @@ function compileGenerator(shape, random = seededRandom(Date.now())) {
|
|
|
2257
2350
|
case "raw": throw new Error("compileGenerator: a raw shape embeds an arbitrary JSON Schema and cannot be auto-generated — supply values another way");
|
|
2258
2351
|
}
|
|
2259
2352
|
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Compile a {@link ContractShape} into a structured fault report for a value —
|
|
2355
|
+
* the diagnostic counterpart of {@link compileGuard} / {@link compileParser}.
|
|
2356
|
+
*
|
|
2357
|
+
* @remarks
|
|
2358
|
+
* MIRROR-PARSE semantics: reuses the exact leaf parsers/guards
|
|
2359
|
+
* {@link compileParser} uses (`parseString` / `parseNumber` / `parseBoolean` /
|
|
2360
|
+
* `isJSONValue` / …), so the soundness invariant
|
|
2361
|
+
* `compileReporter(shape, v).length === 0 ⟺ compileParser(shape)(v) !== undefined`
|
|
2362
|
+
* holds structurally — `explain` mirrors `parse`, not the stricter `is` (a
|
|
2363
|
+
* coercible value like `'42'` against a `numberShape` reports no fault, the
|
|
2364
|
+
* same leniency `parse` grants, even though the strict guard would reject it).
|
|
2365
|
+
*
|
|
2366
|
+
* Faults are collected in stable pre-order (declared key/index order); every
|
|
2367
|
+
* call — object, array, and union alike, including a union's `oneOf` "no
|
|
2368
|
+
* match" / "no consensus" summary fault prepended to its closest variant's
|
|
2369
|
+
* faults — slices its return to at most {@link FAULT_LIMIT} entries, so the
|
|
2370
|
+
* bound holds at every level of nesting, not just the outermost container, on
|
|
2371
|
+
* adversarial input (a huge array, a wide record, a wide union of wide
|
|
2372
|
+
* records). A closed object's extra keys never
|
|
2373
|
+
* fault — `parse` silently drops them, so `explain` mirrors that leniency; an
|
|
2374
|
+
* open object with a constraining `additionalProperties` shape recurses extras
|
|
2375
|
+
* against it instead. A hostile getter or throwing `Proxy` trap is contained
|
|
2376
|
+
* via {@link attempt} and surfaces as a single top-level type fault, never a
|
|
2377
|
+
* throw (AGENTS §14).
|
|
2378
|
+
*
|
|
2379
|
+
* @param shape - The shape to report against
|
|
2380
|
+
* @param value - The value to check
|
|
2381
|
+
* @param path - The path prefix for faults produced at this call (defaults to the root)
|
|
2382
|
+
* @returns The faults found, empty when the value parses successfully
|
|
2383
|
+
*
|
|
2384
|
+
* @example
|
|
2385
|
+
* ```ts
|
|
2386
|
+
* const user = objectShape({ name: stringShape({ min: 1 }) })
|
|
2387
|
+
* compileReporter(user, { name: '' })
|
|
2388
|
+
* // [{ reason: 'constraint', path: ['name'], expected: 'string', constraint: 'min', limit: 1, received: '""' }]
|
|
2389
|
+
* compileReporter(user, { name: 'Ada' }) // []
|
|
2390
|
+
* ```
|
|
2391
|
+
*/
|
|
2392
|
+
function compileReporter(shape, value, path = []) {
|
|
2393
|
+
switch (shape.type) {
|
|
2394
|
+
case "string": {
|
|
2395
|
+
const parsed = parseString(value);
|
|
2396
|
+
if (parsed === void 0) return [{
|
|
2397
|
+
reason: "type",
|
|
2398
|
+
path,
|
|
2399
|
+
expected: "string",
|
|
2400
|
+
received: preview(value)
|
|
2401
|
+
}];
|
|
2402
|
+
const faults = [];
|
|
2403
|
+
if (shape.min !== void 0 && parsed.length < shape.min) faults.push({
|
|
2404
|
+
reason: "constraint",
|
|
2405
|
+
path,
|
|
2406
|
+
expected: "string",
|
|
2407
|
+
constraint: "min",
|
|
2408
|
+
limit: shape.min,
|
|
2409
|
+
received: preview(parsed)
|
|
2410
|
+
});
|
|
2411
|
+
if (shape.max !== void 0 && parsed.length > shape.max) faults.push({
|
|
2412
|
+
reason: "constraint",
|
|
2413
|
+
path,
|
|
2414
|
+
expected: "string",
|
|
2415
|
+
constraint: "max",
|
|
2416
|
+
limit: shape.max,
|
|
2417
|
+
received: preview(parsed)
|
|
2418
|
+
});
|
|
2419
|
+
if (shape.pattern !== void 0 && !shape.pattern.test(parsed)) faults.push({
|
|
2420
|
+
reason: "constraint",
|
|
2421
|
+
path,
|
|
2422
|
+
expected: "string",
|
|
2423
|
+
constraint: "pattern",
|
|
2424
|
+
limit: shape.pattern.source,
|
|
2425
|
+
received: preview(parsed)
|
|
2426
|
+
});
|
|
2427
|
+
return faults;
|
|
2428
|
+
}
|
|
2429
|
+
case "number": {
|
|
2430
|
+
const kind = shape.integer === true ? "integer" : "number";
|
|
2431
|
+
const parsed = parseNumber(value);
|
|
2432
|
+
if (parsed === void 0) return [{
|
|
2433
|
+
reason: "type",
|
|
2434
|
+
path,
|
|
2435
|
+
expected: kind,
|
|
2436
|
+
received: preview(value)
|
|
2437
|
+
}];
|
|
2438
|
+
const faults = [];
|
|
2439
|
+
if (shape.integer === true && !Number.isInteger(parsed)) faults.push({
|
|
2440
|
+
reason: "constraint",
|
|
2441
|
+
path,
|
|
2442
|
+
expected: kind,
|
|
2443
|
+
constraint: "integer",
|
|
2444
|
+
received: preview(parsed)
|
|
2445
|
+
});
|
|
2446
|
+
if (shape.min !== void 0 && parsed < shape.min) faults.push({
|
|
2447
|
+
reason: "constraint",
|
|
2448
|
+
path,
|
|
2449
|
+
expected: kind,
|
|
2450
|
+
constraint: "min",
|
|
2451
|
+
limit: shape.min,
|
|
2452
|
+
received: preview(parsed)
|
|
2453
|
+
});
|
|
2454
|
+
if (shape.max !== void 0 && parsed > shape.max) faults.push({
|
|
2455
|
+
reason: "constraint",
|
|
2456
|
+
path,
|
|
2457
|
+
expected: kind,
|
|
2458
|
+
constraint: "max",
|
|
2459
|
+
limit: shape.max,
|
|
2460
|
+
received: preview(parsed)
|
|
2461
|
+
});
|
|
2462
|
+
return faults;
|
|
2463
|
+
}
|
|
2464
|
+
case "boolean": return parseBoolean(value) === void 0 ? [{
|
|
2465
|
+
reason: "type",
|
|
2466
|
+
path,
|
|
2467
|
+
expected: "boolean",
|
|
2468
|
+
received: preview(value)
|
|
2469
|
+
}] : [];
|
|
2470
|
+
case "null": return value === null ? [] : [{
|
|
2471
|
+
reason: "type",
|
|
2472
|
+
path,
|
|
2473
|
+
expected: "null",
|
|
2474
|
+
received: preview(value)
|
|
2475
|
+
}];
|
|
2476
|
+
case "json": return isJSONValue(value) ? [] : [{
|
|
2477
|
+
reason: "type",
|
|
2478
|
+
path,
|
|
2479
|
+
expected: "json",
|
|
2480
|
+
received: preview(value)
|
|
2481
|
+
}];
|
|
2482
|
+
case "literal": {
|
|
2483
|
+
const allowed = new Set(shape.values);
|
|
2484
|
+
return allowed.has(value) || isString(value) && allowed.has(value.trim()) ? [] : [{
|
|
2485
|
+
reason: "type",
|
|
2486
|
+
path,
|
|
2487
|
+
expected: "literal",
|
|
2488
|
+
received: preview(value)
|
|
2489
|
+
}];
|
|
2490
|
+
}
|
|
2491
|
+
case "array": {
|
|
2492
|
+
if (!isArray(value)) return [{
|
|
2493
|
+
reason: "type",
|
|
2494
|
+
path,
|
|
2495
|
+
expected: "array",
|
|
2496
|
+
received: preview(value)
|
|
2497
|
+
}];
|
|
2498
|
+
const faults = [];
|
|
2499
|
+
if (!attempt(() => {
|
|
2500
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
2501
|
+
if (faults.length >= 64) return;
|
|
2502
|
+
faults.push(...compileReporter(shape.items, value[index], [...path, String(index)]));
|
|
2503
|
+
}
|
|
2504
|
+
}).success) return [{
|
|
2505
|
+
reason: "type",
|
|
2506
|
+
path,
|
|
2507
|
+
expected: "array",
|
|
2508
|
+
received: preview(value)
|
|
2509
|
+
}];
|
|
2510
|
+
if (shape.min !== void 0 && value.length < shape.min) faults.push({
|
|
2511
|
+
reason: "constraint",
|
|
2512
|
+
path,
|
|
2513
|
+
expected: "array",
|
|
2514
|
+
constraint: "min",
|
|
2515
|
+
limit: shape.min,
|
|
2516
|
+
received: String(value.length)
|
|
2517
|
+
});
|
|
2518
|
+
if (shape.max !== void 0 && value.length > shape.max) faults.push({
|
|
2519
|
+
reason: "constraint",
|
|
2520
|
+
path,
|
|
2521
|
+
expected: "array",
|
|
2522
|
+
constraint: "max",
|
|
2523
|
+
limit: shape.max,
|
|
2524
|
+
received: String(value.length)
|
|
2525
|
+
});
|
|
2526
|
+
return faults.length > 64 ? faults.slice(0, 64) : faults;
|
|
2527
|
+
}
|
|
2528
|
+
case "object": {
|
|
2529
|
+
if (!isRecord(value)) return [{
|
|
2530
|
+
reason: "type",
|
|
2531
|
+
path,
|
|
2532
|
+
expected: "object",
|
|
2533
|
+
received: preview(value)
|
|
2534
|
+
}];
|
|
2535
|
+
const record = value;
|
|
2536
|
+
const faults = [];
|
|
2537
|
+
const known = /* @__PURE__ */ new Set();
|
|
2538
|
+
if (!attempt(() => {
|
|
2539
|
+
for (const key of Object.keys(shape.properties)) {
|
|
2540
|
+
if (faults.length >= 64) return;
|
|
2541
|
+
const child = shape.properties[key];
|
|
2542
|
+
if (child === void 0) continue;
|
|
2543
|
+
known.add(key);
|
|
2544
|
+
const optional = child.type === "optional";
|
|
2545
|
+
const inner = optional ? child.inner : child;
|
|
2546
|
+
const raw = record[key];
|
|
2547
|
+
if (raw === void 0) {
|
|
2548
|
+
if (!optional) faults.push({
|
|
2549
|
+
reason: "missing",
|
|
2550
|
+
path: [...path, key],
|
|
2551
|
+
expected: shapeToKind(inner)
|
|
2552
|
+
});
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
faults.push(...compileReporter(inner, raw, [...path, key]));
|
|
2556
|
+
}
|
|
2557
|
+
const extra = shape.additionalProperties;
|
|
2558
|
+
if (extra !== void 0 && extra !== true && extra !== false) for (const key of Object.keys(record)) {
|
|
2559
|
+
if (faults.length >= 64) return;
|
|
2560
|
+
if (known.has(key)) continue;
|
|
2561
|
+
faults.push(...compileReporter(extra, record[key], [...path, key]));
|
|
2562
|
+
}
|
|
2563
|
+
}).success) return [{
|
|
2564
|
+
reason: "type",
|
|
2565
|
+
path,
|
|
2566
|
+
expected: "object",
|
|
2567
|
+
received: preview(value)
|
|
2568
|
+
}];
|
|
2569
|
+
return faults.length > 64 ? faults.slice(0, 64) : faults;
|
|
2570
|
+
}
|
|
2571
|
+
case "union": {
|
|
2572
|
+
const perVariant = shape.variants.map((variant) => compileReporter(variant, value, path));
|
|
2573
|
+
let bestIndex = 0;
|
|
2574
|
+
for (let index = 1; index < perVariant.length; index += 1) {
|
|
2575
|
+
const current = perVariant[index];
|
|
2576
|
+
const best = perVariant[bestIndex];
|
|
2577
|
+
if (current !== void 0 && best !== void 0 && current.length < best.length) bestIndex = index;
|
|
2578
|
+
}
|
|
2579
|
+
const closest = perVariant[bestIndex] ?? [];
|
|
2580
|
+
if (shape.mode === "oneOf") {
|
|
2581
|
+
let matched = 0;
|
|
2582
|
+
for (const variant of shape.variants) if (compileGuard(variant)(value)) matched += 1;
|
|
2583
|
+
if (matched === 1) return [];
|
|
2584
|
+
if (matched === 0) return [{
|
|
2585
|
+
reason: "oneOf",
|
|
2586
|
+
path,
|
|
2587
|
+
matched: 0
|
|
2588
|
+
}, ...closest].slice(0, 64);
|
|
2589
|
+
return [{
|
|
2590
|
+
reason: "oneOf",
|
|
2591
|
+
path,
|
|
2592
|
+
matched
|
|
2593
|
+
}];
|
|
2594
|
+
}
|
|
2595
|
+
if (perVariant.some((variantFaults) => variantFaults.length === 0)) return [];
|
|
2596
|
+
return [{
|
|
2597
|
+
reason: "variant",
|
|
2598
|
+
path,
|
|
2599
|
+
variants: shape.variants.length
|
|
2600
|
+
}, ...closest].slice(0, 64);
|
|
2601
|
+
}
|
|
2602
|
+
case "optional": return value === void 0 ? [] : compileReporter(shape.inner, value, path);
|
|
2603
|
+
case "nullable": return value === null ? [] : compileReporter(shape.inner, value, path);
|
|
2604
|
+
case "raw": return [];
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2260
2607
|
function createContract(shape) {
|
|
2261
2608
|
validateShape(shape);
|
|
2262
2609
|
const schema = compileSchema(shape);
|
|
@@ -2268,6 +2615,9 @@ function createContract(shape) {
|
|
|
2268
2615
|
parse(value) {
|
|
2269
2616
|
return parser(value);
|
|
2270
2617
|
},
|
|
2618
|
+
explain(value) {
|
|
2619
|
+
return compileReporter(shape, value);
|
|
2620
|
+
},
|
|
2271
2621
|
generate(random) {
|
|
2272
2622
|
return compileGenerator(shape, random);
|
|
2273
2623
|
}
|
|
@@ -2599,7 +2949,9 @@ function rawShape(schema) {
|
|
|
2599
2949
|
};
|
|
2600
2950
|
}
|
|
2601
2951
|
//#endregion
|
|
2952
|
+
exports.FAULT_LIMIT = FAULT_LIMIT;
|
|
2602
2953
|
exports.JSON_SCHEMA_TYPES = JSON_SCHEMA_TYPES;
|
|
2954
|
+
exports.PREVIEW_LIMIT = PREVIEW_LIMIT;
|
|
2603
2955
|
exports.andOf = andOf;
|
|
2604
2956
|
exports.arrayOf = arrayOf;
|
|
2605
2957
|
exports.arrayShape = arrayShape;
|
|
@@ -2609,6 +2961,7 @@ exports.boundsOf = boundsOf;
|
|
|
2609
2961
|
exports.compileGenerator = compileGenerator;
|
|
2610
2962
|
exports.compileGuard = compileGuard;
|
|
2611
2963
|
exports.compileParser = compileParser;
|
|
2964
|
+
exports.compileReporter = compileReporter;
|
|
2612
2965
|
exports.compileSchema = compileSchema;
|
|
2613
2966
|
exports.complementOf = complementOf;
|
|
2614
2967
|
exports.createContract = createContract;
|
|
@@ -2723,6 +3076,7 @@ exports.parseRecordField = parseRecordField;
|
|
|
2723
3076
|
exports.parseString = parseString;
|
|
2724
3077
|
exports.parseStringField = parseStringField;
|
|
2725
3078
|
exports.pickOf = pickOf;
|
|
3079
|
+
exports.preview = preview;
|
|
2726
3080
|
exports.rawShape = rawShape;
|
|
2727
3081
|
exports.recordOf = recordOf;
|
|
2728
3082
|
exports.recordShape = recordShape;
|
|
@@ -2730,6 +3084,7 @@ exports.resolveField = resolveField;
|
|
|
2730
3084
|
exports.schemaToParameters = schemaToParameters;
|
|
2731
3085
|
exports.seededRandom = seededRandom;
|
|
2732
3086
|
exports.setOf = setOf;
|
|
3087
|
+
exports.shapeToKind = shapeToKind;
|
|
2733
3088
|
exports.stringOf = stringOf;
|
|
2734
3089
|
exports.stringShape = stringShape;
|
|
2735
3090
|
exports.transformOf = transformOf;
|