ag-common 0.0.861 → 0.0.864

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,19 @@
1
+ import type { 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
+ * Uses _def.typeName instead of instanceof checks to work across different Zod module instances.
7
+ *
8
+ * @example
9
+ * const schema = z.object({
10
+ * name: z.string().describe("The user's name"),
11
+ * age: z.number().optional().describe("Age in years"),
12
+ * });
13
+ * const code = zodSchemaToString(schema);
14
+ * // Returns: z.object({
15
+ * // name: z.string().describe("The user's name"),
16
+ * // age: z.number().optional().describe("Age in years")
17
+ * // })
18
+ */
19
+ export declare function zodSchemaToString(schema: z.ZodObject<z.ZodRawShape>): string;
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodSchemaToString = zodSchemaToString;
4
+ /**
5
+ * Convert a Zod object schema to its code representation for use in LLM prompts.
6
+ * Outputs actual Zod code like: z.object({ field: z.string().nullable().describe("...") })
7
+ *
8
+ * Uses _def.typeName instead of instanceof checks to work across different Zod module instances.
9
+ *
10
+ * @example
11
+ * const schema = z.object({
12
+ * name: z.string().describe("The user's name"),
13
+ * age: z.number().optional().describe("Age in years"),
14
+ * });
15
+ * const code = zodSchemaToString(schema);
16
+ * // Returns: z.object({
17
+ * // name: z.string().describe("The user's name"),
18
+ * // age: z.number().optional().describe("Age in years")
19
+ * // })
20
+ */
21
+ function zodSchemaToString(schema) {
22
+ const fields = [];
23
+ for (const [key, value] of Object.entries(schema.shape)) {
24
+ const zodType = value;
25
+ const typeCode = zodTypeToCode(zodType);
26
+ fields.push(` ${key}: ${typeCode}`);
27
+ }
28
+ return `z.object({\n${fields.join(',\n')}\n})`;
29
+ }
30
+ /**
31
+ * Get the type name from a Zod type's internal definition.
32
+ * This works across different Zod module instances unlike instanceof.
33
+ */
34
+ function getTypeName(zodType) {
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ return zodType._def.typeName;
37
+ }
38
+ /**
39
+ * Recursively convert a Zod type to its code representation
40
+ */
41
+ function zodTypeToCode(zodType) {
42
+ const description = zodType.description;
43
+ const describeChain = description
44
+ ? `.describe(${JSON.stringify(description)})`
45
+ : '';
46
+ const typeName = getTypeName(zodType);
47
+ // Handle wrapper types first (they wrap inner types)
48
+ if (typeName === 'ZodOptional') {
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ const innerType = zodType._def.innerType;
51
+ const inner = zodTypeToCode(innerType).replace(/\.describe\([^)]+\)$/, '');
52
+ return `${inner}.optional()${describeChain}`;
53
+ }
54
+ if (typeName === 'ZodNullable') {
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ const innerType = zodType._def.innerType;
57
+ const inner = zodTypeToCode(innerType).replace(/\.describe\([^)]+\)$/, '');
58
+ return `${inner}.nullable()${describeChain}`;
59
+ }
60
+ // Handle base types
61
+ if (typeName === 'ZodString') {
62
+ return `z.string()${describeChain}`;
63
+ }
64
+ if (typeName === 'ZodNumber') {
65
+ return `z.number()${describeChain}`;
66
+ }
67
+ if (typeName === 'ZodBoolean') {
68
+ return `z.boolean()${describeChain}`;
69
+ }
70
+ if (typeName === 'ZodEnum') {
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ const values = zodType._def.values;
73
+ const enumStr = values.map((v) => JSON.stringify(v)).join(', ');
74
+ return `z.enum([${enumStr}])${describeChain}`;
75
+ }
76
+ if (typeName === 'ZodArray') {
77
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
+ const elementType = zodType._def.type;
79
+ const innerType = zodTypeToCode(elementType);
80
+ return `z.array(${innerType})${describeChain}`;
81
+ }
82
+ if (typeName === 'ZodObject') {
83
+ // Nested object - recurse
84
+ return zodSchemaToString(zodType);
85
+ }
86
+ if (typeName === 'ZodLiteral') {
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
+ const value = zodType._def.value;
89
+ return `z.literal(${JSON.stringify(value)})${describeChain}`;
90
+ }
91
+ if (typeName === 'ZodUnion') {
92
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
+ const options = zodType._def.options;
94
+ const optionsStr = options.map((opt) => zodTypeToCode(opt)).join(', ');
95
+ return `z.union([${optionsStr}])${describeChain}`;
96
+ }
97
+ if (typeName === 'ZodRecord') {
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ const valueType = zodType._def.valueType;
100
+ return `z.record(${zodTypeToCode(valueType)})${describeChain}`;
101
+ }
102
+ if (typeName === 'ZodTuple') {
103
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
104
+ const items = zodType._def.items;
105
+ const itemsStr = items.map((item) => zodTypeToCode(item)).join(', ');
106
+ return `z.tuple([${itemsStr}])${describeChain}`;
107
+ }
108
+ throw new Error(`Unsupported Zod type: ${typeName}`);
109
+ }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.861",
2
+ "version": "0.0.864",
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/**/*",