use super::*;
pub fn validate_enhanced_generate_content_request( request : &GenerateContentRequest ) -> Result< (), ValidationError >
{
if request.contents.is_empty()
{
return Err( ValidationError::EmptyCollection {
field : "contents".to_string(),
context : "GenerateContentRequest".to_string(),
} );
}
for ( i, content ) in request.contents.iter().enumerate()
{
validate_content( content )
.map_err( |e| ValidationError::InvalidFieldValue {
field : format!( "contents[{i}]" ),
value : "Content".to_string(),
reason : e.to_string(),
} )?;
}
if let Some( tool_config ) = &request.tool_config
{
validate_tool_config( tool_config )
.map_err( |e| ValidationError::InvalidFieldValue {
field : "tool_config".to_string(),
value : "ToolConfig".to_string(),
reason : e.to_string(),
} )?;
}
if let Some( system_instruction ) = &request.system_instruction
{
validate_system_instruction( system_instruction )
.map_err( |e| ValidationError::InvalidFieldValue {
field : "system_instruction".to_string(),
value : "SystemInstruction".to_string(),
reason : e.to_string(),
} )?;
}
if let Some( tools ) = &request.tools
{
if tools.is_empty()
{
return Err( ValidationError::EmptyCollection {
field : "tools".to_string(),
context : "GenerateContentRequest".to_string(),
} );
}
for ( i, tool ) in tools.iter().enumerate()
{
validate_tool( tool )
.map_err( |e| ValidationError::InvalidFieldValue {
field : format!( "tools[{}]", i ),
value : "Tool".to_string(),
reason : e.to_string(),
} )?;
}
}
Ok( () )
}
pub fn validate_tool( tool : &Tool ) -> Result< (), ValidationError >
{
if let Some( code_execution_tool ) = &tool.code_execution_tool
{
validate_code_execution_tool( code_execution_tool )
.map_err( |e| ValidationError::InvalidFieldValue {
field : "code_execution_tool".to_string(),
value : "CodeExecutionTool".to_string(),
reason : e.to_string(),
} )?;
}
if let Some( function_declarations ) = &tool.function_declarations
{
if function_declarations.is_empty()
{
return Err( ValidationError::EmptyCollection {
field : "function_declarations".to_string(),
context : "Tool".to_string(),
} );
}
for ( i, function_declaration ) in function_declarations.iter().enumerate()
{
validate_function_declaration( function_declaration )
.map_err( |e| ValidationError::InvalidFieldValue {
field : format!( "function_declarations[{}]", i ),
value : "FunctionDeclaration".to_string(),
reason : e.to_string(),
} )?;
}
}
Ok( () )
}
pub fn validate_function_declaration( declaration : &FunctionDeclaration ) -> Result< (), ValidationError >
{
if declaration.name.trim().is_empty()
{
return Err( ValidationError::RequiredFieldMissing {
field : "name".to_string(),
context : "FunctionDeclaration".to_string(),
} );
}
if !declaration.name.chars().all( |c| c.is_alphanumeric() || c == '_' )
{
return Err( ValidationError::InvalidFieldValue {
field : "name".to_string(),
value : declaration.name.clone(),
reason : "Function name should contain only alphanumeric characters and underscores".to_string(),
} );
}
Ok( () )
}