|
| 1 | +/* r2mcp - MIT - Copyright 2025-2026 - pancake */ |
| 2 | + |
| 3 | +#include "validation.h" |
| 4 | +#include "jsonrpc.h" |
| 5 | + |
| 6 | +/* Check if a parameter exists and is a string */ |
| 7 | +ValidationResult validate_required_string(RJson *args, const char *param_name) { |
| 8 | + const RJson *field = r_json_get (args, param_name); |
| 9 | + if (!field || field->type != R_JSON_STRING) { |
| 10 | + char *msg = r_str_newf ("Missing required parameter '%s' (expected string)", param_name); |
| 11 | + return (ValidationResult){ false, msg }; |
| 12 | + } |
| 13 | + return (ValidationResult){ true, NULL }; |
| 14 | +} |
| 15 | + |
| 16 | +/* Check if a parameter exists and is numeric (integer or double) */ |
| 17 | +ValidationResult validate_required_number(RJson *args, const char *param_name) { |
| 18 | + const RJson *field = r_json_get (args, param_name); |
| 19 | + if (!field || (field->type != R_JSON_INTEGER && field->type != R_JSON_DOUBLE)) { |
| 20 | + char *msg = r_str_newf ("Missing required parameter '%s' (expected number)", param_name); |
| 21 | + return (ValidationResult){ false, msg }; |
| 22 | + } |
| 23 | + return (ValidationResult){ true, NULL }; |
| 24 | +} |
| 25 | + |
| 26 | +/* Check if a parameter exists and is a boolean */ |
| 27 | +ValidationResult validate_required_boolean(RJson *args, const char *param_name) { |
| 28 | + const RJson *field = r_json_get (args, param_name); |
| 29 | + if (!field || field->type != R_JSON_BOOLEAN) { |
| 30 | + char *msg = r_str_newf ("Missing required parameter '%s' (expected boolean)", param_name); |
| 31 | + return (ValidationResult){ false, msg }; |
| 32 | + } |
| 33 | + return (ValidationResult){ true, NULL }; |
| 34 | +} |
| 35 | + |
| 36 | +/* Check if a parameter exists with any valid type */ |
| 37 | +ValidationResult validate_required_param(RJson *args, const char *param_name) { |
| 38 | + const RJson *field = r_json_get (args, param_name); |
| 39 | + if (!field) { |
| 40 | + char *msg = r_str_newf ("Missing required parameter '%s'", param_name); |
| 41 | + return (ValidationResult){ false, msg }; |
| 42 | + } |
| 43 | + return (ValidationResult){ true, NULL }; |
| 44 | +} |
| 45 | + |
| 46 | +/* Parse JSON Schema to extract required properties */ |
| 47 | +static RList *parse_required_properties(const char *schema_json) { |
| 48 | + if (!schema_json || !*schema_json) { |
| 49 | + return NULL; |
| 50 | + } |
| 51 | + |
| 52 | + /* We need a non-modifiable copy for parsing */ |
| 53 | + char *schema_copy = strdup (schema_json); |
| 54 | + if (!schema_copy) { |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + |
| 58 | + RJson *schema = r_json_parse (schema_copy); |
| 59 | + if (!schema || schema->type != R_JSON_OBJECT) { |
| 60 | + free (schema_copy); |
| 61 | + return NULL; |
| 62 | + } |
| 63 | + |
| 64 | + RList *required = NULL; |
| 65 | + const RJson *required_json = r_json_get (schema, "required"); |
| 66 | + if (required_json && required_json->type == R_JSON_ARRAY) { |
| 67 | + required = r_list_newf (free); |
| 68 | + const RJson *item = required_json->children.first; |
| 69 | + while (item) { |
| 70 | + if (item->type == R_JSON_STRING && item->str_value) { |
| 71 | + r_list_append (required, strdup (item->str_value)); |
| 72 | + } |
| 73 | + item = item->next; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + r_json_free (schema); |
| 78 | + free (schema_copy); |
| 79 | + return required; |
| 80 | +} |
| 81 | + |
| 82 | +/* Validate arguments against JSON Schema */ |
| 83 | +ValidationResult validate_arguments(RJson *args, const char *schema_json) { |
| 84 | + if (!schema_json || !*schema_json) { |
| 85 | + /* No schema defined - allow all arguments */ |
| 86 | + return (ValidationResult){ true, NULL }; |
| 87 | + } |
| 88 | + |
| 89 | + /* Parse schema to get required properties */ |
| 90 | + RList *required = parse_required_properties (schema_json); |
| 91 | + if (!required) { |
| 92 | + /* Schema couldn't be parsed - allow all arguments */ |
| 93 | + return (ValidationResult){ true, NULL }; |
| 94 | + } |
| 95 | + |
| 96 | + /* Check each required parameter exists */ |
| 97 | + RListIter *it; |
| 98 | + const char *param; |
| 99 | + r_list_foreach (required, it, param) { |
| 100 | + const RJson *field = r_json_get (args, param); |
| 101 | + if (!field) { |
| 102 | + char *msg = r_str_newf ("Missing required parameter '%s'", param); |
| 103 | + r_list_free (required); |
| 104 | + return (ValidationResult){ false, msg }; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + r_list_free (required); |
| 109 | + return (ValidationResult){ true, NULL }; |
| 110 | +} |
0 commit comments