@orkestrel/contract 0.0.1 → 0.0.3
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 +3 -3
- package/dist/src/core/compilers.d.ts +32 -1
- package/dist/src/core/constants.d.ts +1 -1
- package/dist/src/core/helpers.d.ts +1 -1
- package/dist/src/core/index.cjs +2655 -0
- package/dist/src/core/index.cjs.map +1 -0
- package/dist/src/core/index.d.cts +8 -0
- package/dist/src/core/index.js +592 -101
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/parsers.d.ts +82 -2
- package/dist/src/core/shapers.d.ts +40 -3
- package/dist/src/core/types.d.ts +60 -14
- package/dist/src/core/validators.d.ts +464 -52
- package/package.json +25 -15
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
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]
|
|
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
|
|
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,7 +63,14 @@ 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
|
*/
|
|
73
|
+
export declare function compileGuard<S extends ContractShape>(shape: S): Guard<Infer<S>>;
|
|
56
74
|
export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
57
75
|
/**
|
|
58
76
|
* Compile a {@link ContractShape} into an input parser.
|
|
@@ -72,7 +90,14 @@ export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
|
72
90
|
*
|
|
73
91
|
* @param shape - The shape to compile
|
|
74
92
|
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const parseUser = compileParser(objectShape({ name: stringShape() }))
|
|
97
|
+
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
98
|
+
* ```
|
|
75
99
|
*/
|
|
100
|
+
export declare function compileParser<S extends ContractShape>(shape: S): Parser<Infer<S>>;
|
|
76
101
|
export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
77
102
|
/**
|
|
78
103
|
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
@@ -92,7 +117,13 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
92
117
|
* @param shape - The shape to generate from
|
|
93
118
|
* @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
|
|
94
119
|
* @returns A value matching the shape
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
124
|
+
* ```
|
|
95
125
|
*/
|
|
126
|
+
export declare function compileGenerator<S extends ContractShape>(shape: S, random?: RandomFunction): Infer<S>;
|
|
96
127
|
export declare function compileGenerator(shape: ContractShape, random?: RandomFunction): unknown;
|
|
97
128
|
/**
|
|
98
129
|
* Compile a {@link ContractShape} into a {@link ContractInterface} — the four
|