midjourney-mcp
Version:
A Model Context Protocol server for Midjourney integration via MJ API
255 lines • 9.92 kB
JavaScript
/**
* Validation utilities for Midjourney MCP tools
*/
import { isValidAspectRatio, isValidQuality, isValidStyle, } from '../types/index.js';
// ============================================================================
// Base Validation Functions
// ============================================================================
export function createValidationError(field, message, value) {
return { field, message, value };
}
export function validateRequired(value, fieldName) {
if (value === undefined || value === null || value === '') {
return createValidationError(fieldName, `${fieldName} is required`, value);
}
return null;
}
export function validateString(value, fieldName, minLength, maxLength) {
if (typeof value !== 'string') {
return createValidationError(fieldName, `${fieldName} must be a string`, value);
}
if (minLength !== undefined && value.length < minLength) {
return createValidationError(fieldName, `${fieldName} must be at least ${minLength} characters long`, value);
}
if (maxLength !== undefined && value.length > maxLength) {
return createValidationError(fieldName, `${fieldName} must be no more than ${maxLength} characters long`, value);
}
return null;
}
export function validateNumber(value, fieldName, min, max) {
if (typeof value !== 'number' || isNaN(value)) {
return createValidationError(fieldName, `${fieldName} must be a number`, value);
}
if (min !== undefined && value < min) {
return createValidationError(fieldName, `${fieldName} must be at least ${min}`, value);
}
if (max !== undefined && value > max) {
return createValidationError(fieldName, `${fieldName} must be no more than ${max}`, value);
}
return null;
}
export function validateArray(value, fieldName, minItems, maxItems) {
if (!Array.isArray(value)) {
return createValidationError(fieldName, `${fieldName} must be an array`, value);
}
if (minItems !== undefined && value.length < minItems) {
return createValidationError(fieldName, `${fieldName} must have at least ${minItems} items`, value);
}
if (maxItems !== undefined && value.length > maxItems) {
return createValidationError(fieldName, `${fieldName} must have no more than ${maxItems} items`, value);
}
return null;
}
export function validateBase64(value, fieldName) {
if (!value.match(/^[A-Za-z0-9+/]*={0,2}$/)) {
return createValidationError(fieldName, `${fieldName} must be valid base64 encoded data`, value);
}
return null;
}
// ============================================================================
// Specific Tool Validation Functions
// ============================================================================
export function validateImagineParams(params) {
const errors = [];
// Required fields
const promptError = validateRequired(params.prompt, 'prompt');
if (promptError)
errors.push(promptError);
const promptStringError = validateString(params.prompt, 'prompt', 1, 4000);
if (promptStringError)
errors.push(promptStringError);
// Optional fields
if (params.aspect_ratio !== undefined && !isValidAspectRatio(params.aspect_ratio)) {
errors.push(createValidationError('aspect_ratio', 'Invalid aspect ratio', params.aspect_ratio));
}
if (params.quality !== undefined && !isValidQuality(params.quality)) {
errors.push(createValidationError('quality', 'Invalid quality setting', params.quality));
}
if (params.style !== undefined && !isValidStyle(params.style)) {
errors.push(createValidationError('style', 'Invalid style setting', params.style));
}
if (params.chaos !== undefined) {
const chaosError = validateNumber(params.chaos, 'chaos', 0, 100);
if (chaosError)
errors.push(chaosError);
}
if (params.stylize !== undefined) {
const stylizeError = validateNumber(params.stylize, 'stylize', 0, 1000);
if (stylizeError)
errors.push(stylizeError);
}
if (params.weird !== undefined) {
const weirdError = validateNumber(params.weird, 'weird', 0, 3000);
if (weirdError)
errors.push(weirdError);
}
if (params.reference_images !== undefined) {
const arrayError = validateArray(params.reference_images, 'reference_images', 0, 5);
if (arrayError) {
errors.push(arrayError);
}
else {
params.reference_images.forEach((img, index) => {
const imgError = validateString(img, `reference_images[${index}]`);
if (imgError)
errors.push(imgError);
const base64Error = validateBase64(img, `reference_images[${index}]`);
if (base64Error)
errors.push(base64Error);
});
}
}
return {
isValid: errors.length === 0,
errors
};
}
export function validateUpscaleParams(params) {
const errors = [];
const taskIdError = validateRequired(params.task_id, 'task_id');
if (taskIdError)
errors.push(taskIdError);
const taskIdStringError = validateString(params.task_id, 'task_id');
if (taskIdStringError)
errors.push(taskIdStringError);
const indexError = validateRequired(params.index, 'index');
if (indexError)
errors.push(indexError);
const indexNumberError = validateNumber(params.index, 'index', 1, 4);
if (indexNumberError)
errors.push(indexNumberError);
return {
isValid: errors.length === 0,
errors
};
}
export function validateVariationParams(params) {
return validateUpscaleParams(params); // Same validation as upscale
}
export function validateRerollParams(params) {
const errors = [];
const taskIdError = validateRequired(params.task_id, 'task_id');
if (taskIdError)
errors.push(taskIdError);
const taskIdStringError = validateString(params.task_id, 'task_id');
if (taskIdStringError)
errors.push(taskIdStringError);
return {
isValid: errors.length === 0,
errors
};
}
export function validateBlendParams(params) {
const errors = [];
const imagesError = validateRequired(params.images, 'images');
if (imagesError)
errors.push(imagesError);
const arrayError = validateArray(params.images, 'images', 2, 5);
if (arrayError) {
errors.push(arrayError);
}
else {
params.images.forEach((img, index) => {
const imgError = validateString(img, `images[${index}]`);
if (imgError)
errors.push(imgError);
const base64Error = validateBase64(img, `images[${index}]`);
if (base64Error)
errors.push(base64Error);
});
}
if (params.aspect_ratio !== undefined && !isValidAspectRatio(params.aspect_ratio)) {
errors.push(createValidationError('aspect_ratio', 'Invalid aspect ratio', params.aspect_ratio));
}
return {
isValid: errors.length === 0,
errors
};
}
export function validateDescribeParams(params) {
const errors = [];
const imageError = validateRequired(params.image, 'image');
if (imageError)
errors.push(imageError);
const imageStringError = validateString(params.image, 'image');
if (imageStringError)
errors.push(imageStringError);
const base64Error = validateBase64(params.image, 'image');
if (base64Error)
errors.push(base64Error);
return {
isValid: errors.length === 0,
errors
};
}
export function validateGetTaskParams(params) {
const errors = [];
const taskIdError = validateRequired(params.task_id, 'task_id');
if (taskIdError)
errors.push(taskIdError);
const taskIdStringError = validateString(params.task_id, 'task_id');
if (taskIdStringError)
errors.push(taskIdStringError);
return {
isValid: errors.length === 0,
errors
};
}
export function validateActionParams(params) {
const errors = [];
const taskIdError = validateRequired(params.task_id, 'task_id');
if (taskIdError)
errors.push(taskIdError);
const taskIdStringError = validateString(params.task_id, 'task_id');
if (taskIdStringError)
errors.push(taskIdStringError);
const actionError = validateRequired(params.action, 'action');
if (actionError)
errors.push(actionError);
const actionStringError = validateString(params.action, 'action');
if (actionStringError)
errors.push(actionStringError);
return {
isValid: errors.length === 0,
errors
};
}
// ============================================================================
// Tool Validation Router
// ============================================================================
export function validateToolParams(toolName, params) {
switch (toolName) {
case 'midjourney_imagine':
return validateImagineParams(params);
case 'midjourney_upscale':
return validateUpscaleParams(params);
case 'midjourney_variation':
return validateVariationParams(params);
case 'midjourney_reroll':
return validateRerollParams(params);
case 'midjourney_blend':
return validateBlendParams(params);
case 'midjourney_describe':
return validateDescribeParams(params);
case 'midjourney_get_task':
return validateGetTaskParams(params);
case 'midjourney_action':
return validateActionParams(params);
default:
return {
isValid: false,
errors: [createValidationError('toolName', `Unknown tool: ${toolName}`, toolName)]
};
}
}
//# sourceMappingURL=validators.js.map