@getvision/adapter-express 0.0.0-develop-20251031183955
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/.eslintrc.js +9 -0
- package/.turbo/turbo-build.log +1 -0
- package/README.md +266 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +435 -0
- package/dist/zod-utils.d.ts +7 -0
- package/dist/zod-utils.d.ts.map +1 -0
- package/dist/zod-utils.js +145 -0
- package/dist/zod-validator.d.ts +44 -0
- package/dist/zod-validator.d.ts.map +1 -0
- package/dist/zod-validator.js +98 -0
- package/package.json +30 -0
- package/src/index.ts +529 -0
- package/src/zod-utils.ts +170 -0
- package/src/zod-validator.ts +118 -0
- package/tsconfig.json +13 -0
package/src/zod-utils.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { ZodType, ZodObject, ZodTypeAny } from 'zod'
|
|
2
|
+
import type { RequestBodySchema, SchemaField } from '@getvision/core'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generate JSON template with comments from Zod schema
|
|
6
|
+
*/
|
|
7
|
+
export function generateZodTemplate(schema: ZodType): RequestBodySchema | undefined {
|
|
8
|
+
try {
|
|
9
|
+
const fields = extractZodFields(schema)
|
|
10
|
+
const template = generateJsonTemplate(fields)
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
template,
|
|
14
|
+
fields,
|
|
15
|
+
}
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.warn('Failed to generate template from Zod schema:', error)
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Extract fields from Zod schema (supports v3 and v4)
|
|
24
|
+
*/
|
|
25
|
+
function extractZodFields(schema: ZodTypeAny, path: string[] = []): SchemaField[] {
|
|
26
|
+
const fields: SchemaField[] = []
|
|
27
|
+
|
|
28
|
+
// Support both v3 (_def) and v4 (def)
|
|
29
|
+
const def = (schema as any).def || (schema as any)._def
|
|
30
|
+
if (!def) return fields
|
|
31
|
+
|
|
32
|
+
// Unwrap ZodOptional, ZodNullable, ZodDefault - not needed for top level object
|
|
33
|
+
let unwrapped: any = schema
|
|
34
|
+
|
|
35
|
+
const currentDef = (unwrapped as any).def || (unwrapped as any)._def
|
|
36
|
+
const typeName = currentDef?.type || currentDef?.typeName
|
|
37
|
+
|
|
38
|
+
if (typeName === 'object' || typeName === 'ZodObject') {
|
|
39
|
+
// Get shape - it can be a function (getter) or direct object
|
|
40
|
+
const shapeValue = currentDef?.shape
|
|
41
|
+
const shape = typeof shapeValue === 'function' ? shapeValue() : shapeValue
|
|
42
|
+
|
|
43
|
+
for (const [key, value] of Object.entries(shape || {})) {
|
|
44
|
+
const fieldSchema: any = value
|
|
45
|
+
const fieldDef = fieldSchema.def || fieldSchema._def
|
|
46
|
+
const isOptional = fieldSchema.type === 'optional' ||
|
|
47
|
+
fieldDef?.type === 'optional' ||
|
|
48
|
+
fieldDef?.typeName === 'ZodOptional' ||
|
|
49
|
+
fieldDef?.typeName === 'ZodDefault'
|
|
50
|
+
|
|
51
|
+
// Get description - might be in wrapped schema for optional fields
|
|
52
|
+
let description = fieldDef?.description || fieldSchema.description
|
|
53
|
+
if (!description && fieldDef?.wrapped) {
|
|
54
|
+
const wrappedDef = fieldDef.wrapped.def || fieldDef.wrapped._def
|
|
55
|
+
description = wrappedDef?.description || fieldDef.wrapped.description
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Determine type
|
|
59
|
+
let fieldType = getZodType(fieldSchema)
|
|
60
|
+
|
|
61
|
+
// Check for nested objects/arrays
|
|
62
|
+
const nested = extractZodFields(fieldSchema, [...path, key])
|
|
63
|
+
|
|
64
|
+
fields.push({
|
|
65
|
+
name: key,
|
|
66
|
+
type: fieldType,
|
|
67
|
+
description,
|
|
68
|
+
required: !isOptional,
|
|
69
|
+
nested: nested.length > 0 ? nested : undefined,
|
|
70
|
+
example: getZodExample(fieldSchema, fieldType),
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return fields
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get Zod type as string (supports v3 and v4)
|
|
80
|
+
*/
|
|
81
|
+
function getZodType(schema: ZodTypeAny): string {
|
|
82
|
+
let unwrapped: any = schema
|
|
83
|
+
let def = (unwrapped as any).def || (unwrapped as any)._def
|
|
84
|
+
|
|
85
|
+
// Unwrap optional/nullable/default
|
|
86
|
+
while (def?.type === 'optional' ||
|
|
87
|
+
def?.type === 'nullable' ||
|
|
88
|
+
def?.type === 'default' ||
|
|
89
|
+
def?.typeName === 'ZodOptional' ||
|
|
90
|
+
def?.typeName === 'ZodNullable' ||
|
|
91
|
+
def?.typeName === 'ZodDefault') {
|
|
92
|
+
unwrapped = def?.innerType || def?.wrapped || unwrapped
|
|
93
|
+
def = (unwrapped as any).def || (unwrapped as any)._def
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Support both v4 (type) and v3 (typeName)
|
|
97
|
+
const typeName = def?.type || def?.typeName || (unwrapped as any).type
|
|
98
|
+
|
|
99
|
+
switch (typeName) {
|
|
100
|
+
case 'string':
|
|
101
|
+
case 'ZodString':
|
|
102
|
+
return 'string'
|
|
103
|
+
case 'number':
|
|
104
|
+
case 'ZodNumber':
|
|
105
|
+
return 'number'
|
|
106
|
+
case 'boolean':
|
|
107
|
+
case 'ZodBoolean':
|
|
108
|
+
return 'boolean'
|
|
109
|
+
case 'array':
|
|
110
|
+
case 'ZodArray':
|
|
111
|
+
return 'array'
|
|
112
|
+
case 'object':
|
|
113
|
+
case 'ZodObject':
|
|
114
|
+
return 'object'
|
|
115
|
+
case 'enum':
|
|
116
|
+
case 'ZodEnum':
|
|
117
|
+
return 'enum'
|
|
118
|
+
case 'date':
|
|
119
|
+
case 'ZodDate':
|
|
120
|
+
return 'date'
|
|
121
|
+
default:
|
|
122
|
+
return 'any'
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get example value for Zod type
|
|
128
|
+
*/
|
|
129
|
+
function getZodExample(schema: ZodTypeAny, type: string): any {
|
|
130
|
+
switch (type) {
|
|
131
|
+
case 'string': return ''
|
|
132
|
+
case 'number': return 0
|
|
133
|
+
case 'boolean': return false
|
|
134
|
+
case 'array': return []
|
|
135
|
+
case 'object': return {}
|
|
136
|
+
default: return null
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Generate JSONC template with comments
|
|
142
|
+
*/
|
|
143
|
+
function generateJsonTemplate(fields: SchemaField[], indent = 0): string {
|
|
144
|
+
const lines: string[] = []
|
|
145
|
+
const spacing = ' '.repeat(indent)
|
|
146
|
+
|
|
147
|
+
lines.push('{')
|
|
148
|
+
|
|
149
|
+
fields.forEach((field, index) => {
|
|
150
|
+
const isLast = index === fields.length - 1
|
|
151
|
+
const description = field.description || field.name
|
|
152
|
+
|
|
153
|
+
// Add comment
|
|
154
|
+
lines.push(`${spacing} // ${description}`)
|
|
155
|
+
|
|
156
|
+
// Add field
|
|
157
|
+
let value: string
|
|
158
|
+
if (field.nested && field.nested.length > 0) {
|
|
159
|
+
value = generateJsonTemplate(field.nested, indent + 1)
|
|
160
|
+
} else {
|
|
161
|
+
value = JSON.stringify(field.example)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
lines.push(`${spacing} "${field.name}": ${value}${isLast ? '' : ','}`)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
lines.push(`${spacing}}`)
|
|
168
|
+
|
|
169
|
+
return lines.join('\n')
|
|
170
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction, RequestHandler } from 'express'
|
|
2
|
+
import type { ZodSchema, ZodError } from 'zod'
|
|
3
|
+
|
|
4
|
+
// Store schemas for Vision introspection
|
|
5
|
+
const routeSchemas = new Map<string, { method: string; path: string; schema: ZodSchema }>()
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get stored schema for a route
|
|
9
|
+
*/
|
|
10
|
+
export function getRouteSchema(method: string, path: string): ZodSchema | undefined {
|
|
11
|
+
const key = `${method}:${path}`
|
|
12
|
+
return routeSchemas.get(key)?.schema
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get all stored schemas
|
|
17
|
+
*/
|
|
18
|
+
export function getAllRouteSchemas(): Map<string, { method: string; path: string; schema: ZodSchema }> {
|
|
19
|
+
return routeSchemas
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type ValidateTarget = 'body' | 'query' | 'params'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Zod validator middleware for Express
|
|
26
|
+
* Similar to @hono/zod-validator but stores schema for Vision introspection
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { zValidator } from '@getvision/adapter-express'
|
|
31
|
+
* import { z } from 'zod'
|
|
32
|
+
*
|
|
33
|
+
* const schema = z.object({
|
|
34
|
+
* name: z.string().describe('User name'),
|
|
35
|
+
* email: z.string().email().describe('User email'),
|
|
36
|
+
* })
|
|
37
|
+
*
|
|
38
|
+
* app.post('/users', zValidator('body', schema), (req, res) => {
|
|
39
|
+
* // req.body is now typed and validated
|
|
40
|
+
* const { name, email } = req.body
|
|
41
|
+
* res.json({ name, email })
|
|
42
|
+
* })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function zValidator<T extends ZodSchema>(
|
|
46
|
+
target: ValidateTarget,
|
|
47
|
+
schema: T
|
|
48
|
+
): RequestHandler {
|
|
49
|
+
const middleware: RequestHandler = (req: Request, res: Response, next: NextFunction) => {
|
|
50
|
+
// Store schema for Vision (we'll update it later with actual route info)
|
|
51
|
+
const key = `${req.method}:${req.route?.path || req.path}`
|
|
52
|
+
if (req.route?.path) {
|
|
53
|
+
routeSchemas.set(key, {
|
|
54
|
+
method: req.method,
|
|
55
|
+
path: req.route.path,
|
|
56
|
+
schema,
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Get data to validate based on target
|
|
61
|
+
let data: unknown
|
|
62
|
+
switch (target) {
|
|
63
|
+
case 'body':
|
|
64
|
+
data = req.body
|
|
65
|
+
break
|
|
66
|
+
case 'query':
|
|
67
|
+
data = req.query
|
|
68
|
+
break
|
|
69
|
+
case 'params':
|
|
70
|
+
data = req.params
|
|
71
|
+
break
|
|
72
|
+
default:
|
|
73
|
+
return next(new Error(`Invalid validation target: ${target}`))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Validate data
|
|
77
|
+
const result = schema.safeParse(data)
|
|
78
|
+
|
|
79
|
+
if (!result.success) {
|
|
80
|
+
// Validation failed
|
|
81
|
+
const error = result.error as ZodError
|
|
82
|
+
return res.status(400).json({
|
|
83
|
+
error: 'Validation failed',
|
|
84
|
+
issues: error.issues,
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Store validated data back
|
|
89
|
+
switch (target) {
|
|
90
|
+
case 'body':
|
|
91
|
+
req.body = result.data
|
|
92
|
+
break
|
|
93
|
+
case 'query':
|
|
94
|
+
req.query = result.data as any
|
|
95
|
+
break
|
|
96
|
+
case 'params':
|
|
97
|
+
req.params = result.data as any
|
|
98
|
+
break
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
next()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Attach schema to middleware for Vision introspection
|
|
105
|
+
;(middleware as any).__visionSchema = schema
|
|
106
|
+
;(middleware as any).__visionTarget = target
|
|
107
|
+
|
|
108
|
+
return middleware
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Extract Zod schema from validator middleware
|
|
113
|
+
* Used internally by Vision to generate API docs
|
|
114
|
+
*/
|
|
115
|
+
export function extractSchema(middleware: RequestHandler): ZodSchema | undefined {
|
|
116
|
+
// This is a simplified version - in reality, we store schemas in the Map above
|
|
117
|
+
return undefined
|
|
118
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@repo/typescript-config/base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"target": "ES2022",
|
|
8
|
+
"module": "ESNext",
|
|
9
|
+
"moduleResolution": "bundler"
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*"],
|
|
12
|
+
"exclude": ["node_modules", "dist"]
|
|
13
|
+
}
|