@vibe-agent-toolkit/utils 0.1.7 → 0.1.9
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/README.md +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/zod-introspection.d.ts +146 -0
- package/dist/zod-introspection.d.ts.map +1 -0
- package/dist/zod-introspection.js +200 -0
- package/dist/zod-introspection.js.map +1 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -29,6 +29,50 @@ import {
|
|
|
29
29
|
|
|
30
30
|
## Available Utilities
|
|
31
31
|
|
|
32
|
+
### Zod Type Introspection (Version-Agnostic)
|
|
33
|
+
|
|
34
|
+
**Purpose**: Runtime Zod type detection that works across Zod v3 and v4.
|
|
35
|
+
|
|
36
|
+
Uses duck typing via `_def.typeName` instead of `instanceof` checks, which fail when library and user Zod versions differ. Essential for libraries that accept user-provided Zod schemas.
|
|
37
|
+
|
|
38
|
+
**Quick Example**:
|
|
39
|
+
```typescript
|
|
40
|
+
import { getZodTypeName, isZodType, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
41
|
+
import { z } from 'zod';
|
|
42
|
+
|
|
43
|
+
const schema = z.string().optional();
|
|
44
|
+
|
|
45
|
+
// Get type name (works with Zod v3 or v4)
|
|
46
|
+
const typeName = getZodTypeName(schema);
|
|
47
|
+
console.log(typeName); // 'ZodOptional'
|
|
48
|
+
|
|
49
|
+
// Check if matches expected type
|
|
50
|
+
if (isZodType(schema, ZodTypeNames.STRING)) {
|
|
51
|
+
console.log('String type!');
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Available Functions**:
|
|
56
|
+
- `getZodTypeName(zodType)` - Extract `_def.typeName` safely
|
|
57
|
+
- `isZodType(zodType, typeName)` - Check if type matches expected name
|
|
58
|
+
- `unwrapZodType(zodType)` - Unwrap optional/nullable to get inner type
|
|
59
|
+
- `isZodOptional(zodType)` - Check if type is optional
|
|
60
|
+
- `isZodNullable(zodType)` - Check if type is nullable
|
|
61
|
+
|
|
62
|
+
**Available Constants** (`ZodTypeNames`):
|
|
63
|
+
```typescript
|
|
64
|
+
STRING, NUMBER, BOOLEAN, ARRAY, OBJECT, ENUM,
|
|
65
|
+
OPTIONAL, NULLABLE, DATE, BIGINT, NATIVENUM,
|
|
66
|
+
UNION, INTERSECTION, TUPLE, RECORD, MAP, SET,
|
|
67
|
+
FUNCTION, LAZY, PROMISE, and more...
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**See**: [docs/zod-compatibility.md](../../docs/zod-compatibility.md) for complete guide
|
|
71
|
+
|
|
72
|
+
**Peer Dependency**: Requires `zod ^3.25.0 || ^4.0.0`
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
32
76
|
### Process Spawning
|
|
33
77
|
- `safeExecSync()` - Cross-platform secure command execution without shell
|
|
34
78
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,cAAc,gBAAgB,CAAC;AAG/B,cAAc,iBAAiB,CAAC;AAGhC,cAAc,eAAe,CAAC;AAG9B,cAAc,mBAAmB,CAAC;AAGlC,cAAc,wBAAwB,CAAC;AAGvC,cAAc,gBAAgB,CAAC;AAG/B,cAAc,kBAAkB,CAAC;AAGjC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,cAAc,gBAAgB,CAAC;AAG/B,cAAc,iBAAiB,CAAC;AAGhC,cAAc,eAAe,CAAC;AAG9B,cAAc,mBAAmB,CAAC;AAGlC,cAAc,wBAAwB,CAAC;AAGvC,cAAc,gBAAgB,CAAC;AAG/B,cAAc,kBAAkB,CAAC;AAGjC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,wBAAwB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -20,4 +20,6 @@ export * from './git-utils.js';
|
|
|
20
20
|
export * from './git-tracker.js';
|
|
21
21
|
// Test helpers for isolated test output directories
|
|
22
22
|
export * from './test-helpers.js';
|
|
23
|
+
// Zod type introspection (version-agnostic)
|
|
24
|
+
export * from './zod-introspection.js';
|
|
23
25
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,8DAA8D;AAC9D,cAAc,gBAAgB,CAAC;AAE/B,gCAAgC;AAChC,cAAc,iBAAiB,CAAC;AAEhC,uBAAuB;AACvB,cAAc,eAAe,CAAC;AAE9B,wCAAwC;AACxC,cAAc,mBAAmB,CAAC;AAElC,sBAAsB;AACtB,cAAc,wBAAwB,CAAC;AAEvC,8CAA8C;AAC9C,cAAc,gBAAgB,CAAC;AAE/B,yDAAyD;AACzD,cAAc,kBAAkB,CAAC;AAEjC,oDAAoD;AACpD,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,8DAA8D;AAC9D,cAAc,gBAAgB,CAAC;AAE/B,gCAAgC;AAChC,cAAc,iBAAiB,CAAC;AAEhC,uBAAuB;AACvB,cAAc,eAAe,CAAC;AAE9B,wCAAwC;AACxC,cAAc,mBAAmB,CAAC;AAElC,sBAAsB;AACtB,cAAc,wBAAwB,CAAC;AAEvC,8CAA8C;AAC9C,cAAc,gBAAgB,CAAC;AAE/B,yDAAyD;AACzD,cAAc,kBAAkB,CAAC;AAEjC,oDAAoD;AACpD,cAAc,mBAAmB,CAAC;AAElC,4CAA4C;AAC5C,cAAc,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod type introspection utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides version-agnostic type detection using duck typing instead of
|
|
5
|
+
* instanceof checks, which fail across Zod v3/v4 boundaries.
|
|
6
|
+
*
|
|
7
|
+
* @module zod-introspection
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Zod type names from _def.typeName
|
|
11
|
+
*
|
|
12
|
+
* These internal type names are stable across Zod v3 and v4.
|
|
13
|
+
* Using these constants instead of instanceof checks ensures
|
|
14
|
+
* compatibility when user's Zod version differs from library's.
|
|
15
|
+
*
|
|
16
|
+
* @see https://github.com/colinhacks/zod/issues/2543
|
|
17
|
+
*/
|
|
18
|
+
export declare const ZodTypeNames: {
|
|
19
|
+
readonly STRING: "ZodString";
|
|
20
|
+
readonly NUMBER: "ZodNumber";
|
|
21
|
+
readonly BOOLEAN: "ZodBoolean";
|
|
22
|
+
readonly ARRAY: "ZodArray";
|
|
23
|
+
readonly OBJECT: "ZodObject";
|
|
24
|
+
readonly ENUM: "ZodEnum";
|
|
25
|
+
readonly OPTIONAL: "ZodOptional";
|
|
26
|
+
readonly NULLABLE: "ZodNullable";
|
|
27
|
+
readonly DATE: "ZodDate";
|
|
28
|
+
readonly LITERAL: "ZodLiteral";
|
|
29
|
+
readonly UNION: "ZodUnion";
|
|
30
|
+
readonly INTERSECTION: "ZodIntersection";
|
|
31
|
+
readonly TUPLE: "ZodTuple";
|
|
32
|
+
readonly RECORD: "ZodRecord";
|
|
33
|
+
readonly MAP: "ZodMap";
|
|
34
|
+
readonly SET: "ZodSet";
|
|
35
|
+
readonly FUNCTION: "ZodFunction";
|
|
36
|
+
readonly LAZY: "ZodLazy";
|
|
37
|
+
readonly PROMISE: "ZodPromise";
|
|
38
|
+
readonly BRANDED: "ZodBranded";
|
|
39
|
+
readonly PIPELINE: "ZodPipeline";
|
|
40
|
+
readonly READONLY: "ZodReadonly";
|
|
41
|
+
readonly SYMBOL: "ZodSymbol";
|
|
42
|
+
readonly UNDEFINED: "ZodUndefined";
|
|
43
|
+
readonly NULL: "ZodNull";
|
|
44
|
+
readonly ANY: "ZodAny";
|
|
45
|
+
readonly UNKNOWN: "ZodUnknown";
|
|
46
|
+
readonly NEVER: "ZodNever";
|
|
47
|
+
readonly VOID: "ZodVoid";
|
|
48
|
+
readonly BIGINT: "ZodBigInt";
|
|
49
|
+
readonly EFFECTS: "ZodEffects";
|
|
50
|
+
readonly NATIVENUM: "ZodNativeEnum";
|
|
51
|
+
readonly DISCRIMINATED_UNION: "ZodDiscriminatedUnion";
|
|
52
|
+
readonly DEFAULT: "ZodDefault";
|
|
53
|
+
readonly CATCH: "ZodCatch";
|
|
54
|
+
readonly NAN: "ZodNaN";
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Type representing valid Zod type names
|
|
58
|
+
*/
|
|
59
|
+
export type ZodTypeName = (typeof ZodTypeNames)[keyof typeof ZodTypeNames];
|
|
60
|
+
/**
|
|
61
|
+
* Get Zod type name in version-agnostic way
|
|
62
|
+
*
|
|
63
|
+
* Supports both Zod v3 and v4:
|
|
64
|
+
* - Zod v3: Uses _def.typeName (e.g., 'ZodString')
|
|
65
|
+
* - Zod v4: Uses _def.type (e.g., 'string'), converts to v3 format
|
|
66
|
+
*
|
|
67
|
+
* Uses duck typing instead of instanceof checks, which fail when
|
|
68
|
+
* user's Zod version differs from library's Zod version.
|
|
69
|
+
*
|
|
70
|
+
* @param zodType - Zod type to introspect
|
|
71
|
+
* @returns Type name string (e.g., 'ZodString') or undefined if not a Zod type
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* import { z } from 'zod';
|
|
76
|
+
* import { getZodTypeName, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
77
|
+
*
|
|
78
|
+
* const schema = z.string();
|
|
79
|
+
* const typeName = getZodTypeName(schema);
|
|
80
|
+
* console.log(typeName); // 'ZodString' (both v3 and v4)
|
|
81
|
+
*
|
|
82
|
+
* if (typeName === ZodTypeNames.STRING) {
|
|
83
|
+
* console.log('It\'s a string schema!');
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function getZodTypeName(zodType: unknown): string | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Check if Zod type matches expected type name
|
|
90
|
+
*
|
|
91
|
+
* Version-agnostic type checking using duck typing.
|
|
92
|
+
* Safer than instanceof when Zod versions may differ.
|
|
93
|
+
*
|
|
94
|
+
* @param zodType - Zod type to check
|
|
95
|
+
* @param typeName - Expected type name constant from ZodTypeNames
|
|
96
|
+
* @returns True if type matches, false otherwise
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* import { z } from 'zod';
|
|
101
|
+
* import { isZodType, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
102
|
+
*
|
|
103
|
+
* const schema = z.array(z.string());
|
|
104
|
+
*
|
|
105
|
+
* if (isZodType(schema, ZodTypeNames.ARRAY)) {
|
|
106
|
+
* console.log('It\'s an array schema!');
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare function isZodType(zodType: unknown, typeName: ZodTypeName): boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Unwrap optional/nullable Zod types to get inner type
|
|
113
|
+
*
|
|
114
|
+
* Recursively unwraps ZodOptional and ZodNullable to reach
|
|
115
|
+
* the underlying type. Returns original type if not wrapped.
|
|
116
|
+
*
|
|
117
|
+
* @param zodType - Zod type to unwrap
|
|
118
|
+
* @returns Unwrapped Zod type
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* import { z } from 'zod';
|
|
123
|
+
* import { unwrapZodType, getZodTypeName, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
124
|
+
*
|
|
125
|
+
* const schema = z.string().optional();
|
|
126
|
+
* const unwrapped = unwrapZodType(schema);
|
|
127
|
+
*
|
|
128
|
+
* console.log(getZodTypeName(unwrapped)); // 'ZodString'
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export declare function unwrapZodType(zodType: unknown): unknown;
|
|
132
|
+
/**
|
|
133
|
+
* Check if Zod type is optional (ZodOptional)
|
|
134
|
+
*
|
|
135
|
+
* @param zodType - Zod type to check
|
|
136
|
+
* @returns True if type is optional
|
|
137
|
+
*/
|
|
138
|
+
export declare function isZodOptional(zodType: unknown): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Check if Zod type is nullable (ZodNullable)
|
|
141
|
+
*
|
|
142
|
+
* @param zodType - Zod type to check
|
|
143
|
+
* @returns True if type is nullable
|
|
144
|
+
*/
|
|
145
|
+
export declare function isZodNullable(zodType: unknown): boolean;
|
|
146
|
+
//# sourceMappingURL=zod-introspection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-introspection.d.ts","sourceRoot":"","sources":["../src/zod-introspection.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCf,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,OAAO,YAAY,CAAC,CAAC;AAkC3E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAiBnE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,GAAG,OAAO,CAE1E;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAUvD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAEvD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAEvD"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod type introspection utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides version-agnostic type detection using duck typing instead of
|
|
5
|
+
* instanceof checks, which fail across Zod v3/v4 boundaries.
|
|
6
|
+
*
|
|
7
|
+
* @module zod-introspection
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Zod type names from _def.typeName
|
|
11
|
+
*
|
|
12
|
+
* These internal type names are stable across Zod v3 and v4.
|
|
13
|
+
* Using these constants instead of instanceof checks ensures
|
|
14
|
+
* compatibility when user's Zod version differs from library's.
|
|
15
|
+
*
|
|
16
|
+
* @see https://github.com/colinhacks/zod/issues/2543
|
|
17
|
+
*/
|
|
18
|
+
export const ZodTypeNames = {
|
|
19
|
+
STRING: 'ZodString',
|
|
20
|
+
NUMBER: 'ZodNumber',
|
|
21
|
+
BOOLEAN: 'ZodBoolean',
|
|
22
|
+
ARRAY: 'ZodArray',
|
|
23
|
+
OBJECT: 'ZodObject',
|
|
24
|
+
ENUM: 'ZodEnum',
|
|
25
|
+
OPTIONAL: 'ZodOptional',
|
|
26
|
+
NULLABLE: 'ZodNullable',
|
|
27
|
+
DATE: 'ZodDate',
|
|
28
|
+
LITERAL: 'ZodLiteral',
|
|
29
|
+
UNION: 'ZodUnion',
|
|
30
|
+
INTERSECTION: 'ZodIntersection',
|
|
31
|
+
TUPLE: 'ZodTuple',
|
|
32
|
+
RECORD: 'ZodRecord',
|
|
33
|
+
MAP: 'ZodMap',
|
|
34
|
+
SET: 'ZodSet',
|
|
35
|
+
FUNCTION: 'ZodFunction',
|
|
36
|
+
LAZY: 'ZodLazy',
|
|
37
|
+
PROMISE: 'ZodPromise',
|
|
38
|
+
BRANDED: 'ZodBranded',
|
|
39
|
+
PIPELINE: 'ZodPipeline',
|
|
40
|
+
READONLY: 'ZodReadonly',
|
|
41
|
+
SYMBOL: 'ZodSymbol',
|
|
42
|
+
UNDEFINED: 'ZodUndefined',
|
|
43
|
+
NULL: 'ZodNull',
|
|
44
|
+
ANY: 'ZodAny',
|
|
45
|
+
UNKNOWN: 'ZodUnknown',
|
|
46
|
+
NEVER: 'ZodNever',
|
|
47
|
+
VOID: 'ZodVoid',
|
|
48
|
+
BIGINT: 'ZodBigInt',
|
|
49
|
+
EFFECTS: 'ZodEffects',
|
|
50
|
+
NATIVENUM: 'ZodNativeEnum',
|
|
51
|
+
DISCRIMINATED_UNION: 'ZodDiscriminatedUnion',
|
|
52
|
+
DEFAULT: 'ZodDefault',
|
|
53
|
+
CATCH: 'ZodCatch',
|
|
54
|
+
NAN: 'ZodNaN',
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Convert Zod v4 lowercase type to v3 PascalCase format
|
|
58
|
+
*
|
|
59
|
+
* Zod v4 changed from _def.typeName ('ZodString') to _def.type ('string').
|
|
60
|
+
* This function normalizes v4 types to match v3 format for consistency.
|
|
61
|
+
*
|
|
62
|
+
* @param v4Type - Lowercase type from Zod v4 _def.type
|
|
63
|
+
* @returns PascalCase type name with 'Zod' prefix
|
|
64
|
+
* @internal
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* zodV4TypeToV3Name('string') // 'ZodString'
|
|
69
|
+
* zodV4TypeToV3Name('array') // 'ZodArray'
|
|
70
|
+
* zodV4TypeToV3Name('optional') // 'ZodOptional'
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
function zodV4TypeToV3Name(v4Type) {
|
|
74
|
+
// Handle special cases with different naming
|
|
75
|
+
const specialCases = {
|
|
76
|
+
nativeenum: 'ZodNativeEnum',
|
|
77
|
+
bigint: 'ZodBigInt',
|
|
78
|
+
};
|
|
79
|
+
if (specialCases[v4Type]) {
|
|
80
|
+
return specialCases[v4Type];
|
|
81
|
+
}
|
|
82
|
+
// Convert: 'string' → 'ZodString', 'array' → 'ZodArray'
|
|
83
|
+
return 'Zod' + v4Type.charAt(0).toUpperCase() + v4Type.slice(1);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get Zod type name in version-agnostic way
|
|
87
|
+
*
|
|
88
|
+
* Supports both Zod v3 and v4:
|
|
89
|
+
* - Zod v3: Uses _def.typeName (e.g., 'ZodString')
|
|
90
|
+
* - Zod v4: Uses _def.type (e.g., 'string'), converts to v3 format
|
|
91
|
+
*
|
|
92
|
+
* Uses duck typing instead of instanceof checks, which fail when
|
|
93
|
+
* user's Zod version differs from library's Zod version.
|
|
94
|
+
*
|
|
95
|
+
* @param zodType - Zod type to introspect
|
|
96
|
+
* @returns Type name string (e.g., 'ZodString') or undefined if not a Zod type
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* import { z } from 'zod';
|
|
101
|
+
* import { getZodTypeName, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
102
|
+
*
|
|
103
|
+
* const schema = z.string();
|
|
104
|
+
* const typeName = getZodTypeName(schema);
|
|
105
|
+
* console.log(typeName); // 'ZodString' (both v3 and v4)
|
|
106
|
+
*
|
|
107
|
+
* if (typeName === ZodTypeNames.STRING) {
|
|
108
|
+
* console.log('It\'s a string schema!');
|
|
109
|
+
* }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function getZodTypeName(zodType) {
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
114
|
+
const def = zodType?._def;
|
|
115
|
+
if (!def)
|
|
116
|
+
return undefined;
|
|
117
|
+
// Zod v3: uses _def.typeName (e.g., 'ZodString')
|
|
118
|
+
if (def.typeName) {
|
|
119
|
+
return def.typeName;
|
|
120
|
+
}
|
|
121
|
+
// Zod v4: uses _def.type (e.g., 'string')
|
|
122
|
+
// Convert to Zod v3 format for consistency
|
|
123
|
+
if (def.type) {
|
|
124
|
+
return zodV4TypeToV3Name(def.type);
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Check if Zod type matches expected type name
|
|
130
|
+
*
|
|
131
|
+
* Version-agnostic type checking using duck typing.
|
|
132
|
+
* Safer than instanceof when Zod versions may differ.
|
|
133
|
+
*
|
|
134
|
+
* @param zodType - Zod type to check
|
|
135
|
+
* @param typeName - Expected type name constant from ZodTypeNames
|
|
136
|
+
* @returns True if type matches, false otherwise
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* import { z } from 'zod';
|
|
141
|
+
* import { isZodType, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
142
|
+
*
|
|
143
|
+
* const schema = z.array(z.string());
|
|
144
|
+
*
|
|
145
|
+
* if (isZodType(schema, ZodTypeNames.ARRAY)) {
|
|
146
|
+
* console.log('It\'s an array schema!');
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export function isZodType(zodType, typeName) {
|
|
151
|
+
return getZodTypeName(zodType) === typeName;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Unwrap optional/nullable Zod types to get inner type
|
|
155
|
+
*
|
|
156
|
+
* Recursively unwraps ZodOptional and ZodNullable to reach
|
|
157
|
+
* the underlying type. Returns original type if not wrapped.
|
|
158
|
+
*
|
|
159
|
+
* @param zodType - Zod type to unwrap
|
|
160
|
+
* @returns Unwrapped Zod type
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* import { z } from 'zod';
|
|
165
|
+
* import { unwrapZodType, getZodTypeName, ZodTypeNames } from '@vibe-agent-toolkit/utils';
|
|
166
|
+
*
|
|
167
|
+
* const schema = z.string().optional();
|
|
168
|
+
* const unwrapped = unwrapZodType(schema);
|
|
169
|
+
*
|
|
170
|
+
* console.log(getZodTypeName(unwrapped)); // 'ZodString'
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function unwrapZodType(zodType) {
|
|
174
|
+
const typeName = getZodTypeName(zodType);
|
|
175
|
+
if (typeName === ZodTypeNames.OPTIONAL || typeName === ZodTypeNames.NULLABLE) {
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
|
+
const inner = zodType.unwrap?.() ?? zodType._def?.innerType;
|
|
178
|
+
return inner ? unwrapZodType(inner) : zodType;
|
|
179
|
+
}
|
|
180
|
+
return zodType;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Check if Zod type is optional (ZodOptional)
|
|
184
|
+
*
|
|
185
|
+
* @param zodType - Zod type to check
|
|
186
|
+
* @returns True if type is optional
|
|
187
|
+
*/
|
|
188
|
+
export function isZodOptional(zodType) {
|
|
189
|
+
return isZodType(zodType, ZodTypeNames.OPTIONAL);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Check if Zod type is nullable (ZodNullable)
|
|
193
|
+
*
|
|
194
|
+
* @param zodType - Zod type to check
|
|
195
|
+
* @returns True if type is nullable
|
|
196
|
+
*/
|
|
197
|
+
export function isZodNullable(zodType) {
|
|
198
|
+
return isZodType(zodType, ZodTypeNames.NULLABLE);
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=zod-introspection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-introspection.js","sourceRoot":"","sources":["../src/zod-introspection.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,MAAM,EAAE,WAAW;IACnB,MAAM,EAAE,WAAW;IACnB,OAAO,EAAE,YAAY;IACrB,KAAK,EAAE,UAAU;IACjB,MAAM,EAAE,WAAW;IACnB,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OAAgB;IAC5C,OAAO,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;AACnD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibe-agent-toolkit/utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Core utility functions with no external dependencies",
|
|
6
6
|
"keywords": [
|
|
@@ -34,12 +34,16 @@
|
|
|
34
34
|
"picomatch": "^4.0.3",
|
|
35
35
|
"which": "^5.0.0"
|
|
36
36
|
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
39
|
+
},
|
|
37
40
|
"devDependencies": {
|
|
38
41
|
"@types/picomatch": "^4.0.2",
|
|
39
42
|
"@types/which": "^3.0.4",
|
|
40
43
|
"rimraf": "^6.0.1",
|
|
41
44
|
"typescript": "^5.9.3",
|
|
42
|
-
"vitest": "^3.2.4"
|
|
45
|
+
"vitest": "^3.2.4",
|
|
46
|
+
"zod": "^3.25.0"
|
|
43
47
|
},
|
|
44
48
|
"publishConfig": {
|
|
45
49
|
"access": "public"
|