@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,4 +1,4 @@
1
- import type { AnyConstructor, FromGuards, Guard, GuardsShape, GuardType, IntersectionFromGuards, OptionalFromGuards, TupleFromGuards } from './types.js';
1
+ import { AnyConstructor, 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
  *
@@ -63,7 +63,7 @@ export declare function instanceOf<C>(ctor: C): Guard<InstanceType<C & AnyConstr
63
63
  * isDirection('left') // false
64
64
  * ```
65
65
  */
66
- export declare function enumOf<E extends Record<string, string | number>>(enumeration: E): Guard<E[keyof E]>;
66
+ export declare function enumOf<const E extends Record<string, string | number>>(enumeration: E): Guard<E[keyof E]>;
67
67
  /**
68
68
  * Build a guard that accepts `Set` instances whose every element satisfies
69
69
  * `elementGuard`.
@@ -93,7 +93,7 @@ export declare function mapOf(keyPredicate: (value: unknown) => boolean, valuePr
93
93
  export declare function recordOf<S extends GuardsShape>(shape: S): Guard<FromGuards<S>>;
94
94
  export declare function recordOf<S extends GuardsShape, K extends ReadonlyArray<keyof S & string>>(shape: S, optional: K): Guard<OptionalFromGuards<S, K>>;
95
95
  export declare function recordOf<S extends GuardsShape>(shape: S, optional: true): Guard<Readonly<{
96
- [P in keyof S]: FromGuards<S>[P] | undefined;
96
+ [P in keyof S]?: FromGuards<S>[P];
97
97
  }>>;
98
98
  /**
99
99
  * Build a guard that accepts any iterable whose every element satisfies
@@ -1,4 +1,4 @@
1
- import type { ContractInterface, ContractShape, Guard, Infer, JSONSchema, Parser, RandomFunction } from './types.js';
1
+ import { ContractInterface, ContractShape, 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.
@@ -27,6 +27,12 @@ import type { ContractInterface, ContractShape, Guard, Infer, JSONSchema, Parser
27
27
  *
28
28
  * @param shape - The shape to validate
29
29
  * @throws {Error} When the shape is malformed
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * validateShape(stringShape({ min: 1, max: 10 })) // does not throw
34
+ * validateShape(stringShape({ min: 10, max: 1 })) // throws
35
+ * ```
30
36
  */
31
37
  export declare function validateShape(shape: ContractShape): void;
32
38
  /**
@@ -39,6 +45,11 @@ export declare function validateShape(shape: ContractShape): void;
39
45
  *
40
46
  * @param shape - The shape to compile
41
47
  * @returns The emitted JSON Schema
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * compileSchema(stringShape({ min: 1 })) // { type: 'string', minLength: 1 }
52
+ * ```
42
53
  */
43
54
  export declare function compileSchema(shape: ContractShape): JSONSchema;
44
55
  /**
@@ -52,6 +63,12 @@ export declare function compileSchema(shape: ContractShape): JSONSchema;
52
63
  *
53
64
  * @param shape - The shape to compile
54
65
  * @returns A guard narrowing to the shape's inferred type
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const isUser = compileGuard(objectShape({ name: stringShape() }))
70
+ * isUser({ name: 'Ada' }) // true
71
+ * ```
55
72
  */
56
73
  export declare function compileGuard(shape: ContractShape): Guard<unknown>;
57
74
  /**
@@ -72,6 +89,12 @@ export declare function compileGuard(shape: ContractShape): Guard<unknown>;
72
89
  *
73
90
  * @param shape - The shape to compile
74
91
  * @returns A parser yielding the shape's inferred type or `undefined`
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const parseUser = compileParser(objectShape({ name: stringShape() }))
96
+ * parseUser({ name: 'Ada' }) // { name: 'Ada' }
97
+ * ```
75
98
  */
76
99
  export declare function compileParser(shape: ContractShape): Parser<unknown>;
77
100
  /**
@@ -92,6 +115,11 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
92
115
  * @param shape - The shape to generate from
93
116
  * @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
94
117
  * @returns A value matching the shape
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
122
+ * ```
95
123
  */
96
124
  export declare function compileGenerator(shape: ContractShape, random?: RandomFunction): unknown;
97
125
  /**
@@ -1,4 +1,4 @@
1
- import type { JSONSchemaType } from './types.js';
1
+ import { JSONSchemaType } from './types.js';
2
2
  /**
3
3
  * The seven standard JSON Schema `type` names, frozen.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { FieldPath, JSONSchema, RandomFunction, Result } from './types.js';
1
+ import { 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.