@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
package/dist/src/core/types.d.ts
CHANGED
|
@@ -431,6 +431,51 @@ export interface ObjectShapeOptions<A extends boolean | ContractShape = boolean
|
|
|
431
431
|
export interface RecordShapeOptions {
|
|
432
432
|
readonly description?: string;
|
|
433
433
|
}
|
|
434
|
+
/** The kind of value a {@link Fault} expected — the shape-projected counterpart of a `ContractShape`'s `type`. */
|
|
435
|
+
export type FaultKind = 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'literal' | 'array' | 'object' | 'union' | 'json';
|
|
436
|
+
/** The refinement a {@link Fault} of reason `'constraint'` violates. */
|
|
437
|
+
export type FaultConstraint = 'min' | 'max' | 'pattern' | 'integer';
|
|
438
|
+
/**
|
|
439
|
+
* A single structured parse-failure diagnostic — one entry of an
|
|
440
|
+
* {@link ContractInterface.explain} report.
|
|
441
|
+
*
|
|
442
|
+
* @remarks
|
|
443
|
+
* A discriminated union on `reason`:
|
|
444
|
+
* - `'type'` — the value could not coerce to `expected` at all.
|
|
445
|
+
* - `'missing'` — a required object property was absent.
|
|
446
|
+
* - `'constraint'` — the value coerced to `expected` but violated one
|
|
447
|
+
* refinement (`min` / `max` / `pattern` / `integer`); `limit` carries the
|
|
448
|
+
* violated bound/pattern when applicable.
|
|
449
|
+
* - `'variant'` — an `anyOf`-mode union matched no variant; `variants` is the
|
|
450
|
+
* variant count, followed (in the report) by the closest variant's own faults.
|
|
451
|
+
* - `'oneOf'` — a `oneOf`-mode union matched zero or two-or-more variants;
|
|
452
|
+
* `matched` is the raw match count.
|
|
453
|
+
*/
|
|
454
|
+
export type Fault = {
|
|
455
|
+
readonly reason: 'type';
|
|
456
|
+
readonly path: FieldPath;
|
|
457
|
+
readonly expected: FaultKind;
|
|
458
|
+
readonly received: string;
|
|
459
|
+
} | {
|
|
460
|
+
readonly reason: 'missing';
|
|
461
|
+
readonly path: FieldPath;
|
|
462
|
+
readonly expected: FaultKind;
|
|
463
|
+
} | {
|
|
464
|
+
readonly reason: 'constraint';
|
|
465
|
+
readonly path: FieldPath;
|
|
466
|
+
readonly expected: FaultKind;
|
|
467
|
+
readonly constraint: FaultConstraint;
|
|
468
|
+
readonly limit?: number | string;
|
|
469
|
+
readonly received: string;
|
|
470
|
+
} | {
|
|
471
|
+
readonly reason: 'variant';
|
|
472
|
+
readonly path: FieldPath;
|
|
473
|
+
readonly variants: number;
|
|
474
|
+
} | {
|
|
475
|
+
readonly reason: 'oneOf';
|
|
476
|
+
readonly path: FieldPath;
|
|
477
|
+
readonly matched: number;
|
|
478
|
+
};
|
|
434
479
|
/** A deterministic random source returning a value in `[0, 1)`. */
|
|
435
480
|
export type RandomFunction = () => number;
|
|
436
481
|
/**
|
|
@@ -446,5 +491,18 @@ export interface ContractInterface<T> {
|
|
|
446
491
|
readonly schema: JSONSchema;
|
|
447
492
|
readonly is: Guard<T>;
|
|
448
493
|
parse(value: unknown): T | undefined;
|
|
494
|
+
/**
|
|
495
|
+
* Report every structured parse fault a value has against this contract.
|
|
496
|
+
*
|
|
497
|
+
* @remarks
|
|
498
|
+
* An empty report means the value is valid. Soundness invariant:
|
|
499
|
+
* `explain(v).length === 0` if and only if `parse(v) !== undefined` — explain
|
|
500
|
+
* mirrors `parse`'s coercion, not the stricter `is`. Faults are listed in
|
|
501
|
+
* stable pre-order (declared key/index order).
|
|
502
|
+
*
|
|
503
|
+
* @param value - The value to check
|
|
504
|
+
* @returns The faults found, empty when the value parses successfully
|
|
505
|
+
*/
|
|
506
|
+
explain(value: unknown): readonly Fault[];
|
|
449
507
|
generate(random?: RandomFunction): T;
|
|
450
508
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orkestrel/contract",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Zero-dependency contract toolkit — type guards, combinators, parsers, and a shape DSL that compiles one contract shape into a guard, parser, JSON Schema, and generator. The foundation package of the @orkestrel line.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contract",
|