json-eval-rs 0.0.120

High-performance JSON Logic evaluator with schema validation and dependency tracking. Built on blazing-fast Rust engine.
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
use json_eval_rs::JSONEval;
use serde_json::json;

#[test]
fn test_validation_error_has_all_fields() {
    // Schema with pattern rule to test all error fields
    let schema = json!({
        "type": "object",
        "properties": {
            "email": {
                "type": "string",
                "title": "Email",
                "rules": {
                    "pattern": {
                        "value": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
                        "message": "Invalid email format",
                        "code": "email.invalid_format"
                    }
                }
            },
            "age": {
                "type": "number",
                "title": "Age",
                "rules": {
                    "minValue": {
                        "value": 1,
                        "message": "Age must be at least 1",
                        "code": "age.too_young"
                    }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    // Invalid data
    let data = json!({
        "email": "invalid-email",
        "age": 0
    });
    let data_str = serde_json::to_string(&data).unwrap();

    eval.evaluate(&data_str, None, None, None).unwrap();
    let validation = eval.validate(&data_str, None, None, None, None, None).unwrap();

    assert!(validation.has_error, "Should have validation errors");
    assert_eq!(validation.errors.len(), 2, "Should have 2 errors");

    // Check pattern error has all fields
    let email_error = validation
        .errors
        .get("email")
        .expect("Should have email error");
    assert_eq!(email_error.rule_type, "pattern");
    assert_eq!(email_error.message, "Invalid email format");
    assert_eq!(email_error.code, Some("email.invalid_format".to_string()));
    assert!(
        email_error.pattern.is_some(),
        "Pattern error should have pattern field"
    );
    assert!(
        email_error.field_value.is_some(),
        "Pattern error should have field_value"
    );
    assert_eq!(email_error.field_value.as_ref().unwrap(), "invalid-email");
    assert!(
        email_error.data.is_none(),
        "Pattern error should not have data field"
    );

    // Check minValue error has code but not pattern/field_value
    let age_error = validation.errors.get("age").expect("Should have age error");
    assert_eq!(age_error.rule_type, "minValue");
    assert_eq!(age_error.message, "Age must be at least 1");
    assert_eq!(age_error.code, Some("age.too_young".to_string()));
    assert!(
        age_error.pattern.is_none(),
        "minValue error should not have pattern"
    );
    assert!(
        age_error.field_value.is_none(),
        "minValue error should not have field_value"
    );
    assert!(
        age_error.data.is_none(),
        "minValue error should not have data"
    );
}

#[test]
fn test_validation_error_default_code() {
    // Schema without custom code - should generate default
    let schema = json!({
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "rules": {
                    "required": {
                        "value": true,
                        "message": "Name is required"
                    }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data = json!({});
    let data_str = serde_json::to_string(&data).unwrap();

    eval.evaluate(&data_str, None, None, None).unwrap();
    let validation = eval.validate(&data_str, None, None, None, None, None).unwrap();

    assert!(validation.has_error);
    let error = validation
        .errors
        .get("name")
        .expect("Should have name error");

    // Default code should be "{path}.{ruleName}"
    assert_eq!(error.code, Some("name.required".to_string()));
}

#[test]
fn test_validation_error_serialization() {
    // Test that errors serialize correctly with optional fields
    let schema = json!({
        "type": "object",
        "properties": {
            "test": {
                "type": "string",
                "rules": {
                    "required": {
                        "value": true,
                        "message": "Required"
                    }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data = json!({});
    let data_str = serde_json::to_string(&data).unwrap();

    eval.evaluate(&data_str, None, None, None).unwrap();
    let validation = eval.validate(&data_str, None, None, None, None, None).unwrap();

    // Serialize the validation result
    let json_str = serde_json::to_string(&validation).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();

    // Check structure
    assert_eq!(parsed["has_error"], true);
    assert!(parsed["errors"].is_object());

    let error = &parsed["errors"]["test"];
    assert_eq!(error["type"], "required");
    assert_eq!(error["message"], "Required");
    assert_eq!(error["code"], "test.required");

    // Optional fields should not be present when None
    assert!(!error.as_object().unwrap().contains_key("pattern"));
    assert!(!error.as_object().unwrap().contains_key("fieldValue"));
    assert!(!error.as_object().unwrap().contains_key("data"));

    // Test with fieldValue present
    let schema_pattern = json!({
        "type": "object",
        "properties": {
            "code": {
                "type": "string",
                "rules": {
                    "pattern": {
                        "value": "^[0-9]+$",
                        "message": "Must be digits"
                    }
                }
            }
        }
    });

    let schema_pattern_str = serde_json::to_string(&schema_pattern).unwrap();
    let mut eval_pattern = JSONEval::new(&schema_pattern_str, None, None).unwrap();
    let data_pattern = json!({ "code": "abc" });
    let data_pattern_str = serde_json::to_string(&data_pattern).unwrap();

    eval_pattern
        .evaluate(&data_pattern_str, None, None, None)
        .unwrap();
    let validation_pattern = eval_pattern
        .validate(&data_pattern_str, None, None, None, None, None)
        .unwrap();

    let json_str_pattern = serde_json::to_string(&validation_pattern).unwrap();
    let parsed_pattern: serde_json::Value = serde_json::from_str(&json_str_pattern).unwrap();

    let error_pattern = &parsed_pattern["errors"]["code"];
    assert_eq!(error_pattern["type"], "pattern");
    assert_eq!(error_pattern["fieldValue"], "abc");
}

#[test]
fn test_validate_cache_identical_data() {
    let schema = json!({
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Name is required" }
                }
            },
            "score": {
                "type": "number",
                "rules": {
                    "minValue": { "value": 50, "message": "Minimum score is 50" }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data = json!({ "name": "Alice", "score": 40 });
    let data_str = serde_json::to_string(&data).unwrap();

    let res1 = eval.validate(&data_str, None, None, None, None, None).unwrap();
    assert!(res1.has_error);
    assert_eq!(res1.errors.len(), 1);
    assert!(res1.errors.contains_key("score"));

    // Second validate call with identical data should hit cache and return matching result
    let res2 = eval.validate(&data_str, None, None, None, None, None).unwrap();
    assert_eq!(res1.has_error, res2.has_error);
    assert_eq!(res1.errors.len(), res2.errors.len());
    assert_eq!(
        res1.errors.get("score").unwrap().message,
        res2.errors.get("score").unwrap().message
    );
}

#[test]
fn test_validate_cache_incremental_field_change() {
    let schema = json!({
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Name is required" }
                }
            },
            "age": {
                "type": "number",
                "rules": {
                    "minValue": { "value": 18, "message": "Must be at least 18" }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    // 1. Initial invalid data (age < 18)
    let d1 = serde_json::to_string(&json!({ "name": "Bob", "age": 16 })).unwrap();
    let r1 = eval.validate(&d1, None, None, None, None, None).unwrap();
    assert!(r1.has_error);
    assert_eq!(r1.errors.len(), 1);
    assert!(r1.errors.contains_key("age"));

    // 2. Incremental change: fix age, name unchanged
    let d2 = serde_json::to_string(&json!({ "name": "Bob", "age": 20 })).unwrap();
    let r2 = eval.validate(&d2, None, None, None, None, None).unwrap();
    assert!(!r2.has_error);
    assert_eq!(r2.errors.len(), 0);

    // 3. Incremental change: age remains valid, name emptied
    let d3 = serde_json::to_string(&json!({ "name": "", "age": 20 })).unwrap();
    let r3 = eval.validate(&d3, None, None, None, None, None).unwrap();
    assert!(r3.has_error);
    assert_eq!(r3.errors.len(), 1);
    assert!(r3.errors.contains_key("name"));
}

#[test]
fn test_validate_disabled_field_with_rules() {
    let schema = json!({
        "type": "object",
        "properties": {
            "fixed_id": {
                "type": "string",
                "disabled": true,
                "rules": {
                    "required": { "value": true, "message": "ID is required" },
                    "minLength": { "value": 3, "message": "ID must have 3+ characters" }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    // By default (validate_readonly = None or Some(false)):
    // Missing value on disabled field -> skipped, NO validation error
    let d1 = serde_json::to_string(&json!({})).unwrap();
    let r1_default = eval.validate(&d1, None, None, None, None, None).unwrap();
    assert!(!r1_default.has_error, "Disabled field must be skipped by default");
    assert!(!r1_default.errors.contains_key("fixed_id"));

    let r1_false = eval.validate(&d1, None, None, None, Some(false), None).unwrap();
    assert!(!r1_false.has_error, "Disabled field must be skipped when validate_readonly=false");

    // When validate_readonly = Some(true):
    // Missing value on disabled field -> fails required rule
    let r1_true = eval.validate(&d1, None, None, None, Some(true), None).unwrap();
    assert!(r1_true.has_error, "Disabled field with required rule must be validated when validate_readonly=true");
    assert!(r1_true.errors.contains_key("fixed_id"));
    assert_eq!(r1_true.errors["fixed_id"].rule_type, "required");

    // Invalid length on disabled field with validate_readonly=true -> should fail minLength rule
    let d2 = serde_json::to_string(&json!({ "fixed_id": "AB" })).unwrap();
    let r2_true = eval.validate(&d2, None, None, None, Some(true), None).unwrap();
    assert!(r2_true.has_error);
    assert!(r2_true.errors.contains_key("fixed_id"));
    assert_eq!(r2_true.errors["fixed_id"].rule_type, "minLength");

    // With validate_readonly=false, invalid length on disabled field is skipped
    let r2_false = eval.validate(&d2, None, None, None, Some(false), None).unwrap();
    assert!(!r2_false.has_error);

    // Valid value on disabled field -> should pass in both modes
    let d3 = serde_json::to_string(&json!({ "fixed_id": "ABC" })).unwrap();
    let r3_default = eval.validate(&d3, None, None, None, None, None).unwrap();
    assert!(!r3_default.has_error);
    let r3_true = eval.validate(&d3, None, None, None, Some(true), None).unwrap();
    assert!(!r3_true.has_error);
}


#[test]
fn test_validate_unmapped_field_missing_from_data_and_eval_data() {
    let schema = json!({
        "type": "object",
        "properties": {
            "unmapped_required_code": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Code is required" }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data_str = "{}";
    let res = eval.validate(data_str, None, None, None, None, None).unwrap();
    assert!(res.has_error);
    assert!(res.errors.contains_key("unmapped_required_code"));
    assert_eq!(res.errors["unmapped_required_code"].rule_type, "required");
}

#[test]
fn test_validate_cache_invalidation_on_reload() {
    let schema1 = json!({
        "type": "object",
        "properties": {
            "val": {
                "type": "number",
                "rules": {
                    "minValue": { "value": 10, "message": "Min 10" }
                }
            }
        }
    });

    let schema2 = json!({
        "type": "object",
        "properties": {
            "val": {
                "type": "number",
                "rules": {
                    "minValue": { "value": 100, "message": "Min 100" }
                }
            }
        }
    });

    let mut eval = JSONEval::new(&schema1.to_string(), None, None).unwrap();
    let d = serde_json::to_string(&json!({ "val": 50 })).unwrap();

    let r1 = eval.validate(&d, None, None, None, None, None).unwrap();
    assert!(!r1.has_error, "50 >= 10");

    // Reload with schema2 where min is 100
    eval.reload_schema(&schema2.to_string(), None, None).unwrap();
    let r2 = eval.validate(&d, None, None, None, None, None).unwrap();
    assert!(r2.has_error, "50 < 100, cache should have been invalidated on reload");
    assert_eq!(r2.errors["val"].message, "Min 100");
}

#[test]
fn test_validate_readonly_field_direct_and_conditional() {
    let schema = json!({
        "type": "object",
        "properties": {
            "ro_field": {
                "type": "string",
                "readonly": true,
                "rules": {
                    "required": { "value": true, "message": "Readonly field is required" }
                }
            },
            "cond_ro_field": {
                "type": "string",
                "condition": {
                    "readonly": true
                },
                "rules": {
                    "required": { "value": true, "message": "Conditional readonly is required" }
                }
            },
            "active_field": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Active field is required" }
                }
            }
        }
    });

    let mut eval = JSONEval::new(&schema.to_string(), None, None).unwrap();
    let data_empty = "{}";

    // Default (validate_readonly = None or false): only active_field should have error
    let res_default = eval.validate(data_empty, None, None, None, None, None).unwrap();
    assert!(res_default.has_error);
    assert_eq!(res_default.errors.len(), 1);
    assert!(res_default.errors.contains_key("active_field"));
    assert!(!res_default.errors.contains_key("ro_field"));
    assert!(!res_default.errors.contains_key("cond_ro_field"));

    // validate_readonly = Some(true): ro_field, cond_ro_field, and active_field must all be validated
    let res_true = eval.validate(data_empty, None, None, None, Some(true), None).unwrap();
    assert!(res_true.has_error);
    assert_eq!(res_true.errors.len(), 3);
    assert!(res_true.errors.contains_key("active_field"));
    assert!(res_true.errors.contains_key("ro_field"));
    assert!(res_true.errors.contains_key("cond_ro_field"));
}

#[test]
fn test_validate_layout_disabled_ref() {
    let schema = json!({
        "type": "object",
        "properties": {
            "elem": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Elem is required" }
                }
            }
        },
        "$layout": {
            "elements": [
                {
                    "type": "Control",
                    "$ref": "#/properties/elem",
                    "disabled": true
                }
            ]
        }
    });

    let mut eval = JSONEval::new(&schema.to_string(), None, None).unwrap();
    let data_empty = "{}";

    // Resolving layout fills layout resolution state
    eval.resolve_layout(false).unwrap();

    // Default: layout disabled field is skipped
    let res_default = eval.validate(data_empty, None, None, None, None, None).unwrap();
    assert!(!res_default.has_error, "Layout disabled ref should be skipped by default");

    // validate_readonly = Some(true): layout disabled field is validated
    let res_true = eval.validate(data_empty, None, None, None, Some(true), None).unwrap();
    assert!(res_true.has_error, "Layout disabled ref should be validated when validate_readonly=true");
    assert!(res_true.errors.contains_key("elem"));
}

#[test]
fn test_validate_with_include_subforms_flag() {
    let schema = json!({
        "type": "object",
        "properties": {
            "title": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Title is required" }
                }
            },
            "contacts": {
                "type": "array",
                "items": {
                    "properties": {
                        "name": {
                            "type": "string",
                            "rules": {
                                "required": { "value": true, "message": "Name is required" }
                            }
                        },
                        "phone": {
                            "type": "string",
                            "rules": {
                                "required": { "value": true, "message": "Phone is required" }
                            }
                        }
                    }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data = json!({
        "title": "",
        "contacts": [
            { "name": "Alice", "phone": "" },
            { "name": "", "phone": "12345" }
        ]
    });
    let data_str = serde_json::to_string(&data).unwrap();

    // 1. include_subforms = None (default): only root fields validated
    let res_default = eval.validate(&data_str, None, None, None, None, None).unwrap();
    assert!(res_default.has_error);
    assert_eq!(res_default.errors.len(), 1);
    assert!(res_default.errors.contains_key("title"));
    assert!(!res_default.errors.contains_key("contacts.0.phone"));
    assert!(!res_default.errors.contains_key("contacts.1.name"));

    // 2. include_subforms = Some(false): only root fields validated
    let res_false = eval.validate(&data_str, None, None, None, None, Some(false)).unwrap();
    assert!(res_false.has_error);
    assert_eq!(res_false.errors.len(), 1);
    assert!(res_false.errors.contains_key("title"));

    // 3. include_subforms = Some(true): root + all array subform items validated
    let res_true = eval.validate(&data_str, None, None, None, None, Some(true)).unwrap();
    assert!(res_true.has_error);
    assert_eq!(res_true.errors.len(), 3);
    assert!(res_true.errors.contains_key("title"));
    assert!(res_true.errors.contains_key("contacts.0.phone"));
    assert!(res_true.errors.contains_key("contacts.1.name"));
    assert!(!res_true.errors.contains_key("contacts.0.name"));
    assert!(!res_true.errors.contains_key("contacts.1.phone"));

    let err_c0_phone = &res_true.errors["contacts.0.phone"];
    assert_eq!(err_c0_phone.message, "Phone is required");
    assert_eq!(err_c0_phone.code, Some("contacts.0.phone.required".to_string()));

    let err_c1_name = &res_true.errors["contacts.1.name"];
    assert_eq!(err_c1_name.message, "Name is required");
    assert_eq!(err_c1_name.code, Some("contacts.1.name.required".to_string()));
}

#[test]
fn test_validate_with_include_subforms_paths_filter_and_cache() {
    let schema = json!({
        "type": "object",
        "properties": {
            "title": {
                "type": "string",
                "rules": {
                    "required": { "value": true, "message": "Title is required" }
                }
            },
            "contacts": {
                "type": "array",
                "items": {
                    "properties": {
                        "name": {
                            "type": "string",
                            "rules": {
                                "required": { "value": true, "message": "Name is required" }
                            }
                        },
                        "phone": {
                            "type": "string",
                            "rules": {
                                "required": { "value": true, "message": "Phone is required" }
                            }
                        }
                    }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data = json!({
        "title": "",
        "contacts": [
            { "name": "Alice", "phone": "" },
            { "name": "", "phone": "12345" }
        ]
    });
    let data_str = serde_json::to_string(&data).unwrap();

    // 1. Selective path: only contacts.0.phone
    let paths_item0 = vec!["contacts.0.phone".to_string()];
    let res_item0 = eval.validate(&data_str, None, Some(&paths_item0), None, None, Some(true)).unwrap();
    assert!(res_item0.has_error);
    assert_eq!(res_item0.errors.len(), 1);
    assert!(res_item0.errors.contains_key("contacts.0.phone"));

    // 2. Selective path: whole item 0 -> should validate item 0 fields only
    let paths_whole_item0 = vec!["contacts.0".to_string()];
    let res_whole0 = eval.validate(&data_str, None, Some(&paths_whole_item0), None, None, Some(true)).unwrap();
    assert!(res_whole0.has_error);
    assert_eq!(res_whole0.errors.len(), 1);
    assert!(res_whole0.errors.contains_key("contacts.0.phone"));
    assert!(!res_whole0.errors.contains_key("contacts.1.name"));

    // 3. Selective path: title only
    let paths_title = vec!["title".to_string()];
    let res_title = eval.validate(&data_str, None, Some(&paths_title), None, None, Some(true)).unwrap();
    assert!(res_title.has_error);
    assert_eq!(res_title.errors.len(), 1);
    assert!(res_title.errors.contains_key("title"));

    // 4. Cache isolation: full validate with include_subforms=true then include_subforms=false
    let r_full_true = eval.validate(&data_str, None, None, None, None, Some(true)).unwrap();
    assert_eq!(r_full_true.errors.len(), 3);

    let r_full_false = eval.validate(&data_str, None, None, None, None, Some(false)).unwrap();
    assert_eq!(r_full_false.errors.len(), 1, "Cache hit must not leak subform errors when include_subforms=false");

    let r_full_true_again = eval.validate(&data_str, None, None, None, None, Some(true)).unwrap();
    assert_eq!(r_full_true_again.errors.len(), 3, "Cached subforms result returned when include_subforms=true");
}

#[test]
fn test_validate_with_include_subforms_nested_and_items_root_key() {
    let schema = json!({
        "type": "object",
        "properties": {
            "illustration": {
                "type": "object",
                "properties": {
                    "riders": {
                        "type": "array",
                        "itemsRootKey": "riders",
                        "items": {
                            "properties": {
                                "sa": {
                                    "type": "number",
                                    "rules": {
                                        "minValue": { "value": 1000, "message": "Min SA is 1000" }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });

    let schema_str = serde_json::to_string(&schema).unwrap();
    let mut eval = JSONEval::new(&schema_str, None, None).unwrap();

    let data = json!({
        "illustration": {
            "riders": [
                { "sa": 500 },
                { "sa": 2000 }
            ]
        }
    });
    let data_str = serde_json::to_string(&data).unwrap();

    let res = eval.validate(&data_str, None, None, None, None, Some(true)).unwrap();
    assert!(res.has_error);
    assert_eq!(res.errors.len(), 1);
    let err = res.errors.get("illustration.riders.0.sa").expect("Should map to illustration.riders.0.sa");
    assert_eq!(err.message, "Min SA is 1000");
    assert_eq!(err.code, Some("illustration.riders.0.sa.minValue".to_string()));
}