Skip to content

Commit 8c9364b

Browse files
committed
add zod validation
1 parent a586fac commit 8c9364b

File tree

3 files changed

+48
-68
lines changed

3 files changed

+48
-68
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"multer": "2.0.1",
2929
"google-auth-library": "^9.0.0",
3030
"@aws-sdk/client-s3": "^3.450.0",
31-
"@aws-sdk/lib-storage": "^3.450.0"
31+
"@aws-sdk/lib-storage": "^3.450.0",
32+
"zod": "^3.23.8"
3233
},
3334
"devDependencies": {
3435
"nodemon": "^3.0.3",
Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,40 @@
1+
import { z } from 'zod'
12
import { imageModels } from '../shared/imageModels/index.js'
23

3-
// Validate the parameters for the image generation request and return only the valid parameters
4+
const bodySchema = z.object({
5+
prompt: z.string().min(1, { message: "'prompt' is a required parameter" }),
6+
model: z.string().min(1, { message: "'model' is a required parameter" }),
7+
response_format: z.enum(['url', 'b64_json']).default('url'),
8+
quality: z.enum(['auto', 'low', 'medium', 'high']).optional()
9+
})
10+
411
export function validateImageParams(req) {
5-
const { prompt, model, response_format, size, quality } = req.body
6-
const files = req.files || {}
12+
const parseResult = bodySchema.safeParse(req.body)
13+
if (!parseResult.success) {
14+
throw new Error(parseResult.error.errors[0].message)
15+
}
716

8-
if (!prompt) throw new Error("'prompt' is a required parameter")
9-
if (!model) throw new Error("'model' is a required parameter")
17+
const { prompt, model, response_format, quality } = parseResult.data
18+
const files = req.files || {}
1019

11-
if (model === 'google/gemini-2.0-flash-prev') {
12-
throw new Error("'google/gemini-2.0-flash-prev' is temporary disabled. Please use 'google/gemini-2.0-flash-exp' instead.")
13-
}
20+
if (model === 'google/gemini-2.0-flash-prev') {
21+
throw new Error("'google/gemini-2.0-flash-prev' is temporary disabled. Please use 'google/gemini-2.0-flash-exp' instead.")
22+
}
1423

15-
// Validate model parameter and config
16-
const modelConfig = imageModels[model]
17-
if (!modelConfig) throw new Error("model '" + model + "' is not available")
18-
if (!modelConfig?.providers[0].id) throw new Error("model provider for '" + model + "' is not available")
24+
const modelConfig = imageModels[model]
25+
if (!modelConfig) throw new Error(`model '${model}' is not available`)
26+
if (!modelConfig?.providers[0]?.id) throw new Error(`model provider for '${model}' is not available`)
1927

20-
// Set default response_format and validate
21-
const validResponseFormat = response_format || 'url'
22-
if (!['url', 'b64_json'].includes(validResponseFormat)) {
23-
throw new Error("'response_format' must be either 'url' or 'b64_json'")
24-
}
25-
if (size) {
26-
throw new Error("'size' is not yet supported.")
27-
}
28-
// Validate quality parameter. Can be low, medium, high.
29-
let qualityLower
30-
if (quality) {
31-
qualityLower = quality.toLowerCase()
32-
const allowedQualities = ['auto', 'low', 'medium', 'high']
33-
if (!allowedQualities.includes(qualityLower)) {
34-
throw new Error(`'quality' must be one of: ${allowedQualities.join(', ')}`)
35-
}
36-
}
37-
38-
// Initialize validFiles object
39-
let validFiles = {}
40-
41-
// Validate image parameter - can be multiple images (up to 16)
42-
if (files.image) {
43-
validFiles.image = files.image
44-
// Ensure we don't exceed the maximum number of images
45-
if (Array.isArray(validFiles.image) && validFiles.image.length > 16) {
46-
throw new Error("Maximum of 16 images can be uploaded")
47-
}
48-
}
49-
50-
// Validate mask parameter
51-
if (files.mask) {
52-
validFiles.mask = files.mask
53-
}
28+
// Validate files
29+
const validFiles = {}
30+
if (files.image) {
31+
const images = Array.isArray(files.image) ? files.image : [files.image]
32+
if (images.length > 16) throw new Error('Maximum of 16 images can be uploaded')
33+
validFiles.image = images
34+
}
35+
if (files.mask) {
36+
validFiles.mask = Array.isArray(files.mask) ? files.mask[0] : files.mask
37+
}
5438

55-
return { prompt, model, response_format: validResponseFormat, quality: qualityLower, files: validFiles }
39+
return { prompt, model, response_format, quality, files: validFiles }
5640
}
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1+
import { z } from 'zod'
12
import { videoModels } from '../shared/videoModels/index.js'
23

4+
const bodySchema = z.object({
5+
prompt: z.string().min(1, { message: "'prompt' is a required parameter" }),
6+
model: z.string().min(1, { message: "'model' is a required parameter" }),
7+
response_format: z.enum(['url', 'b64_json']).default('url'),
8+
})
9+
310
// Validate the parameters for the video generation request and return only the valid parameters
411
export function validateVideoParams(req) {
5-
const { prompt, model, response_format, size, quality } = req.body
12+
const parseResult = bodySchema.safeParse(req.body)
13+
if (!parseResult.success) {
14+
throw new Error(parseResult.error.errors[0].message)
15+
}
616

7-
if (!prompt) throw new Error("'prompt' is a required parameter")
8-
if (!model) throw new Error("'model' is a required parameter")
17+
const { prompt, model, response_format } = parseResult.data
918

1019
// Validate model parameter and config
1120
const modelConfig = videoModels[model]
12-
if (!modelConfig) throw new Error("model '" + model + "' is not available")
13-
if (!modelConfig?.providers[0].id) throw new Error("model provider for '" + model + "' is not available")
14-
15-
// Set default response_format and validate
16-
const validResponseFormat = response_format || 'url'
17-
if (!['url', 'b64_json'].includes(validResponseFormat)) {
18-
throw new Error("'response_format' must be either 'url' or 'b64_json'")
19-
}
20-
if (size) {
21-
throw new Error("'size' is not yet supported.")
22-
}
23-
24-
25-
if (quality) {
26-
throw new Error("'quality' is not yet supported.")
27-
}
21+
if (!modelConfig) throw new Error(`model '${model}' is not available`)
22+
if (!modelConfig?.providers[0]?.id) throw new Error(`model provider for '${model}' is not available`)
2823

29-
return { prompt, model, response_format: validResponseFormat }
24+
return { prompt, model, response_format }
3025
}

0 commit comments

Comments
 (0)