@@ -380,45 +380,61 @@ defmodule LLMAgent.Handlers do
380
380
defp validate_tool_parameters ( args , schema ) do
381
381
# Implementation of JSON Schema validation
382
382
# This is a simplified version, in production would use a proper validator
383
- try do
384
- # Check required fields
385
- required = Map . get ( schema , "required" , [ ] )
386
- missing_fields = Enum . filter ( required , fn field -> is_nil ( Map . get ( args , field ) ) end )
383
+ with { :ok , _ } <- validate_required_fields ( args , schema ) ,
384
+ { :ok , _ } <- validate_field_types ( args , schema ) do
385
+ { :ok , args }
386
+ end
387
+ rescue
388
+ e -> { :error , % { validation_error: Exception . message ( e ) } }
389
+ end
387
390
388
- if length ( missing_fields ) > 0 do
389
- { :error , % { missing_required: missing_fields } }
390
- else
391
- # Validate types if properties defined
392
- properties = Map . get ( schema , "properties" , % { } )
393
-
394
- validation_errors =
395
- Enum . reduce ( properties , % { } , fn { field , field_schema } , errors ->
396
- value = Map . get ( args , field )
397
-
398
- if is_nil ( value ) do
399
- # Skip validation for optional fields that are not provided
400
- errors
401
- else
402
- # Validate field type
403
- expected_type = Map . get ( field_schema , "type" )
404
- actual_type = determine_json_type ( value )
405
-
406
- if expected_type != actual_type do
407
- Map . put ( errors , field , "expected #{ expected_type } , got #{ actual_type } " )
408
- else
409
- errors
410
- end
411
- end
412
- end )
391
+ # Validate that all required fields are present
392
+ defp validate_required_fields ( args , schema ) do
393
+ required = Map . get ( schema , "required" , [ ] )
394
+ missing_fields = Enum . filter ( required , fn field -> is_nil ( Map . get ( args , field ) ) end )
413
395
414
- if map_size ( validation_errors ) > 0 do
415
- { :error , % { type_mismatch: validation_errors } }
416
- else
417
- { :ok , args }
418
- end
396
+ if length ( missing_fields ) > 0 do
397
+ { :error , % { missing_required: missing_fields } }
398
+ else
399
+ { :ok , args }
400
+ end
401
+ end
402
+
403
+ # Validate field types against schema
404
+ defp validate_field_types ( args , schema ) do
405
+ properties = Map . get ( schema , "properties" , % { } )
406
+ validation_errors = check_field_types ( args , properties )
407
+
408
+ if map_size ( validation_errors ) > 0 do
409
+ { :error , % { type_mismatch: validation_errors } }
410
+ else
411
+ { :ok , args }
412
+ end
413
+ end
414
+
415
+ # Helper to check types of all fields
416
+ defp check_field_types ( args , properties ) do
417
+ Enum . reduce ( properties , % { } , fn { field , field_schema } , errors ->
418
+ value = Map . get ( args , field )
419
+
420
+ if is_nil ( value ) do
421
+ # Skip validation for optional fields that are not provided
422
+ errors
423
+ else
424
+ validate_field_type ( field , field_schema , value , errors )
419
425
end
420
- rescue
421
- e -> { :error , % { validation_error: Exception . message ( e ) } }
426
+ end )
427
+ end
428
+
429
+ # Validate individual field type
430
+ defp validate_field_type ( field , field_schema , value , errors ) do
431
+ expected_type = Map . get ( field_schema , "type" )
432
+ actual_type = determine_json_type ( value )
433
+
434
+ if expected_type != actual_type do
435
+ Map . put ( errors , field , "expected #{ expected_type } , got #{ actual_type } " )
436
+ else
437
+ errors
422
438
end
423
439
end
424
440
0 commit comments