|
| 1 | +import { z } from 'zod' |
1 | 2 | import { imageModels } from '../shared/imageModels/index.js' |
2 | 3 |
|
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 | + |
4 | 11 | 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 | + } |
7 | 16 |
|
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 || {} |
10 | 19 |
|
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 | + } |
14 | 23 |
|
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`) |
19 | 27 |
|
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 | + } |
54 | 38 |
|
55 | | - return { prompt, model, response_format: validResponseFormat, quality: qualityLower, files: validFiles } |
| 39 | + return { prompt, model, response_format, quality, files: validFiles } |
56 | 40 | } |
0 commit comments