spikard-core 0.15.3

Shared transport-agnostic primitives for Spikard runtimes
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Table-driven error mapping for JSON Schema validation failures
//!
//! This module provides a structured approach to mapping JSON Schema validation errors
//! to consistent error codes and messages. Instead of massive if-else chains, we use
//! enum variants and pattern matching for maintainability and testability.

use serde_json::Value;

/// Represents the different types of validation errors that can occur
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ErrorCondition {
    /// String length is below minimum
    StringTooShort { min_length: Option<u64> },
    /// String length exceeds maximum
    StringTooLong { max_length: Option<u64> },
    /// Number is not greater than exclusive minimum
    GreaterThan { value: Option<i64> },
    /// Number is not greater than or equal to minimum
    GreaterThanEqual { value: Option<i64> },
    /// Number is not less than exclusive maximum
    LessThan { value: Option<i64> },
    /// Number is not less than or equal to maximum
    LessThanEqual { value: Option<i64> },
    /// Value is not one of allowed enum values
    Enum { values: Option<Vec<String>> },
    /// String does not match pattern
    StringPatternMismatch { pattern: Option<String> },
    /// Email format validation failed
    EmailFormat,
    /// UUID format validation failed
    UuidFormat,
    /// Date-time format validation failed
    DatetimeFormat,
    /// Date format validation failed
    DateFormat,
    /// Other format validation failed
    FormatError,
    /// Type mismatch
    TypeMismatch { expected_type: String },
    /// Required field is missing
    Missing,
    /// Additional properties not allowed
    AdditionalProperties { field: String },
    /// Array has too few items
    TooFewItems { min_items: Option<usize> },
    /// Array has too many items
    TooManyItems,
    /// Fallback for unmapped errors
    #[default]
    ValidationError,
}

impl ErrorCondition {
    /// Determine the error condition from schema path and error message
    #[must_use]
    #[allow(clippy::ignored_unit_patterns)]
    pub fn from_schema_error(schema_path_str: &str, error_msg: &str) -> Self {
        match () {
            () if schema_path_str.contains("minLength") => Self::StringTooShort { min_length: None },
            () if schema_path_str.contains("maxLength") => Self::StringTooLong { max_length: None },
            () if schema_path_str.contains("exclusiveMinimum")
                || (error_msg.contains("less than or equal to") && error_msg.contains("minimum")) =>
            {
                Self::GreaterThan { value: None }
            }
            () if schema_path_str.contains("minimum") || error_msg.contains("less than the minimum") => {
                Self::GreaterThanEqual { value: None }
            }
            () if schema_path_str.contains("exclusiveMaximum")
                || (error_msg.contains("greater than or equal to") && error_msg.contains("maximum")) =>
            {
                Self::LessThan { value: None }
            }
            () if schema_path_str.contains("maximum") || error_msg.contains("greater than the maximum") => {
                Self::LessThanEqual { value: None }
            }
            () if schema_path_str.contains("enum") || error_msg.contains("is not one of") => {
                Self::Enum { values: None }
            }
            () if schema_path_str.contains("pattern") || error_msg.contains("does not match") => {
                Self::StringPatternMismatch { pattern: None }
            }
            () if schema_path_str.contains("format") => {
                if error_msg.contains("email") {
                    Self::EmailFormat
                } else if error_msg.contains("uuid") {
                    Self::UuidFormat
                } else if error_msg.contains("date-time") {
                    Self::DatetimeFormat
                } else if error_msg.contains("date") {
                    Self::DateFormat
                } else {
                    Self::FormatError
                }
            }
            _ if schema_path_str.contains("/type") => Self::TypeMismatch {
                expected_type: "unknown".to_string(),
            },
            _ if schema_path_str.ends_with("/required") => Self::Missing,
            _ if schema_path_str.contains("/additionalProperties")
                || error_msg.contains("Additional properties are not allowed") =>
            {
                Self::AdditionalProperties { field: String::new() }
            }
            _ if schema_path_str.contains("/minItems") => Self::TooFewItems { min_items: None },
            _ if schema_path_str.contains("/maxItems") => Self::TooManyItems,
            _ => Self::ValidationError,
        }
    }

    /// Get the error type code for this condition
    #[must_use]
    pub const fn error_type(&self) -> &'static str {
        match self {
            Self::StringTooShort { .. } => "string_too_short",
            Self::StringTooLong { .. } => "string_too_long",
            Self::GreaterThan { .. } => "greater_than",
            Self::GreaterThanEqual { .. } => "greater_than_equal",
            Self::LessThan { .. } => "less_than",
            Self::LessThanEqual { .. } => "less_than_equal",
            Self::Enum { .. } => "enum",
            Self::StringPatternMismatch { .. } | Self::EmailFormat => "string_pattern_mismatch",
            Self::UuidFormat => "uuid_parsing",
            Self::DatetimeFormat => "datetime_parsing",
            Self::DateFormat => "date_parsing",
            Self::FormatError => "format_error",
            Self::TypeMismatch { .. } => "type_error",
            Self::Missing => "missing",
            Self::AdditionalProperties { .. } | Self::ValidationError => "validation_error",
            Self::TooFewItems { .. } => "too_short",
            Self::TooManyItems => "too_long",
        }
    }

    /// Get default message for this error condition
    #[must_use]
    pub const fn default_message(&self) -> &'static str {
        match self {
            Self::StringTooShort { .. } => "String is too short",
            Self::StringTooLong { .. } => "String is too long",
            Self::GreaterThan { .. } => "Input should be greater than the minimum",
            Self::GreaterThanEqual { .. } => "Input should be greater than or equal to the minimum",
            Self::LessThan { .. } => "Input should be less than the maximum",
            Self::LessThanEqual { .. } => "Input should be less than or equal to the maximum",
            Self::Enum { .. } => "Input should be one of the allowed values",
            Self::StringPatternMismatch { .. } => "String does not match expected pattern",
            Self::EmailFormat => "String should match email pattern",
            Self::UuidFormat => "Input should be a valid UUID",
            Self::DatetimeFormat => "Input should be a valid datetime",
            Self::DateFormat => "Input should be a valid date",
            Self::FormatError => "Invalid format",
            Self::TypeMismatch { .. } => "Invalid type",
            Self::Missing => "Field required",
            Self::AdditionalProperties { .. } => "Additional properties are not allowed",
            Self::TooFewItems { .. } => "List should have at least N items after validation",
            Self::TooManyItems => "List should have at most N items after validation",
            Self::ValidationError => "Validation error",
        }
    }
}

/// Maps validation conditions to error details with schema context
pub struct ErrorMapper;

impl ErrorMapper {
    /// Map an error condition to its type, message, and context
    ///
    /// # Panics
    /// Panics if accessing `.last()` on an empty vector for enum values extraction.
    #[must_use]
    #[allow(
        clippy::too_many_lines,
        clippy::option_if_let_else,
        clippy::redundant_closure_for_method_calls,
        clippy::uninlined_format_args
    )]
    pub fn map_error(
        condition: &ErrorCondition,
        schema: &Value,
        schema_prop_path: &str,
        generic_message: &str,
    ) -> (String, String, Option<Value>) {
        match condition {
            ErrorCondition::StringTooShort { .. } => {
                if let Some(min_len) = schema
                    .pointer(&format!("{}/minLength", schema_prop_path))
                    .and_then(|v| v.as_u64())
                {
                    let ctx = serde_json::json!({"min_length": min_len});
                    (
                        "string_too_short".to_string(),
                        format!("String should have at least {} characters", min_len),
                        Some(ctx),
                    )
                } else {
                    ("string_too_short".to_string(), "String is too short".to_string(), None)
                }
            }
            ErrorCondition::StringTooLong { .. } => {
                if let Some(max_len) = schema
                    .pointer(&format!("{}/maxLength", schema_prop_path))
                    .and_then(|v| v.as_u64())
                {
                    let ctx = serde_json::json!({"max_length": max_len});
                    (
                        "string_too_long".to_string(),
                        format!("String should have at most {} characters", max_len),
                        Some(ctx),
                    )
                } else {
                    ("string_too_long".to_string(), "String is too long".to_string(), None)
                }
            }
            ErrorCondition::GreaterThan { .. } => {
                if let Some(min_val) = schema
                    .pointer(&format!("{}/exclusiveMinimum", schema_prop_path))
                    .and_then(|v| v.as_i64())
                {
                    let ctx = serde_json::json!({"gt": min_val});
                    (
                        "greater_than".to_string(),
                        format!("Input should be greater than {}", min_val),
                        Some(ctx),
                    )
                } else {
                    (
                        "greater_than".to_string(),
                        "Input should be greater than the minimum".to_string(),
                        None,
                    )
                }
            }
            ErrorCondition::GreaterThanEqual { .. } => {
                if let Some(min_val) = schema
                    .pointer(&format!("{}/minimum", schema_prop_path))
                    .and_then(|v| v.as_i64())
                {
                    let ctx = serde_json::json!({"ge": min_val});
                    (
                        "greater_than_equal".to_string(),
                        format!("Input should be greater than or equal to {}", min_val),
                        Some(ctx),
                    )
                } else {
                    (
                        "greater_than_equal".to_string(),
                        "Input should be greater than or equal to the minimum".to_string(),
                        None,
                    )
                }
            }
            ErrorCondition::LessThan { .. } => {
                if let Some(max_val) = schema
                    .pointer(&format!("{}/exclusiveMaximum", schema_prop_path))
                    .and_then(|v| v.as_i64())
                {
                    let ctx = serde_json::json!({"lt": max_val});
                    (
                        "less_than".to_string(),
                        format!("Input should be less than {}", max_val),
                        Some(ctx),
                    )
                } else {
                    (
                        "less_than".to_string(),
                        "Input should be less than the maximum".to_string(),
                        None,
                    )
                }
            }
            ErrorCondition::LessThanEqual { .. } => {
                if let Some(max_val) = schema
                    .pointer(&format!("{}/maximum", schema_prop_path))
                    .and_then(|v| v.as_i64())
                {
                    let ctx = serde_json::json!({"le": max_val});
                    (
                        "less_than_equal".to_string(),
                        format!("Input should be less than or equal to {}", max_val),
                        Some(ctx),
                    )
                } else {
                    (
                        "less_than_equal".to_string(),
                        "Input should be less than or equal to the maximum".to_string(),
                        None,
                    )
                }
            }
            ErrorCondition::Enum { .. } => {
                if let Some(enum_values) = schema
                    .pointer(&format!("{}/enum", schema_prop_path))
                    .and_then(|v| v.as_array())
                {
                    let values: Vec<String> = enum_values
                        .iter()
                        .filter_map(|v| v.as_str().map(|s| format!("'{}'", s)))
                        .collect();

                    let msg = if values.len() > 1 {
                        let last = values.last().unwrap();
                        let rest = &values[..values.len() - 1];
                        format!("Input should be {} or {}", rest.join(", "), last)
                    } else if !values.is_empty() {
                        format!("Input should be {}", values[0])
                    } else {
                        "Input should be one of the allowed values".to_string()
                    };

                    let expected_str = if values.len() > 1 {
                        let last = values.last().unwrap();
                        let rest = &values[..values.len() - 1];
                        format!("{} or {}", rest.join(", "), last)
                    } else if !values.is_empty() {
                        values[0].clone()
                    } else {
                        "allowed values".to_string()
                    };
                    let ctx = serde_json::json!({"expected": expected_str});
                    ("enum".to_string(), msg, Some(ctx))
                } else {
                    (
                        "enum".to_string(),
                        "Input should be one of the allowed values".to_string(),
                        None,
                    )
                }
            }
            ErrorCondition::StringPatternMismatch { .. } => {
                if let Some(pattern) = schema
                    .pointer(&format!("{}/pattern", schema_prop_path))
                    .and_then(|v| v.as_str())
                {
                    let ctx = serde_json::json!({"pattern": pattern});
                    let msg = format!("String should match pattern '{}'", pattern);
                    ("string_pattern_mismatch".to_string(), msg, Some(ctx))
                } else {
                    (
                        "string_pattern_mismatch".to_string(),
                        "String does not match expected pattern".to_string(),
                        None,
                    )
                }
            }
            ErrorCondition::EmailFormat => {
                let email_pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
                let ctx = serde_json::json!({"pattern": email_pattern});
                (
                    "string_pattern_mismatch".to_string(),
                    format!("String should match pattern '{}'", email_pattern),
                    Some(ctx),
                )
            }
            ErrorCondition::UuidFormat => (
                "uuid_parsing".to_string(),
                "Input should be a valid UUID".to_string(),
                None,
            ),
            ErrorCondition::DatetimeFormat => (
                "datetime_parsing".to_string(),
                "Input should be a valid datetime".to_string(),
                None,
            ),
            ErrorCondition::DateFormat => (
                "date_parsing".to_string(),
                "Input should be a valid date".to_string(),
                None,
            ),
            ErrorCondition::FormatError => ("format_error".to_string(), generic_message.to_string(), None),
            ErrorCondition::TypeMismatch { expected_type } => {
                let (error_type, msg) = match expected_type.as_str() {
                    "integer" => (
                        "int_parsing".to_string(),
                        "Input should be a valid integer, unable to parse string as an integer".to_string(),
                    ),
                    "number" => (
                        "float_parsing".to_string(),
                        "Input should be a valid number, unable to parse string as a number".to_string(),
                    ),
                    "boolean" => (
                        "bool_parsing".to_string(),
                        "Input should be a valid boolean".to_string(),
                    ),
                    "string" => ("string_type".to_string(), "Input should be a valid string".to_string()),
                    _ => (
                        "type_error".to_string(),
                        format!("Input should be a valid {}", expected_type),
                    ),
                };
                (error_type, msg, None)
            }
            ErrorCondition::Missing => ("missing".to_string(), "Field required".to_string(), None),
            ErrorCondition::AdditionalProperties { field } => {
                let ctx = serde_json::json!({
                    "additional_properties": false,
                    "unexpected_field": field
                });
                (
                    "validation_error".to_string(),
                    "Additional properties are not allowed".to_string(),
                    Some(ctx),
                )
            }
            ErrorCondition::TooFewItems { min_items } => {
                let min = schema
                    .pointer(&format!("{}/minItems", schema_prop_path))
                    .and_then(|v| v.as_u64())
                    .or_else(|| min_items.map(|v| v as u64))
                    .unwrap_or(1);
                let ctx = serde_json::json!({
                    "min_length": min
                });
                (
                    "too_short".to_string(),
                    format!("List should have at least {} item after validation", min),
                    Some(ctx),
                )
            }
            ErrorCondition::TooManyItems => {
                let max = schema
                    .pointer(&format!("{}/maxItems", schema_prop_path))
                    .and_then(|v| v.as_u64())
                    .unwrap_or(1);
                let ctx = serde_json::json!({
                    "max_length": max
                });
                (
                    "too_long".to_string(),
                    format!("List should have at most {} items after validation", max),
                    Some(ctx),
                )
            }
            ErrorCondition::ValidationError => ("validation_error".to_string(), generic_message.to_string(), None),
        }
    }
}

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

    #[test]
    fn test_string_too_short_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/minLength", "");
        assert_eq!(condition, ErrorCondition::StringTooShort { min_length: None });
    }

    #[test]
    fn test_string_too_long_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/maxLength", "");
        assert_eq!(condition, ErrorCondition::StringTooLong { max_length: None });
    }

    #[test]
    fn test_minimum_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/minimum", "");
        assert_eq!(condition, ErrorCondition::GreaterThanEqual { value: None });
    }

    #[test]
    fn test_exclusive_minimum_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/exclusiveMinimum", "");
        assert_eq!(condition, ErrorCondition::GreaterThan { value: None });
    }

    #[test]
    fn test_maximum_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/maximum", "");
        assert_eq!(condition, ErrorCondition::LessThanEqual { value: None });
    }

    #[test]
    fn test_exclusive_maximum_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/exclusiveMaximum", "");
        assert_eq!(condition, ErrorCondition::LessThan { value: None });
    }

    #[test]
    fn test_enum_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/enum", "");
        assert_eq!(condition, ErrorCondition::Enum { values: None });
    }

    #[test]
    fn test_pattern_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/pattern", "");
        assert_eq!(condition, ErrorCondition::StringPatternMismatch { pattern: None });
    }

    #[test]
    fn test_email_format_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/format", "email");
        assert_eq!(condition, ErrorCondition::EmailFormat);
    }

    #[test]
    fn test_uuid_format_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/format", "uuid");
        assert_eq!(condition, ErrorCondition::UuidFormat);
    }

    #[test]
    fn test_datetime_format_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/format", "date-time");
        assert_eq!(condition, ErrorCondition::DatetimeFormat);
    }

    #[test]
    fn test_date_format_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/format", "date");
        assert_eq!(condition, ErrorCondition::DateFormat);
    }

    #[test]
    fn test_type_error_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/type", "");
        assert!(matches!(condition, ErrorCondition::TypeMismatch { .. }));
    }

    #[test]
    fn test_missing_field_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/required", "");
        assert_eq!(condition, ErrorCondition::Missing);
    }

    #[test]
    fn test_additional_properties_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/additionalProperties", "");
        assert!(matches!(condition, ErrorCondition::AdditionalProperties { .. }));
    }

    #[test]
    fn test_min_items_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/minItems", "");
        assert!(matches!(condition, ErrorCondition::TooFewItems { .. }));
    }

    #[test]
    fn test_max_items_detection() {
        let condition = ErrorCondition::from_schema_error("some/path/maxItems", "");
        assert_eq!(condition, ErrorCondition::TooManyItems);
    }

    #[test]
    fn test_error_type_codes() {
        assert_eq!(
            ErrorCondition::StringTooShort { min_length: None }.error_type(),
            "string_too_short"
        );
        assert_eq!(
            ErrorCondition::StringTooLong { max_length: None }.error_type(),
            "string_too_long"
        );
        assert_eq!(ErrorCondition::GreaterThan { value: None }.error_type(), "greater_than");
        assert_eq!(
            ErrorCondition::GreaterThanEqual { value: None }.error_type(),
            "greater_than_equal"
        );
        assert_eq!(ErrorCondition::LessThan { value: None }.error_type(), "less_than");
        assert_eq!(
            ErrorCondition::LessThanEqual { value: None }.error_type(),
            "less_than_equal"
        );
        assert_eq!(ErrorCondition::Enum { values: None }.error_type(), "enum");
        assert_eq!(
            ErrorCondition::StringPatternMismatch { pattern: None }.error_type(),
            "string_pattern_mismatch"
        );
        assert_eq!(ErrorCondition::EmailFormat.error_type(), "string_pattern_mismatch");
        assert_eq!(ErrorCondition::UuidFormat.error_type(), "uuid_parsing");
        assert_eq!(ErrorCondition::DatetimeFormat.error_type(), "datetime_parsing");
        assert_eq!(ErrorCondition::DateFormat.error_type(), "date_parsing");
        assert_eq!(ErrorCondition::FormatError.error_type(), "format_error");
        assert_eq!(
            ErrorCondition::TypeMismatch {
                expected_type: "integer".to_string()
            }
            .error_type(),
            "type_error"
        );
        assert_eq!(ErrorCondition::Missing.error_type(), "missing");
        assert_eq!(
            ErrorCondition::AdditionalProperties {
                field: "extra".to_string()
            }
            .error_type(),
            "validation_error"
        );
        assert_eq!(
            ErrorCondition::TooFewItems { min_items: None }.error_type(),
            "too_short"
        );
        assert_eq!(ErrorCondition::TooManyItems.error_type(), "too_long");
    }

    #[test]
    fn test_mapper_string_length_constraints() {
        let schema = json!({
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 5,
                    "maxLength": 20
                }
            }
        });

        let condition = ErrorCondition::StringTooShort { min_length: None };
        let (error_type, msg, ctx_result) = ErrorMapper::map_error(&condition, &schema, "/properties/name", "");
        assert_eq!(error_type, "string_too_short");
        assert_eq!(msg, "String should have at least 5 characters");
        assert_eq!(ctx_result, Some(json!({"min_length": 5})));
    }

    #[test]
    fn test_mapper_numeric_constraints() {
        let schema = json!({
            "properties": {
                "age": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 150,
                    "exclusiveMinimum": -1,
                    "exclusiveMaximum": 151
                }
            }
        });

        let condition = ErrorCondition::GreaterThanEqual { value: None };
        let (error_type, msg, ctx) = ErrorMapper::map_error(&condition, &schema, "/properties/age", "");
        assert_eq!(error_type, "greater_than_equal");
        assert_eq!(msg, "Input should be greater than or equal to 0");
        assert_eq!(ctx, Some(json!({"ge": 0})));
    }

    #[test]
    fn test_mapper_enum() {
        let schema = json!({
            "properties": {
                "status": {
                    "type": "string",
                    "enum": ["active", "inactive", "pending"]
                }
            }
        });

        let condition = ErrorCondition::Enum { values: None };
        let (error_type, msg, _ctx) = ErrorMapper::map_error(&condition, &schema, "/properties/status", "");
        assert_eq!(error_type, "enum");
        assert!(msg.contains("'active'"));
        assert!(msg.contains("'inactive'"));
        assert!(msg.contains("'pending'"));
    }

    #[test]
    fn test_mapper_type_mismatch() {
        let schema = json!({
            "properties": {
                "count": { "type": "integer" }
            }
        });

        let condition = ErrorCondition::TypeMismatch {
            expected_type: "integer".to_string(),
        };
        let (error_type, msg, _) = ErrorMapper::map_error(&condition, &schema, "/properties/count", "");
        assert_eq!(error_type, "int_parsing");
        assert!(msg.contains("integer"));
    }

    #[test]
    fn test_mapper_email_format() {
        let schema = json!({});

        let condition = ErrorCondition::EmailFormat;
        let (error_type, msg, ctx) = ErrorMapper::map_error(&condition, &schema, "", "");
        assert_eq!(error_type, "string_pattern_mismatch");
        assert!(msg.contains('@'));
        assert!(ctx.is_some());
    }

    #[test]
    fn test_mapper_uuid_format() {
        let schema = json!({});

        let condition = ErrorCondition::UuidFormat;
        let (error_type, msg, _) = ErrorMapper::map_error(&condition, &schema, "", "");
        assert_eq!(error_type, "uuid_parsing");
        assert_eq!(msg, "Input should be a valid UUID");
    }

    #[test]
    fn test_mapper_additional_properties() {
        let schema = json!({});

        let condition = ErrorCondition::AdditionalProperties {
            field: "extra_field".to_string(),
        };
        let (error_type, msg, ctx) = ErrorMapper::map_error(&condition, &schema, "", "");
        assert_eq!(error_type, "validation_error");
        assert_eq!(msg, "Additional properties are not allowed");
        assert_eq!(
            ctx,
            Some(json!({
                "additional_properties": false,
                "unexpected_field": "extra_field"
            }))
        );
    }
}