@barnum/barnum 0.0.0-main-3c07d795 → 0.0.0-main-18fb14ce

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,10 +1,36 @@
1
1
  import { type Action, type MergeTuple, type Option as OptionT, type Pipeable, type Result as ResultT, type TaggedUnion, type TypedAction } from "./ast.js";
2
+ import { z } from "zod";
2
3
  /**
3
4
  * Typed combinators for structural data transformations.
4
5
  *
5
6
  * All builtins emit `{ kind: "Builtin", builtin: { kind: ... } }` handler
6
7
  * kinds. The Rust scheduler executes them inline (no subprocess).
7
8
  */
9
+ /**
10
+ * Reverse of VoidToNull: maps `null` back to `void` in the def so that
11
+ * `taggedUnionSchema({ Clean: z.null() })` produces the same phantom __def
12
+ * as `TaggedUnion<{ Clean: void }>`.
13
+ */
14
+ type NullToVoid<TDef> = {
15
+ [K in keyof TDef]: TDef[K] extends null ? void : TDef[K];
16
+ };
17
+ /**
18
+ * Build a Zod schema for a `TaggedUnion<TDef>` — a discriminated union of
19
+ * `{ kind: K; value: V }` objects.
20
+ *
21
+ * Each key in `cases` becomes a variant. The value Zod schema validates the
22
+ * `value` field. Use `z.null()` for void variants.
23
+ *
24
+ * ```ts
25
+ * const schema = taggedUnionSchema({
26
+ * HasErrors: z.array(TypeErrorValidator),
27
+ * Clean: z.null(),
28
+ * });
29
+ * ```
30
+ */
31
+ export declare function taggedUnionSchema<TDef extends Record<string, z.ZodTypeAny>>(cases: TDef): z.ZodType<TaggedUnion<NullToVoid<{
32
+ [K in keyof TDef & string]: z.infer<TDef[K]>;
33
+ }>>>;
8
34
  export declare function constant<TValue>(value: TValue): TypedAction<any, TValue>;
9
35
  export declare function identity<TValue = any>(): TypedAction<TValue, TValue>;
10
36
  export declare const drop: TypedAction<any, never>;
@@ -171,6 +197,15 @@ export declare const Option: {
171
197
  * Desugars to: `branch({ Some: pipe(drop(), constant(false)), None: pipe(drop(), constant(true)) })`
172
198
  */
173
199
  readonly isNone: <T>() => TypedAction<OptionT<T>, boolean>;
200
+ /**
201
+ * Build a Zod schema for `Option<T>`.
202
+ *
203
+ * ```ts
204
+ * const schema = Option.schema(z.string());
205
+ * // validates: { kind: "Some", value: "hello" } or { kind: "None", value: null }
206
+ * ```
207
+ */
208
+ readonly schema: <TValue>(valueSchema: z.ZodType<TValue>) => z.ZodType<OptionT<TValue>>;
174
209
  };
175
210
  /**
176
211
  * Result namespace. All combinators produce TypedAction AST nodes that
@@ -265,4 +300,14 @@ export declare const Result: {
265
300
  * Test if the value is Err. `Result<TValue, TError> → boolean`
266
301
  */
267
302
  readonly isErr: <TValue, TError>() => TypedAction<ResultT<TValue, TError>, boolean>;
303
+ /**
304
+ * Build a Zod schema for `Result<TValue, TError>`.
305
+ *
306
+ * ```ts
307
+ * const schema = Result.schema(z.string(), z.number());
308
+ * // validates: { kind: "Ok", value: "hello" } or { kind: "Err", value: 42 }
309
+ * ```
310
+ */
311
+ readonly schema: <TValue, TError>(okSchema: z.ZodType<TValue>, errSchema: z.ZodType<TError>) => z.ZodType<ResultT<TValue, TError>>;
268
312
  };
313
+ export {};
package/dist/builtins.js CHANGED
@@ -1,11 +1,31 @@
1
1
  import { typedAction, } from "./ast.js";
2
2
  import { chain } from "./chain.js";
3
+ import { z } from "zod";
3
4
  /**
4
- * Typed combinators for structural data transformations.
5
+ * Build a Zod schema for a `TaggedUnion<TDef>` — a discriminated union of
6
+ * `{ kind: K; value: V }` objects.
5
7
  *
6
- * All builtins emit `{ kind: "Builtin", builtin: { kind: ... } }` handler
7
- * kinds. The Rust scheduler executes them inline (no subprocess).
8
+ * Each key in `cases` becomes a variant. The value Zod schema validates the
9
+ * `value` field. Use `z.null()` for void variants.
10
+ *
11
+ * ```ts
12
+ * const schema = taggedUnionSchema({
13
+ * HasErrors: z.array(TypeErrorValidator),
14
+ * Clean: z.null(),
15
+ * });
16
+ * ```
8
17
  */
18
+ export function taggedUnionSchema(cases) {
19
+ const variants = Object.entries(cases).map(([kind, valueSchema]) => z.object({ kind: z.literal(kind), value: valueSchema }));
20
+ if (variants.length < 2) {
21
+ throw new Error("taggedUnionSchema requires at least 2 variants");
22
+ }
23
+ return z.discriminatedUnion("kind", [
24
+ variants[0],
25
+ variants[1],
26
+ ...variants.slice(2),
27
+ ]);
28
+ }
9
29
  // ---------------------------------------------------------------------------
10
30
  // Constant — produce a fixed value (takes no pipeline input)
11
31
  // ---------------------------------------------------------------------------
@@ -444,6 +464,20 @@ export const Option = {
444
464
  };
445
465
  return typedAction(optionBranch({ kind: "Chain", first: DROP, rest: constFalse }, { kind: "Chain", first: DROP, rest: constTrue }));
446
466
  },
467
+ /**
468
+ * Build a Zod schema for `Option<T>`.
469
+ *
470
+ * ```ts
471
+ * const schema = Option.schema(z.string());
472
+ * // validates: { kind: "Some", value: "hello" } or { kind: "None", value: null }
473
+ * ```
474
+ */
475
+ schema(valueSchema) {
476
+ return z.discriminatedUnion("kind", [
477
+ z.object({ kind: z.literal("Some"), value: valueSchema }),
478
+ z.object({ kind: z.literal("None"), value: z.null() }),
479
+ ]);
480
+ },
447
481
  };
448
482
  // ---------------------------------------------------------------------------
449
483
  // Result namespace — combinators for Result<TValue, TError> tagged unions
@@ -623,4 +657,18 @@ export const Result = {
623
657
  };
624
658
  return typedAction(resultBranch({ kind: "Chain", first: DROP, rest: constFalse }, { kind: "Chain", first: DROP, rest: constTrue }));
625
659
  },
660
+ /**
661
+ * Build a Zod schema for `Result<TValue, TError>`.
662
+ *
663
+ * ```ts
664
+ * const schema = Result.schema(z.string(), z.number());
665
+ * // validates: { kind: "Ok", value: "hello" } or { kind: "Err", value: 42 }
666
+ * ```
667
+ */
668
+ schema(okSchema, errSchema) {
669
+ return z.discriminatedUnion("kind", [
670
+ z.object({ kind: z.literal("Ok"), value: okSchema }),
671
+ z.object({ kind: z.literal("Err"), value: errSchema }),
672
+ ]);
673
+ },
626
674
  };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { TaggedUnion, OptionDef, ResultDef } from "./ast.js";
2
2
  export * from "./ast.js";
3
- export { constant, identity, drop, tag, merge, flatten, getField, getIndex, pick, dropResult, withResource, tap, range, splitFirst, splitLast, wrapInField, Option, Result, } from "./builtins.js";
3
+ export { constant, identity, drop, tag, merge, flatten, getField, getIndex, pick, dropResult, withResource, tap, range, splitFirst, splitLast, wrapInField, taggedUnionSchema, Option, Result, } from "./builtins.js";
4
4
  export * from "./handler.js";
5
5
  export { runPipeline, type RunPipelineOptions, type LogLevel } from "./run.js";
6
6
  export { zodToCheckedJsonSchema } from "./schema.js";
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./ast.js";
2
- export { constant, identity, drop, tag, merge, flatten, getField, getIndex, pick, dropResult, withResource, tap, range, splitFirst, splitLast, wrapInField, Option, Result, } from "./builtins.js";
2
+ export { constant, identity, drop, tag, merge, flatten, getField, getIndex, pick, dropResult, withResource, tap, range, splitFirst, splitLast, wrapInField, taggedUnionSchema, Option, Result, } from "./builtins.js";
3
3
  export * from "./handler.js";
4
4
  export { runPipeline } from "./run.js";
5
5
  export { zodToCheckedJsonSchema } from "./schema.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barnum/barnum",
3
- "version": "0.0.0-main-3c07d795",
3
+ "version": "0.0.0-main-18fb14ce",
4
4
  "description": "Barnum workflow engine",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/builtins.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  typedAction,
10
10
  } from "./ast.js";
11
11
  import { chain } from "./chain.js";
12
+ import { z } from "zod";
12
13
 
13
14
  /**
14
15
  * Typed combinators for structural data transformations.
@@ -17,6 +18,53 @@ import { chain } from "./chain.js";
17
18
  * kinds. The Rust scheduler executes them inline (no subprocess).
18
19
  */
19
20
 
21
+ // ---------------------------------------------------------------------------
22
+ // TaggedUnion Zod schema constructor
23
+ // ---------------------------------------------------------------------------
24
+
25
+ /**
26
+ * Reverse of VoidToNull: maps `null` back to `void` in the def so that
27
+ * `taggedUnionSchema({ Clean: z.null() })` produces the same phantom __def
28
+ * as `TaggedUnion<{ Clean: void }>`.
29
+ */
30
+ type NullToVoid<TDef> = {
31
+ [K in keyof TDef]: TDef[K] extends null ? void : TDef[K];
32
+ };
33
+
34
+ /**
35
+ * Build a Zod schema for a `TaggedUnion<TDef>` — a discriminated union of
36
+ * `{ kind: K; value: V }` objects.
37
+ *
38
+ * Each key in `cases` becomes a variant. The value Zod schema validates the
39
+ * `value` field. Use `z.null()` for void variants.
40
+ *
41
+ * ```ts
42
+ * const schema = taggedUnionSchema({
43
+ * HasErrors: z.array(TypeErrorValidator),
44
+ * Clean: z.null(),
45
+ * });
46
+ * ```
47
+ */
48
+ export function taggedUnionSchema<TDef extends Record<string, z.ZodTypeAny>>(
49
+ cases: TDef,
50
+ ): z.ZodType<
51
+ TaggedUnion<NullToVoid<{ [K in keyof TDef & string]: z.infer<TDef[K]> }>>
52
+ > {
53
+ const variants = Object.entries(cases).map(([kind, valueSchema]) =>
54
+ z.object({ kind: z.literal(kind), value: valueSchema }),
55
+ );
56
+ if (variants.length < 2) {
57
+ throw new Error("taggedUnionSchema requires at least 2 variants");
58
+ }
59
+ return z.discriminatedUnion("kind", [
60
+ variants[0],
61
+ variants[1],
62
+ ...variants.slice(2),
63
+ ]) as z.ZodType<
64
+ TaggedUnion<NullToVoid<{ [K in keyof TDef & string]: z.infer<TDef[K]> }>>
65
+ >;
66
+ }
67
+
20
68
  // ---------------------------------------------------------------------------
21
69
  // Constant — produce a fixed value (takes no pipeline input)
22
70
  // ---------------------------------------------------------------------------
@@ -576,6 +624,21 @@ export const Option = {
576
624
  ),
577
625
  );
578
626
  },
627
+
628
+ /**
629
+ * Build a Zod schema for `Option<T>`.
630
+ *
631
+ * ```ts
632
+ * const schema = Option.schema(z.string());
633
+ * // validates: { kind: "Some", value: "hello" } or { kind: "None", value: null }
634
+ * ```
635
+ */
636
+ schema<TValue>(valueSchema: z.ZodType<TValue>): z.ZodType<OptionT<TValue>> {
637
+ return z.discriminatedUnion("kind", [
638
+ z.object({ kind: z.literal("Some"), value: valueSchema }),
639
+ z.object({ kind: z.literal("None"), value: z.null() }),
640
+ ]) as z.ZodType<OptionT<TValue>>;
641
+ },
579
642
  } as const;
580
643
 
581
644
  // ---------------------------------------------------------------------------
@@ -828,4 +891,22 @@ export const Result = {
828
891
  ),
829
892
  );
830
893
  },
894
+
895
+ /**
896
+ * Build a Zod schema for `Result<TValue, TError>`.
897
+ *
898
+ * ```ts
899
+ * const schema = Result.schema(z.string(), z.number());
900
+ * // validates: { kind: "Ok", value: "hello" } or { kind: "Err", value: 42 }
901
+ * ```
902
+ */
903
+ schema<TValue, TError>(
904
+ okSchema: z.ZodType<TValue>,
905
+ errSchema: z.ZodType<TError>,
906
+ ): z.ZodType<ResultT<TValue, TError>> {
907
+ return z.discriminatedUnion("kind", [
908
+ z.object({ kind: z.literal("Ok"), value: okSchema }),
909
+ z.object({ kind: z.literal("Err"), value: errSchema }),
910
+ ]) as z.ZodType<ResultT<TValue, TError>>;
911
+ },
831
912
  } as const;
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ export {
18
18
  splitFirst,
19
19
  splitLast,
20
20
  wrapInField,
21
+ taggedUnionSchema,
21
22
  Option,
22
23
  Result,
23
24
  } from "./builtins.js";