domainstack-wasm 1.0.0

WASM browser validation for domainstack: run the same validation logic in browser and server
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
//! # domainstack-wasm
//!
//! WASM browser validation for domainstack. Run the same validation logic
//! in both browser and server.
//!
//! ## Quick Start
//!
//! ```javascript
//! import { createValidator } from '@domainstack/wasm';
//!
//! const validator = await createValidator();
//! const result = validator.validate('Booking', {
//!   guest_email: 'invalid',
//!   rooms: 15
//! });
//!
//! if (!result.ok) {
//!   result.errors?.forEach(e => console.log(e.path, e.message));
//! }
//! ```
//!
//! ## Architecture
//!
//! This crate provides a thin WASM layer over domainstack validation.
//! Types are registered at compile time, and validation is dispatched
//! by type name at runtime.

use serde::Serialize;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;

// Re-export for use in proc macro generated code
pub use domainstack::{Validate, ValidationError};
pub use serde::de::DeserializeOwned;

/// Initialize panic hook for better error messages in browser console.
/// Call this once at application startup.
#[wasm_bindgen(start)]
pub fn init() {
    #[cfg(feature = "console_error_panic_hook")]
    console_error_panic_hook::set_once();
}

// ============================================================================
// WASM-Serializable Types
// ============================================================================

/// Result of a validation operation.
///
/// This is the ONLY type returned to JavaScript. No nulls, no exceptions.
#[derive(Debug, Clone, Serialize)]
pub struct ValidationResult {
    /// Whether validation passed
    pub ok: bool,

    /// Validation violations (present when ok=false and validation failed)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub errors: Option<Vec<WasmViolation>>,

    /// System error (present when ok=false and a system error occurred)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<SystemError>,
}

impl ValidationResult {
    /// Create a successful result
    pub fn success() -> Self {
        Self {
            ok: true,
            errors: None,
            error: None,
        }
    }

    /// Create a validation failure result
    pub fn validation_failed(violations: Vec<WasmViolation>) -> Self {
        Self {
            ok: false,
            errors: Some(violations),
            error: None,
        }
    }

    /// Create a system error result
    pub fn system_error(code: &'static str, message: String) -> Self {
        Self {
            ok: false,
            errors: None,
            error: Some(SystemError { code, message }),
        }
    }
}

/// A validation violation, serializable for WASM.
#[derive(Debug, Clone, Serialize)]
pub struct WasmViolation {
    /// Field path (e.g., "guest_email", "rooms\[0\].adults")
    pub path: String,

    /// Error code (e.g., "invalid_email", "out_of_range")
    pub code: String,

    /// Human-readable message
    pub message: String,

    /// Additional metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<HashMap<String, String>>,
}

impl From<&domainstack::Violation> for WasmViolation {
    fn from(v: &domainstack::Violation) -> Self {
        let meta = if v.meta.is_empty() {
            None
        } else {
            Some(
                v.meta
                    .iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect(),
            )
        };

        Self {
            path: v.path.to_string(),
            code: v.code.to_string(),
            message: v.message.clone(),
            meta,
        }
    }
}

/// System-level error (not a validation failure).
#[derive(Debug, Clone, Serialize)]
pub struct SystemError {
    /// Error code: "unknown_type" | "parse_error" | "internal_error"
    pub code: &'static str,

    /// Human-readable error message
    pub message: String,
}

// ============================================================================
// Dispatch Error
// ============================================================================

/// Internal error type for dispatch operations
pub enum DispatchError {
    /// Type name not found in registry
    UnknownType,

    /// JSON parsing failed
    ParseError(String),

    /// Validation failed (boxed to reduce enum size)
    Validation(Box<ValidationError>),
}

// ============================================================================
// Type Registry
// ============================================================================

/// Function signature for type validators
pub type ValidateFn = fn(&str) -> Result<(), DispatchError>;

/// Registry of type validators.
///
/// In production, this is populated by the `#[wasm_validate]` proc macro.
/// For now, users must manually register types.
pub struct TypeRegistry {
    validators: HashMap<&'static str, ValidateFn>,
}

impl TypeRegistry {
    /// Create an empty registry
    pub fn new() -> Self {
        Self {
            validators: HashMap::new(),
        }
    }

    /// Register a type validator
    pub fn register<T>(&mut self, type_name: &'static str)
    where
        T: Validate + DeserializeOwned + 'static,
    {
        self.validators.insert(type_name, validate_json::<T>);
    }

    /// Validate JSON against a registered type
    pub fn validate(&self, type_name: &str, json: &str) -> Result<(), DispatchError> {
        match self.validators.get(type_name) {
            Some(validate_fn) => validate_fn(json),
            None => Err(DispatchError::UnknownType),
        }
    }

    /// Check if a type is registered
    pub fn has_type(&self, type_name: &str) -> bool {
        self.validators.contains_key(type_name)
    }

    /// List all registered type names
    pub fn type_names(&self) -> Vec<&'static str> {
        self.validators.keys().copied().collect()
    }
}

impl Default for TypeRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Validate JSON for a specific type
fn validate_json<T>(json: &str) -> Result<(), DispatchError>
where
    T: Validate + DeserializeOwned,
{
    let value: T =
        serde_json::from_str(json).map_err(|e| DispatchError::ParseError(e.to_string()))?;
    value
        .validate()
        .map_err(|e| DispatchError::Validation(Box::new(e)))
}

// ============================================================================
// Global Registry
// ============================================================================

use std::cell::RefCell;

thread_local! {
    static REGISTRY: RefCell<TypeRegistry> = RefCell::new(TypeRegistry::new());
}

/// Register a type for WASM validation.
///
/// # Example
///
/// ```ignore
/// use domainstack::prelude::*;
/// use domainstack_wasm::register_type;
///
/// #[derive(Validate, Deserialize)]
/// struct Booking {
///     #[validate(email)]
///     guest_email: String,
/// }
///
/// // Call once at initialization
/// register_type::<Booking>("Booking");
/// ```
pub fn register_type<T>(type_name: &'static str)
where
    T: Validate + DeserializeOwned + 'static,
{
    REGISTRY.with(|r| r.borrow_mut().register::<T>(type_name));
}

/// Check if a type is registered
pub fn is_type_registered(type_name: &str) -> bool {
    REGISTRY.with(|r| r.borrow().has_type(type_name))
}

/// Get list of all registered type names
pub fn registered_types() -> Vec<&'static str> {
    REGISTRY.with(|r| r.borrow().type_names())
}

// ============================================================================
// WASM Entry Points
// ============================================================================

/// Validate JSON data against a registered type.
///
/// # Arguments
///
/// * `type_name` - The name of the type to validate against (e.g., "Booking")
/// * `json` - JSON string to validate
///
/// # Returns
///
/// A `ValidationResult` object (always, never throws):
/// - `{ ok: true }` - Validation passed
/// - `{ ok: false, errors: [...] }` - Validation failed
/// - `{ ok: false, error: { code, message } }` - System error
#[wasm_bindgen]
pub fn validate(type_name: &str, json: &str) -> JsValue {
    let result = REGISTRY.with(|r| r.borrow().validate(type_name, json));

    let validation_result = match result {
        Ok(()) => ValidationResult::success(),
        Err(DispatchError::UnknownType) => {
            ValidationResult::system_error("unknown_type", format!("Unknown type: {}", type_name))
        }
        Err(DispatchError::ParseError(msg)) => ValidationResult::system_error("parse_error", msg),
        Err(DispatchError::Validation(err)) => {
            let violations = err.violations.iter().map(WasmViolation::from).collect();
            ValidationResult::validation_failed(violations)
        }
    };

    // Serialize to JsValue, with fallback for serialization errors
    serde_wasm_bindgen::to_value(&validation_result).unwrap_or_else(|_| {
        let fallback = ValidationResult::system_error(
            "internal_error",
            "Failed to serialize validation result".to_string(),
        );
        serde_wasm_bindgen::to_value(&fallback).unwrap()
    })
}

/// Validate a JavaScript object against a registered type.
///
/// This is a convenience wrapper that accepts a JS object directly
/// instead of a JSON string.
///
/// # Arguments
///
/// * `type_name` - The name of the type to validate against
/// * `value` - JavaScript object to validate
#[wasm_bindgen]
pub fn validate_object(type_name: &str, value: JsValue) -> JsValue {
    // Serialize JS object to JSON string
    let json = match js_sys::JSON::stringify(&value) {
        Ok(s) => s.as_string().unwrap_or_default(),
        Err(_) => {
            let result = ValidationResult::system_error(
                "parse_error",
                "Failed to serialize JavaScript object to JSON".to_string(),
            );
            return serde_wasm_bindgen::to_value(&result).unwrap();
        }
    };

    validate(type_name, &json)
}

/// Get list of registered type names.
///
/// Useful for debugging and introspection.
#[wasm_bindgen]
pub fn get_registered_types() -> JsValue {
    let types = registered_types();
    serde_wasm_bindgen::to_value(&types).unwrap_or(JsValue::NULL)
}

/// Check if a type is registered.
#[wasm_bindgen]
pub fn has_type(type_name: &str) -> bool {
    is_type_registered(type_name)
}

// ============================================================================
// Builder Pattern for TypeScript
// ============================================================================

/// Validator instance for TypeScript ergonomics.
///
/// ```javascript
/// const validator = await createValidator();
/// const result = validator.validate('Booking', { ... });
/// ```
#[wasm_bindgen]
pub struct Validator {
    // Marker field - registry is global via thread_local
    _private: (),
}

#[wasm_bindgen]
impl Validator {
    /// Validate JSON string against a type
    pub fn validate(&self, type_name: &str, json: &str) -> JsValue {
        validate(type_name, json)
    }

    /// Validate JS object against a type
    #[wasm_bindgen(js_name = validateObject)]
    pub fn validate_object(&self, type_name: &str, value: JsValue) -> JsValue {
        validate_object(type_name, value)
    }

    /// Get registered type names
    #[wasm_bindgen(js_name = getTypes)]
    pub fn get_types(&self) -> JsValue {
        get_registered_types()
    }

    /// Check if type is registered
    #[wasm_bindgen(js_name = hasType)]
    pub fn has_type(&self, type_name: &str) -> bool {
        has_type(type_name)
    }
}

/// Create a validator instance.
///
/// This is the main entry point for TypeScript/JavaScript.
///
/// ```javascript
/// import { createValidator } from '@domainstack/wasm';
///
/// const validator = await createValidator();
/// ```
#[wasm_bindgen(js_name = createValidator)]
pub fn create_validator() -> Validator {
    Validator { _private: () }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_validation_result_success() {
        let result = ValidationResult::success();
        assert!(result.ok);
        assert!(result.errors.is_none());
        assert!(result.error.is_none());
    }

    #[test]
    fn test_validation_result_failure() {
        let violations = vec![WasmViolation {
            path: "email".to_string(),
            code: "invalid_email".to_string(),
            message: "Invalid email format".to_string(),
            meta: None,
        }];
        let result = ValidationResult::validation_failed(violations);
        assert!(!result.ok);
        assert!(result.errors.is_some());
        assert_eq!(result.errors.as_ref().unwrap().len(), 1);
        assert!(result.error.is_none());
    }

    #[test]
    fn test_validation_result_system_error() {
        let result =
            ValidationResult::system_error("unknown_type", "Unknown type: Foo".to_string());
        assert!(!result.ok);
        assert!(result.errors.is_none());
        assert!(result.error.is_some());
        assert_eq!(result.error.as_ref().unwrap().code, "unknown_type");
    }

    #[test]
    fn test_registry_unknown_type() {
        let registry = TypeRegistry::new();
        let result = registry.validate("NonExistent", "{}");
        assert!(matches!(result, Err(DispatchError::UnknownType)));
    }

    // Integration tests with actual Validate types
    mod integration {
        use super::*;
        use domainstack::Validate;
        use serde::Deserialize;

        #[derive(Debug, Validate, Deserialize)]
        struct TestBooking {
            #[validate(length(min = 1, max = 100))]
            guest_name: String,

            #[validate(range(min = 1, max = 10))]
            rooms: u8,
        }

        #[test]
        fn test_register_and_validate_success() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestBooking>("TestBooking");

            let json = r#"{"guest_name": "John Doe", "rooms": 2}"#;
            let result = registry.validate("TestBooking", json);
            assert!(result.is_ok());
        }

        #[test]
        fn test_register_and_validate_failure() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestBooking>("TestBooking");

            // rooms = 15 is out of range (max = 10)
            let json = r#"{"guest_name": "John", "rooms": 15}"#;
            let result = registry.validate("TestBooking", json);

            assert!(matches!(result, Err(DispatchError::Validation(_))));
            if let Err(DispatchError::Validation(err)) = result {
                assert!(!err.violations.is_empty());
                assert_eq!(err.violations[0].path.to_string(), "rooms");
            }
        }

        #[test]
        fn test_parse_error() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestBooking>("TestBooking");

            let json = r#"{"guest_name": "John", "rooms": "not a number"}"#;
            let result = registry.validate("TestBooking", json);

            assert!(matches!(result, Err(DispatchError::ParseError(_))));
        }

        #[test]
        fn test_wasm_violation_from_violation() {
            let violation = domainstack::Violation {
                path: domainstack::Path::from("email"),
                code: "invalid_email",
                message: "Invalid email format".to_string(),
                meta: domainstack::Meta::default(),
            };

            let wasm_violation = WasmViolation::from(&violation);
            assert_eq!(wasm_violation.path, "email");
            assert_eq!(wasm_violation.code, "invalid_email");
            assert_eq!(wasm_violation.message, "Invalid email format");
            assert!(wasm_violation.meta.is_none());
        }

        #[test]
        fn test_wasm_violation_with_meta() {
            let mut meta = domainstack::Meta::default();
            meta.insert("min", "1");
            meta.insert("max", "10");

            let violation = domainstack::Violation {
                path: domainstack::Path::from("age"),
                code: "out_of_range",
                message: "Must be between 1 and 10".to_string(),
                meta,
            };

            let wasm_violation = WasmViolation::from(&violation);
            assert!(wasm_violation.meta.is_some());
            let meta = wasm_violation.meta.unwrap();
            assert_eq!(meta.get("min"), Some(&"1".to_string()));
            assert_eq!(meta.get("max"), Some(&"10".to_string()));
        }

        #[test]
        fn test_multiple_violations() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestBooking>("TestBooking");

            // Both fields invalid
            let json = r#"{"guest_name": "", "rooms": 15}"#;
            let result = registry.validate("TestBooking", json);

            assert!(matches!(result, Err(DispatchError::Validation(_))));
            if let Err(DispatchError::Validation(err)) = result {
                assert_eq!(err.violations.len(), 2);
            }
        }

        #[test]
        fn test_empty_json_object() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestBooking>("TestBooking");

            // Missing required fields - should fail parse
            let json = r#"{}"#;
            let result = registry.validate("TestBooking", json);
            assert!(matches!(result, Err(DispatchError::ParseError(_))));
        }

        #[test]
        fn test_invalid_json_syntax() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestBooking>("TestBooking");

            let json = r#"{ invalid json }"#;
            let result = registry.validate("TestBooking", json);
            assert!(matches!(result, Err(DispatchError::ParseError(_))));
        }

        #[test]
        fn test_registry_has_type() {
            let mut registry = TypeRegistry::new();
            assert!(!registry.has_type("TestBooking"));

            registry.register::<TestBooking>("TestBooking");
            assert!(registry.has_type("TestBooking"));
            assert!(!registry.has_type("NonExistent"));
        }

        #[test]
        fn test_registry_type_names() {
            let mut registry = TypeRegistry::new();
            assert!(registry.type_names().is_empty());

            registry.register::<TestBooking>("TestBooking");
            let names = registry.type_names();
            assert_eq!(names.len(), 1);
            assert!(names.contains(&"TestBooking"));
        }

        // Test nested validation
        #[derive(Debug, Validate, Deserialize)]
        struct TestAddress {
            #[validate(length(min = 1, max = 100))]
            street: String,

            #[validate(length(min = 1, max = 50))]
            city: String,
        }

        #[derive(Debug, Validate, Deserialize)]
        struct TestPerson {
            #[validate(length(min = 1, max = 50))]
            name: String,

            #[validate(nested)]
            address: TestAddress,
        }

        #[test]
        fn test_nested_validation_with_path() {
            let mut registry = TypeRegistry::new();
            registry.register::<TestPerson>("TestPerson");

            // Invalid city in nested address
            let json = r#"{"name": "John", "address": {"street": "123 Main", "city": ""}}"#;
            let result = registry.validate("TestPerson", json);

            assert!(matches!(result, Err(DispatchError::Validation(_))));
            if let Err(DispatchError::Validation(err)) = result {
                assert!(!err.violations.is_empty());
                // Path should include nested field
                let path = err.violations[0].path.to_string();
                assert!(path.contains("address") || path.contains("city"));
            }
        }
    }

    // Tests for global registry functions
    mod global_registry {
        use super::*;
        use serde::Deserialize;

        #[derive(Debug, Validate, Deserialize)]
        struct GlobalTestType {
            #[validate(length(min = 1))]
            name: String,
        }

        #[test]
        fn test_global_register_and_check() {
            // Register type globally
            register_type::<GlobalTestType>("GlobalTestType");

            // Check it's registered
            assert!(is_type_registered("GlobalTestType"));
            assert!(!is_type_registered("NotRegistered"));

            // Check it appears in list
            let types = registered_types();
            assert!(types.contains(&"GlobalTestType"));
        }
    }

    // Tests for ValidationResult constructors
    mod validation_result {
        use super::*;

        #[test]
        fn test_success_serialization() {
            let result = ValidationResult::success();
            let json = serde_json::to_string(&result).unwrap();
            assert!(json.contains("\"ok\":true"));
            assert!(!json.contains("errors"));
            assert!(!json.contains("error"));
        }

        #[test]
        fn test_validation_failed_serialization() {
            let violations = vec![
                WasmViolation {
                    path: "field1".to_string(),
                    code: "error1".to_string(),
                    message: "Error 1".to_string(),
                    meta: None,
                },
                WasmViolation {
                    path: "field2".to_string(),
                    code: "error2".to_string(),
                    message: "Error 2".to_string(),
                    meta: None,
                },
            ];
            let result = ValidationResult::validation_failed(violations);
            let json = serde_json::to_string(&result).unwrap();
            assert!(json.contains("\"ok\":false"));
            assert!(json.contains("\"errors\""));
            assert!(json.contains("field1"));
            assert!(json.contains("field2"));
        }

        #[test]
        fn test_system_error_serialization() {
            let result = ValidationResult::system_error("parse_error", "Invalid JSON".to_string());
            let json = serde_json::to_string(&result).unwrap();
            assert!(json.contains("\"ok\":false"));
            assert!(json.contains("\"error\""));
            assert!(json.contains("parse_error"));
            assert!(json.contains("Invalid JSON"));
        }

        #[test]
        fn test_empty_violations_list() {
            let result = ValidationResult::validation_failed(vec![]);
            let json = serde_json::to_string(&result).unwrap();
            assert!(json.contains("\"ok\":false"));
            assert!(json.contains("\"errors\":[]"));
        }

        #[test]
        fn test_violation_with_meta_serialization() {
            let mut meta = HashMap::new();
            meta.insert("min".to_string(), "1".to_string());
            meta.insert("max".to_string(), "10".to_string());

            let violations = vec![WasmViolation {
                path: "count".to_string(),
                code: "out_of_range".to_string(),
                message: "Must be between 1 and 10".to_string(),
                meta: Some(meta),
            }];
            let result = ValidationResult::validation_failed(violations);
            let json = serde_json::to_string(&result).unwrap();
            assert!(json.contains("\"meta\""));
            assert!(json.contains("\"min\":\"1\""));
            assert!(json.contains("\"max\":\"10\""));
        }

        #[test]
        fn test_special_characters_in_message() {
            let result = ValidationResult::system_error(
                "parse_error",
                r#"Expected "}" at line 1, column 5"#.to_string(),
            );
            let json = serde_json::to_string(&result).unwrap();
            assert!(json.contains("parse_error"));
            // Message should be properly escaped - verify valid JSON by parsing as Value
            assert!(serde_json::from_str::<serde_json::Value>(&json).is_ok());
        }
    }

    // Tests for TypeRegistry edge cases
    mod registry_edge_cases {
        use super::*;
        use serde::Deserialize;

        #[derive(Debug, Validate, Deserialize)]
        struct TypeA {
            #[validate(length(min = 1))]
            name: String,
        }

        #[derive(Debug, Validate, Deserialize)]
        struct TypeB {
            #[validate(range(min = 0, max = 100))]
            value: i32,
        }

        #[test]
        fn test_registry_default_impl() {
            let registry = TypeRegistry::default();
            assert!(registry.type_names().is_empty());
        }

        #[test]
        fn test_multiple_types_registration() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("TypeA");
            registry.register::<TypeB>("TypeB");

            assert!(registry.has_type("TypeA"));
            assert!(registry.has_type("TypeB"));
            assert_eq!(registry.type_names().len(), 2);
        }

        #[test]
        fn test_type_overwriting() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("SharedName");

            // Validate with TypeA rules (length validation)
            let result = registry.validate("SharedName", r#"{"name": ""}"#);
            assert!(matches!(result, Err(DispatchError::Validation(_))));

            // Overwrite with TypeB
            registry.register::<TypeB>("SharedName");

            // Now validates with TypeB rules (range validation)
            let result = registry.validate("SharedName", r#"{"value": 50}"#);
            assert!(result.is_ok());
        }

        #[test]
        fn test_empty_type_name() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("");

            assert!(registry.has_type(""));
            let result = registry.validate("", r#"{"name": "test"}"#);
            assert!(result.is_ok());
        }

        #[test]
        fn test_type_name_with_special_characters() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("Type::With::Colons");
            registry.register::<TypeB>("Type<Generic>");

            assert!(registry.has_type("Type::With::Colons"));
            assert!(registry.has_type("Type<Generic>"));
        }

        #[test]
        fn test_validate_with_whitespace_in_json() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("TypeA");

            let json = r#"
                {
                    "name": "test"
                }
            "#;
            let result = registry.validate("TypeA", json);
            assert!(result.is_ok());
        }

        #[test]
        fn test_validate_with_extra_fields_in_json() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("TypeA");

            // Extra fields should be ignored (default serde behavior)
            let json = r#"{"name": "test", "extra": "ignored"}"#;
            let result = registry.validate("TypeA", json);
            assert!(result.is_ok());
        }

        #[test]
        fn test_unicode_in_validation_values() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("TypeA");

            let json = r#"{"name": "日本語テスト 🎉"}"#;
            let result = registry.validate("TypeA", json);
            assert!(result.is_ok());
        }

        #[test]
        fn test_null_value_in_json() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("TypeA");

            let json = r#"{"name": null}"#;
            let result = registry.validate("TypeA", json);
            // String field can't be null, should fail parse
            assert!(matches!(result, Err(DispatchError::ParseError(_))));
        }

        #[test]
        fn test_empty_string_validation() {
            let mut registry = TypeRegistry::new();
            registry.register::<TypeA>("TypeA");

            let json = r#"{"name": ""}"#;
            let result = registry.validate("TypeA", json);
            // Empty string fails min length 1 validation
            assert!(matches!(result, Err(DispatchError::Validation(_))));
        }
    }

    // Tests for WasmViolation conversion edge cases
    mod wasm_violation_edge_cases {
        use super::*;

        #[test]
        fn test_violation_with_empty_path() {
            let violation = domainstack::Violation {
                path: domainstack::Path::root(),
                code: "invalid",
                message: "Invalid".to_string(),
                meta: domainstack::Meta::default(),
            };

            let wasm_violation = WasmViolation::from(&violation);
            assert_eq!(wasm_violation.path, "");
        }

        #[test]
        fn test_violation_with_complex_nested_path() {
            let path = domainstack::Path::root()
                .field("orders")
                .index(0)
                .field("items")
                .index(5)
                .field("variant");

            let violation = domainstack::Violation {
                path,
                code: "invalid",
                message: "Invalid".to_string(),
                meta: domainstack::Meta::default(),
            };

            let wasm_violation = WasmViolation::from(&violation);
            assert_eq!(wasm_violation.path, "orders[0].items[5].variant");
        }

        #[test]
        fn test_violation_with_special_chars_in_code() {
            let violation = domainstack::Violation {
                path: domainstack::Path::from("field"),
                code: "error_code_with_underscores",
                message: "Error".to_string(),
                meta: domainstack::Meta::default(),
            };

            let wasm_violation = WasmViolation::from(&violation);
            assert_eq!(wasm_violation.code, "error_code_with_underscores");
        }

        #[test]
        fn test_violation_preserves_long_message() {
            let long_message = "A".repeat(1000);
            let violation = domainstack::Violation {
                path: domainstack::Path::from("field"),
                code: "error",
                message: long_message.clone(),
                meta: domainstack::Meta::default(),
            };

            let wasm_violation = WasmViolation::from(&violation);
            assert_eq!(wasm_violation.message, long_message);
        }

        #[test]
        fn test_violation_meta_numeric_values() {
            let mut meta = domainstack::Meta::default();
            meta.insert("min", 1);
            meta.insert("max", 100);
            meta.insert("actual", 150);

            let violation = domainstack::Violation {
                path: domainstack::Path::from("field"),
                code: "out_of_range",
                message: "Out of range".to_string(),
                meta,
            };

            let wasm_violation = WasmViolation::from(&violation);
            let meta = wasm_violation.meta.unwrap();
            // Numeric values should be converted to strings
            assert_eq!(meta.get("min"), Some(&"1".to_string()));
            assert_eq!(meta.get("max"), Some(&"100".to_string()));
            assert_eq!(meta.get("actual"), Some(&"150".to_string()));
        }
    }

    // Tests for dispatch error handling
    mod dispatch_error_tests {
        use super::*;
        use serde::Deserialize;

        #[derive(Debug, Validate, Deserialize)]
        #[allow(dead_code)]
        struct SimpleType {
            value: i32,
        }

        #[test]
        fn test_dispatch_error_unknown_type_message() {
            let registry = TypeRegistry::new();
            let result = registry.validate("DoesNotExist", "{}");

            match result {
                Err(DispatchError::UnknownType) => {}
                _ => panic!("Expected UnknownType error"),
            }
        }

        #[test]
        fn test_dispatch_error_parse_error_contains_details() {
            let mut registry = TypeRegistry::new();
            registry.register::<SimpleType>("SimpleType");

            let result = registry.validate("SimpleType", r#"{"value": "not_a_number"}"#);

            match result {
                Err(DispatchError::ParseError(msg)) => {
                    assert!(!msg.is_empty());
                }
                _ => panic!("Expected ParseError"),
            }
        }

        #[test]
        fn test_dispatch_error_validation_boxed() {
            let mut registry = TypeRegistry::new();

            #[derive(Debug, Validate, Deserialize)]
            struct AlwaysInvalid {
                #[validate(range(min = 10, max = 5))] // Invalid range, will fail
                value: i32,
            }

            registry.register::<AlwaysInvalid>("AlwaysInvalid");

            // Any value should trigger validation error
            let result = registry.validate("AlwaysInvalid", r#"{"value": 7}"#);

            match result {
                Err(DispatchError::Validation(boxed_err)) => {
                    assert!(!boxed_err.violations.is_empty());
                }
                _ => panic!("Expected Validation error"),
            }
        }
    }
}