openrouter_api 0.6.0

A Rust client library for the OpenRouter API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Common validation utilities used across all API endpoints

use crate::error::{Error, Result};
use std::collections::HashSet;

/// Validates a required string field
pub fn validate_required_string<'a>(
    value: &'a Option<String>,
    field_name: &str,
) -> Result<&'a String> {
    value
        .as_ref()
        .ok_or_else(|| Error::ConfigError(format!("Required field '{}' is missing", field_name)))
}

/// Validates a string field that must not be empty after trimming
pub fn validate_non_empty_string(value: &str, field_name: &str) -> Result<()> {
    if value.trim().is_empty() {
        return Err(Error::ConfigError(format!(
            "Field '{}' cannot be empty",
            field_name
        )));
    }
    Ok(())
}

/// Validates string length bounds
pub fn validate_string_length(value: &str, field_name: &str, min: usize, max: usize) -> Result<()> {
    if value.len() < min {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be at least {} characters",
            field_name, min
        )));
    }
    if value.len() > max {
        return Err(Error::ConfigError(format!(
            "Field '{}' must not exceed {} characters",
            field_name, max
        )));
    }
    Ok(())
}

/// Validates numeric range for any comparable numeric type
pub fn validate_numeric_range<T>(value: T, field_name: &str, min: T, max: T) -> Result<()>
where
    T: PartialOrd + std::fmt::Display,
{
    if value < min || value > max {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be between {} and {}",
            field_name, min, max
        )));
    }
    Ok(())
}

/// Validates that a numeric value is >= minimum
pub fn validate_numeric_min<T>(value: T, field_name: &str, min: T) -> Result<()>
where
    T: PartialOrd + std::fmt::Display,
{
    if value < min {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be at least {}",
            field_name, min
        )));
    }
    Ok(())
}

/// Validates that a numeric value is <= maximum
pub fn validate_numeric_max<T>(value: T, field_name: &str, max: T) -> Result<()>
where
    T: PartialOrd + std::fmt::Display,
{
    if value > max {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be at most {}",
            field_name, max
        )));
    }
    Ok(())
}

/// Validates URL format
pub fn validate_url(url: &str, field_name: &str) -> Result<()> {
    url::Url::parse(url)
        .map_err(|_| Error::ConfigError(format!("Field '{}' must be a valid URL", field_name)))?;
    Ok(())
}

/// Validates that a URL uses specific schemes
pub fn validate_url_scheme(url: &str, field_name: &str, allowed_schemes: &[&str]) -> Result<()> {
    let parsed = url::Url::parse(url)
        .map_err(|_| Error::ConfigError(format!("Field '{}' must be a valid URL", field_name)))?;

    if !allowed_schemes.contains(&parsed.scheme()) {
        return Err(Error::ConfigError(format!(
            "Field '{}' must use one of these schemes: {}",
            field_name,
            allowed_schemes.join(", ")
        )));
    }

    Ok(())
}

/// Validates date format (YYYY-MM-DD)
pub fn validate_date_format(date: &str, field_name: &str) -> Result<()> {
    if date.len() != 10 {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be in YYYY-MM-DD format",
            field_name
        )));
    }

    // Basic format validation
    if date.chars().nth(4) != Some('-') || date.chars().nth(7) != Some('-') {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be in YYYY-MM-DD format",
            field_name
        )));
    }

    // Try to parse as date
    chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d").map_err(|_| {
        Error::ConfigError(format!(
            "Field '{}' must be a valid date in YYYY-MM-DD format",
            field_name
        ))
    })?;

    Ok(())
}

/// Validates date range (start_date <= end_date)
pub fn validate_date_range(start_date: &str, end_date: &str) -> Result<()> {
    let start = chrono::NaiveDate::parse_from_str(start_date, "%Y-%m-%d")
        .map_err(|_| Error::ConfigError("Invalid start_date format. Use YYYY-MM-DD".to_string()))?;

    let end = chrono::NaiveDate::parse_from_str(end_date, "%Y-%m-%d")
        .map_err(|_| Error::ConfigError("Invalid end_date format. Use YYYY-MM-DD".to_string()))?;

    if start > end {
        return Err(Error::ConfigError(
            "start_date cannot be after end_date".to_string(),
        ));
    }

    Ok(())
}

/// Validates that a field is one of allowed values
pub fn validate_enum_value<T: AsRef<str>>(
    value: T,
    field_name: &str,
    allowed_values: &[&str],
) -> Result<()> {
    let value_str = value.as_ref();
    if !allowed_values.contains(&value_str) {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be one of: {}",
            field_name,
            allowed_values.join(", ")
        )));
    }
    Ok(())
}

/// Validates that a collection is not empty
pub fn validate_non_empty_collection<T>(collection: &[T], field_name: &str) -> Result<()> {
    if collection.is_empty() {
        return Err(Error::ConfigError(format!(
            "Field '{}' cannot be empty",
            field_name
        )));
    }
    Ok(())
}

/// Validates collection size bounds
pub fn validate_collection_size<T>(
    collection: &[T],
    field_name: &str,
    min: usize,
    max: usize,
) -> Result<()> {
    if collection.len() < min {
        return Err(Error::ConfigError(format!(
            "Field '{}' must contain at least {} items",
            field_name, min
        )));
    }
    if collection.len() > max {
        return Err(Error::ConfigError(format!(
            "Field '{}' must contain at most {} items",
            field_name, max
        )));
    }
    Ok(())
}

/// Validates that all items in a collection are unique
pub fn validate_unique_items<T: std::hash::Hash + Eq + std::fmt::Display>(
    items: &[T],
    field_name: &str,
) -> Result<()> {
    let mut seen = HashSet::new();

    for (index, item) in items.iter().enumerate() {
        if !seen.insert(item) {
            return Err(Error::ConfigError(format!(
                "Duplicate item '{}' found in field '{}' at index {}",
                item, field_name, index
            )));
        }
    }

    Ok(())
}

/// Validates JSON object structure
pub fn validate_json_object(value: &serde_json::Value, field_name: &str) -> Result<()> {
    if !value.is_object() {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be a JSON object",
            field_name
        )));
    }
    Ok(())
}

/// Validates that a value is a specific JSON type
pub fn validate_json_type(
    value: &serde_json::Value,
    field_name: &str,
    expected_type: &str,
) -> Result<()> {
    let is_valid = match expected_type {
        "string" => value.is_string(),
        "number" => value.is_number(),
        "integer" => value.is_i64() || value.is_u64(),
        "boolean" => value.is_boolean(),
        "array" => value.is_array(),
        "object" => value.is_object(),
        "null" => value.is_null(),
        _ => false,
    };

    if !is_valid {
        return Err(Error::ConfigError(format!(
            "Field '{}' must be of type {}",
            field_name, expected_type
        )));
    }

    Ok(())
}

/// Validates optional numeric parameter from JSON object
pub fn validate_optional_numeric_param(
    params: &serde_json::Map<String, serde_json::Value>,
    key: &str,
    min: f64,
    max: f64,
) -> Result<()> {
    if let Some(value) = params.get(key) {
        if let Some(num) = value.as_f64() {
            validate_numeric_range(num, key, min, max)?;
        } else {
            return Err(Error::ConfigError(format!(
                "Parameter '{}' must be a number",
                key
            )));
        }
    }
    Ok(())
}

/// Validates optional integer parameter from JSON object
pub fn validate_optional_integer_param(
    params: &serde_json::Map<String, serde_json::Value>,
    key: &str,
    min: i64,
    max: i64,
) -> Result<()> {
    if let Some(value) = params.get(key) {
        if let Some(num) = value.as_i64() {
            validate_numeric_range(num, key, min, max)?;
        } else {
            return Err(Error::ConfigError(format!(
                "Parameter '{}' must be an integer",
                key
            )));
        }
    }
    Ok(())
}

/// Validates optional string parameter from JSON object
pub fn validate_optional_string_param(
    params: &serde_json::Map<String, serde_json::Value>,
    key: &str,
    min_length: usize,
    max_length: usize,
) -> Result<()> {
    if let Some(value) = params.get(key) {
        if let Some(s) = value.as_str() {
            validate_string_length(s, key, min_length, max_length)?;
        } else {
            return Err(Error::ConfigError(format!(
                "Parameter '{}' must be a string",
                key
            )));
        }
    }
    Ok(())
}

/// Validates sampling parameters commonly used across APIs
pub fn validate_sampling_parameters(
    temperature: Option<f64>,
    top_p: Option<f64>,
    top_k: Option<u32>,
    frequency_penalty: Option<f64>,
    presence_penalty: Option<f64>,
) -> Result<()> {
    // Temperature: [0.0, 2.0]
    if let Some(temp) = temperature {
        validate_numeric_range(temp, "temperature", 0.0, 2.0)?;
    }

    // Top P: (0.0, 1.0]
    if let Some(top_p_val) = top_p {
        if top_p_val <= 0.0 || top_p_val > 1.0 {
            return Err(Error::ConfigError(format!(
                "Top P must be between 0.0 (exclusive) and 1.0 (inclusive), got {}",
                top_p_val
            )));
        }
    }

    // Top K: [1, ∞) or 0 (disabled)
    if let Some(top_k_val) = top_k {
        if top_k_val != 0 && top_k_val < 1 {
            return Err(Error::ConfigError(format!(
                "Top K must be 0 (disabled) or >= 1, got {}",
                top_k_val
            )));
        }
    }

    // Frequency Penalty: [-2.0, 2.0]
    if let Some(fp) = frequency_penalty {
        validate_numeric_range(fp, "frequency_penalty", -2.0, 2.0)?;
    }

    // Presence Penalty: [-2.0, 2.0]
    if let Some(pp) = presence_penalty {
        validate_numeric_range(pp, "presence_penalty", -2.0, 2.0)?;
    }

    Ok(())
}

/// Validates model identifier format
pub fn validate_model_id(model: &str) -> Result<()> {
    validate_non_empty_string(model, "model")?;
    validate_string_length(model, "model", 1, 200)?;

    // Basic format validation - should contain provider/model format
    if !model.contains('/') {
        return Err(Error::ConfigError(
            "Model ID should be in format 'provider/model' (e.g., 'openai/gpt-4')".to_string(),
        ));
    }

    Ok(())
}

/// Validates that a field value matches a regex pattern
pub fn validate_regex_pattern(value: &str, field_name: &str, pattern: &str) -> Result<()> {
    let regex = regex::Regex::new(pattern).map_err(|e| {
        Error::ConfigError(format!(
            "Invalid regex pattern for field '{}': {}",
            field_name, e
        ))
    })?;

    if !regex.is_match(value) {
        return Err(Error::ConfigError(format!(
            "Field '{}' does not match required pattern",
            field_name
        )));
    }

    Ok(())
}

/// Validates that all strings in a collection are non-empty after trimming
pub fn validate_non_empty_strings(strings: &[String], field_name: &str) -> Result<()> {
    for (index, s) in strings.iter().enumerate() {
        if s.trim().is_empty() {
            return Err(Error::ConfigError(format!(
                "String at index {} in field '{}' cannot be empty",
                index, field_name
            )));
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_non_empty_string() {
        assert!(validate_non_empty_string("hello", "test").is_ok());
        assert!(validate_non_empty_string("  hello  ", "test").is_ok());
        assert!(validate_non_empty_string("", "test").is_err());
        assert!(validate_non_empty_string("   ", "test").is_err());
    }

    #[test]
    fn test_validate_string_length() {
        assert!(validate_string_length("hello", "test", 1, 10).is_ok());
        assert!(validate_string_length("hello", "test", 5, 10).is_ok());
        assert!(validate_string_length("hello", "test", 6, 10).is_err()); // too short
        assert!(validate_string_length("hello world", "test", 1, 5).is_err()); // too long
    }

    #[test]
    fn test_validate_numeric_range() {
        assert!(validate_numeric_range(5, "test", 1, 10).is_ok());
        assert!(validate_numeric_range(1, "test", 1, 10).is_ok());
        assert!(validate_numeric_range(10, "test", 1, 10).is_ok());
        assert!(validate_numeric_range(0, "test", 1, 10).is_err()); // too low
        assert!(validate_numeric_range(11, "test", 1, 10).is_err()); // too high
    }

    #[test]
    fn test_validate_url() {
        assert!(validate_url("https://example.com", "test").is_ok());
        assert!(validate_url("http://example.com", "test").is_ok());
        assert!(validate_url("not-a-url", "test").is_err());
    }

    #[test]
    fn test_validate_date_format() {
        assert!(validate_date_format("2024-01-15", "test").is_ok());
        assert!(validate_date_format("2024-13-15", "test").is_err()); // invalid month
        assert!(validate_date_format("2024-01-32", "test").is_err()); // invalid day
        assert!(validate_date_format("24-01-15", "test").is_err()); // wrong format
    }

    #[test]
    fn test_validate_enum_value() {
        let allowed = ["user", "assistant", "system"];
        assert!(validate_enum_value("user", "test", &allowed).is_ok());
        assert!(validate_enum_value("invalid", "test", &allowed).is_err());
    }

    #[test]
    fn test_validate_sampling_parameters() {
        // Valid parameters
        assert!(
            validate_sampling_parameters(Some(0.7), Some(0.9), Some(40), Some(0.5), Some(0.3))
                .is_ok()
        );

        // Invalid temperature
        assert!(validate_sampling_parameters(Some(3.0), None, None, None, None).is_err());

        // Invalid top_p
        assert!(validate_sampling_parameters(None, Some(0.0), None, None, None).is_err());
    }

    #[test]
    fn test_validate_model_id() {
        assert!(validate_model_id("openai/gpt-4").is_ok());
        assert!(validate_model_id("anthropic/claude-3").is_ok());
        assert!(validate_model_id("invalid-model").is_err()); // missing slash
        assert!(validate_model_id("").is_err()); // empty
    }
}