ag-common 0.0.861 → 0.0.863

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.
@@ -13,3 +13,4 @@ export * from './ssm';
13
13
  export * from './ssmInfra';
14
14
  export * from './sts';
15
15
  export * from './validations';
16
+ export * from './zod';
@@ -29,3 +29,4 @@ __exportStar(require("./ssm"), exports);
29
29
  __exportStar(require("./ssmInfra"), exports);
30
30
  __exportStar(require("./sts"), exports);
31
31
  __exportStar(require("./validations"), exports);
32
+ __exportStar(require("./zod"), exports);
@@ -0,0 +1,17 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Convert a Zod object schema to its code representation for use in LLM prompts.
4
+ * Outputs actual Zod code like: z.object({ field: z.string().nullable().describe("...") })
5
+ *
6
+ * @example
7
+ * const schema = z.object({
8
+ * name: z.string().describe("The user's name"),
9
+ * age: z.number().optional().describe("Age in years"),
10
+ * });
11
+ * const code = zodSchemaToString(schema);
12
+ * // Returns: z.object({
13
+ * // name: z.string().describe("The user's name"),
14
+ * // age: z.number().optional().describe("Age in years")
15
+ * // })
16
+ */
17
+ export declare function zodSchemaToString(schema: z.ZodObject<z.ZodRawShape>): string;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodSchemaToString = zodSchemaToString;
4
+ const zod_1 = require("zod");
5
+ /**
6
+ * Convert a Zod object schema to its code representation for use in LLM prompts.
7
+ * Outputs actual Zod code like: z.object({ field: z.string().nullable().describe("...") })
8
+ *
9
+ * @example
10
+ * const schema = z.object({
11
+ * name: z.string().describe("The user's name"),
12
+ * age: z.number().optional().describe("Age in years"),
13
+ * });
14
+ * const code = zodSchemaToString(schema);
15
+ * // Returns: z.object({
16
+ * // name: z.string().describe("The user's name"),
17
+ * // age: z.number().optional().describe("Age in years")
18
+ * // })
19
+ */
20
+ function zodSchemaToString(schema) {
21
+ const fields = [];
22
+ for (const [key, value] of Object.entries(schema.shape)) {
23
+ const zodType = value;
24
+ const typeCode = zodTypeToCode(zodType);
25
+ fields.push(` ${key}: ${typeCode}`);
26
+ }
27
+ return `z.object({\n${fields.join(',\n')}\n})`;
28
+ }
29
+ /**
30
+ * Recursively convert a Zod type to its code representation
31
+ */
32
+ function zodTypeToCode(zodType) {
33
+ const description = zodType.description;
34
+ const describeChain = description
35
+ ? `.describe(${JSON.stringify(description)})`
36
+ : '';
37
+ // Handle wrapper types first (they wrap inner types)
38
+ if (zodType instanceof zod_1.z.ZodOptional) {
39
+ const inner = zodTypeToCode(zodType.unwrap()).replace(/\.describe\([^)]+\)$/, '');
40
+ return `${inner}.optional()${describeChain}`;
41
+ }
42
+ if (zodType instanceof zod_1.z.ZodNullable) {
43
+ const inner = zodTypeToCode(zodType.unwrap()).replace(/\.describe\([^)]+\)$/, '');
44
+ return `${inner}.nullable()${describeChain}`;
45
+ }
46
+ // Handle base types
47
+ if (zodType instanceof zod_1.z.ZodString) {
48
+ return `z.string()${describeChain}`;
49
+ }
50
+ if (zodType instanceof zod_1.z.ZodNumber) {
51
+ return `z.number()${describeChain}`;
52
+ }
53
+ if (zodType instanceof zod_1.z.ZodBoolean) {
54
+ return `z.boolean()${describeChain}`;
55
+ }
56
+ if (zodType instanceof zod_1.z.ZodEnum) {
57
+ const values = zodType._def.values;
58
+ const enumStr = values.map((v) => JSON.stringify(v)).join(', ');
59
+ return `z.enum([${enumStr}])${describeChain}`;
60
+ }
61
+ if (zodType instanceof zod_1.z.ZodArray) {
62
+ const innerType = zodTypeToCode(zodType.element);
63
+ return `z.array(${innerType})${describeChain}`;
64
+ }
65
+ if (zodType instanceof zod_1.z.ZodObject) {
66
+ // Nested object - recurse
67
+ return zodSchemaToString(zodType);
68
+ }
69
+ if (zodType instanceof zod_1.z.ZodLiteral) {
70
+ const value = zodType._def.value;
71
+ return `z.literal(${JSON.stringify(value)})${describeChain}`;
72
+ }
73
+ if (zodType instanceof zod_1.z.ZodUnion) {
74
+ const options = zodType._def.options;
75
+ const optionsStr = options.map((opt) => zodTypeToCode(opt)).join(', ');
76
+ return `z.union([${optionsStr}])${describeChain}`;
77
+ }
78
+ if (zodType instanceof zod_1.z.ZodRecord) {
79
+ const valueType = zodTypeToCode(zodType._def.valueType);
80
+ return `z.record(${valueType})${describeChain}`;
81
+ }
82
+ if (zodType instanceof zod_1.z.ZodTuple) {
83
+ const items = zodType._def.items;
84
+ const itemsStr = items.map((item) => zodTypeToCode(item)).join(', ');
85
+ return `z.tuple([${itemsStr}])${describeChain}`;
86
+ }
87
+ return `z.unknown()${describeChain}`;
88
+ }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.861",
2
+ "version": "0.0.863",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
@@ -73,7 +73,8 @@
73
73
  "jwks-rsa": "^3.2.0",
74
74
  "node-cache": "^5.1.2",
75
75
  "rimraf": "^6.1.2",
76
- "storybook": "^10.1.11"
76
+ "storybook": "^10.1.11",
77
+ "zod": "^3.25.76"
77
78
  },
78
79
  "files": [
79
80
  "dist/**/*",