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
use super::JSONEval;
use crate::jsoneval::cancellation::CancellationToken;
use crate::jsoneval::json_parser;
use crate::jsoneval::path_utils;
use crate::jsoneval::types::{ValidationError, ValidationResult};
use crate::time_block;
use indexmap::IndexMap;
use serde_json::Value;
impl JSONEval {
/// Invalidate the validation cache
pub(crate) fn invalidate_validation_cache(&self) {
let mut cache = match self.validation_cache.write() {
Ok(c) => c,
Err(poisoned) => poisoned.into_inner(),
};
cache.clear();
}
/// Validate data against schema rules
pub fn validate(
&mut self,
data: &str,
context: Option<&str>,
paths: Option<&[String]>,
token: Option<&CancellationToken>,
validate_readonly: Option<bool>,
) -> Result<ValidationResult, String> {
let validate_ro = validate_readonly.unwrap_or(false);
if let Some(t) = token {
if t.is_cancelled() {
return Err("Cancelled".to_string());
}
}
// Fast path: if no path filtering and data/context are identical to last validation,
// return cached full result immediately.
if paths.is_none() || paths.is_some_and(|p| p.is_empty()) {
if let Ok(cache) = self.validation_cache.read() {
if let Some(cached) = cache.get_cached_full_result(data, context, validate_ro) {
return Ok(cached);
}
}
}
time_block!("validate() [total]", {
// Acquire lock for synchronous execution
let _lock = self.eval_lock.lock().unwrap();
// Parse and update data
let (data_value, context_value) = time_block!(" parse data & context", {
let d = json_parser::parse_json_str(data)?;
let c = if let Some(ctx) = context {
json_parser::parse_json_str(ctx)?
} else {
Value::Object(serde_json::Map::new())
};
Ok::<_, String>((d, c))
})?;
// Update context
self.context = context_value.clone();
// Update eval_data with new data/context
time_block!(" replace_data_and_context", {
self.eval_data
.replace_data_and_context(data_value.clone(), context_value);
});
// Drop lock before calling evaluate_others which needs mutable access
drop(_lock);
// Re-evaluate rule evaluations to ensure fresh values
// This ensures all rule.$evaluation expressions are re-computed
time_block!(" evaluate_others", {
self.evaluate_others(paths, token);
});
time_block!(" ensure_layout_resolved", {
self.ensure_layout_resolved();
});
let mut errors: IndexMap<String, ValidationError> = IndexMap::new();
let layout_state = self.layout_state.read().unwrap();
let layout_hidden_refs = &layout_state.layout_hidden_refs;
let layout_disabled_refs = &layout_state.layout_disabled_refs;
let mut hidden_cache =
std::collections::HashMap::with_capacity(self.fields_with_rules.len());
let mut readonly_cache =
std::collections::HashMap::with_capacity(self.fields_with_rules.len());
// Use pre-parsed fields_with_rules from schema parsing (no runtime collection needed)
// This list was collected during schema parse and contains all fields with rules
time_block!(" fields_with_rules loop", {
for field_path in self.fields_with_rules.iter() {
// Check if we should validate this path (path filtering)
if let Some(filter_paths) = paths {
if !filter_paths.is_empty()
&& !filter_paths.iter().any(|p| {
field_path.starts_with(p.as_str()) || p.starts_with(field_path.as_str())
})
{
continue;
}
}
self.validate_field_cached(
field_path,
&data_value,
layout_hidden_refs,
layout_disabled_refs,
&mut hidden_cache,
&mut readonly_cache,
validate_ro,
&mut errors,
);
if let Some(t) = token {
if t.is_cancelled() {
return Err("Cancelled".to_string());
}
}
}
});
drop(layout_state);
let has_error = !errors.is_empty();
let result = ValidationResult { has_error, errors };
if paths.is_none() || paths.is_some_and(|p| p.is_empty()) {
if let Ok(mut cache) = self.validation_cache.write() {
cache.save_full_result(
data.to_string(),
context.map(|s| s.to_string()),
validate_ro,
result.clone(),
);
}
} else if let Ok(mut cache) = self.validation_cache.write() {
cache.invalidate_full_result();
}
Ok(result)
})
}
/// Validate using the data already present in `eval_data` (set by `with_item_cache_swap`).
///
/// Skips JSON parsing and `replace_data_and_context` — use this inside the
/// cache-swap closure to avoid redundant work when the subform data is already set.
pub(crate) fn validate_pre_set(
&mut self,
data_value: Value,
paths: Option<&[String]>,
token: Option<&CancellationToken>,
validate_readonly: Option<bool>,
) -> Result<crate::ValidationResult, String> {
let validate_ro = validate_readonly.unwrap_or(false);
// Re-evaluate rule evaluations with the current (already-set) data.
self.evaluate_others(paths, token);
self.ensure_layout_resolved();
let mut errors: IndexMap<String, ValidationError> = IndexMap::new();
let layout_state = self.layout_state.read().unwrap();
let layout_hidden_refs = &layout_state.layout_hidden_refs;
let layout_disabled_refs = &layout_state.layout_disabled_refs;
let mut hidden_cache =
std::collections::HashMap::with_capacity(self.fields_with_rules.len());
let mut readonly_cache =
std::collections::HashMap::with_capacity(self.fields_with_rules.len());
for field_path in self.fields_with_rules.iter() {
if let Some(filter_paths) = paths {
if !filter_paths.is_empty()
&& !filter_paths.iter().any(|p| {
field_path.starts_with(p.as_str()) || p.starts_with(field_path.as_str())
})
{
continue;
}
}
if let Some(t) = token {
if t.is_cancelled() {
return Err("Cancelled".to_string());
}
}
self.validate_field_cached(
field_path,
&data_value,
layout_hidden_refs,
layout_disabled_refs,
&mut hidden_cache,
&mut readonly_cache,
validate_ro,
&mut errors,
);
}
drop(layout_state);
let has_error = !errors.is_empty();
Ok(crate::ValidationResult { has_error, errors })
}
/// Validate a single field that has rules (convenience wrapper without external cache)
#[allow(dead_code)]
pub(crate) fn validate_field(
&self,
field_path: &str,
data: &Value,
validate_readonly: bool,
errors: &mut IndexMap<String, ValidationError>,
) {
let layout_state = self.layout_state.read().unwrap();
let mut hidden_cache = std::collections::HashMap::new();
let mut readonly_cache = std::collections::HashMap::new();
self.validate_field_cached(
field_path,
data,
&layout_state.layout_hidden_refs,
&layout_state.layout_disabled_refs,
&mut hidden_cache,
&mut readonly_cache,
validate_readonly,
errors,
);
}
/// Validate a single field that has rules, with pre-acquired layout refs and caches
#[allow(clippy::too_many_arguments)]
pub(crate) fn validate_field_cached(
&self,
field_path: &str,
data: &Value,
layout_hidden_refs: &indexmap::IndexSet<String>,
layout_disabled_refs: &indexmap::IndexSet<String>,
hidden_cache: &mut std::collections::HashMap<String, bool>,
readonly_cache: &mut std::collections::HashMap<String, bool>,
validate_readonly: bool,
errors: &mut IndexMap<String, ValidationError>,
) {
// Skip if already has error
if errors.contains_key(field_path) {
return;
}
// Resolve schema for this field
let schema_path = path_utils::dot_notation_to_schema_pointer(field_path);
let pointer_path = schema_path.trim_start_matches('#');
// Try to get schema, if not found, try with /properties/ prefix for standard JSON Schema
let (field_schema, resolved_path) = match self.evaluated_schema.pointer(pointer_path) {
Some(s) => (s, pointer_path.to_string()),
None => {
let alt_path = format!("/properties{}", pointer_path);
match self.evaluated_schema.pointer(&alt_path) {
Some(s) => (s, alt_path),
None => return,
}
}
};
// Skip hidden fields using cached layout & schema lookup
let is_hidden = self.is_effective_hidden_with_cache(
&resolved_path,
layout_hidden_refs,
hidden_cache,
);
if is_hidden {
if let Ok(mut cache) = self.validation_cache.write() {
cache.update_field(
field_path.to_string(),
Value::Null,
true,
validate_readonly,
Value::Null,
None,
);
}
return;
}
// Skip readonly / disabled fields unless validate_readonly is true
if !validate_readonly {
let is_readonly = self.is_effective_readonly_with_cache(
&resolved_path,
layout_disabled_refs,
readonly_cache,
);
if is_readonly {
if let Ok(mut cache) = self.validation_cache.write() {
cache.update_field(
field_path.to_string(),
Value::Null,
false,
false,
Value::Null,
None,
);
}
return;
}
}
if let Value::Object(schema_map) = field_schema {
// Get rules object
let rules_val = match schema_map.get("rules") {
Some(r @ Value::Object(_)) => r,
_ => return,
};
let rules = rules_val.as_object().unwrap();
// Get field data
let field_data = self.get_field_data(field_path, data);
// Check field cache
let cached_lookup = if let Ok(cache) = self.validation_cache.read() {
cache.check_field_cache(field_path, &field_data, false, validate_readonly, rules_val)
} else {
None
};
if let Some(cached_error) = cached_lookup {
if let Some(err) = cached_error {
errors.insert(field_path.to_string(), err);
}
return;
}
// Validate each rule
let mut field_error: Option<ValidationError> = None;
time_block!(" validate_rules loop", {
for (rule_name, rule_value) in rules {
self.validate_rule(
field_path,
rule_name,
rule_value,
&field_data,
schema_map,
field_schema,
errors,
);
if let Some(err) = errors.get(field_path) {
field_error = Some(err.clone());
break;
}
}
});
if let Ok(mut cache) = self.validation_cache.write() {
cache.update_field(
field_path.to_string(),
field_data,
false,
validate_readonly,
rules_val.clone(),
field_error,
);
}
}
}
/// Get data value for a field path
pub(crate) fn get_field_data(&self, field_path: &str, data: &Value) -> Value {
let mut current = data;
for part in field_path.split('.') {
match current {
Value::Object(map) => {
current = map.get(part).unwrap_or(&Value::Null);
}
_ => return Value::Null,
}
}
current.clone()
}
/// Validate a single rule
#[allow(clippy::too_many_arguments)]
pub(crate) fn validate_rule(
&self,
field_path: &str,
rule_name: &str,
rule_value: &Value,
field_data: &Value,
schema_map: &serde_json::Map<String, Value>,
_schema: &Value,
errors: &mut IndexMap<String, ValidationError>,
) {
// Skip if already has error
if errors.contains_key(field_path) {
return;
}
let schema_type = schema_map
.get("type")
.and_then(|t| t.as_str())
.unwrap_or("");
// The rule_value passed in already reflects the evaluated rule from evaluated_schema
let evaluated_rule = rule_value;
// Extract rule active status, message, etc
// Logic depends on rule structure (object with value/message or direct value)
let (rule_active, rule_message, rule_code, rule_data) = match evaluated_rule {
Value::Object(rule_obj) => {
let active = rule_obj.get("value").unwrap_or(&Value::Bool(false));
// Handle message - could be string or object with "value"
let message = match rule_obj.get("message") {
Some(Value::String(s)) => s.clone(),
Some(Value::Object(msg_obj)) if msg_obj.contains_key("value") => msg_obj
.get("value")
.and_then(|v| v.as_str())
.unwrap_or("Validation failed")
.to_string(),
Some(msg_val) => msg_val.as_str().unwrap_or("Validation failed").to_string(),
None => "Validation failed".to_string(),
};
let code = rule_obj
.get("code")
.and_then(|c| c.as_str())
.map(|s| s.to_string());
// Handle data - extract "value" from objects with $evaluation
let data = rule_obj.get("data").map(|d| {
if let Value::Object(data_obj) = d {
let mut cleaned_data = serde_json::Map::new();
for (key, value) in data_obj {
// If value is an object with only "value" key, extract it
if let Value::Object(val_obj) = value {
if val_obj.len() == 1 && val_obj.contains_key("value") {
cleaned_data.insert(key.clone(), val_obj["value"].clone());
} else {
cleaned_data.insert(key.clone(), value.clone());
}
} else {
cleaned_data.insert(key.clone(), value.clone());
}
}
Value::Object(cleaned_data)
} else {
d.clone()
}
});
(active.clone(), message, code, data)
}
_ => (
evaluated_rule.clone(),
"Validation failed".to_string(),
None,
None,
),
};
// Generate default code if not provided
let error_code = rule_code.or_else(|| Some(format!("{}.{}", field_path, rule_name)));
let is_empty = matches!(field_data, Value::Null)
|| (field_data.is_string() && field_data.as_str().unwrap_or("").is_empty())
|| (field_data.is_array() && field_data.as_array().unwrap().is_empty());
match rule_name {
"required" => {
if rule_active == Value::Bool(true) {
if is_empty {
errors.insert(
field_path.to_string(),
ValidationError {
rule_type: "required".to_string(),
message: rule_message,
code: error_code,
pattern: None,
field_value: None,
data: None,
},
);
}
}
}
"minLength" | "maxLength" | "minValue" | "maxValue" => {
if rule_value_fails(rule_name, &rule_active, field_data, is_empty, schema_type) {
errors.insert(
field_path.to_string(),
ValidationError {
rule_type: rule_name.to_string(),
message: rule_message,
code: error_code,
pattern: None,
field_value: None,
data: None,
},
);
}
}
"pattern" => {
if !is_empty {
if let Some(pattern) = rule_active.as_str() {
if let Some(text) = field_data.as_str() {
let cached_regex = if let Ok(cache) = self.regex_cache.read() {
cache.get(pattern).cloned()
} else {
None
};
let regex = match cached_regex {
Some(r) => r,
None => {
let mut cache = self.regex_cache.write().unwrap();
cache.entry(pattern.to_string()).or_insert_with(|| {
regex::Regex::new(pattern)
.unwrap_or_else(|_| regex::Regex::new("(?:)").unwrap())
}).clone()
}
};
if !regex.is_match(text) {
errors.insert(
field_path.to_string(),
ValidationError {
rule_type: "pattern".to_string(),
message: rule_message,
code: error_code,
pattern: Some(pattern.to_string()),
field_value: Some(text.to_string()),
data: None,
},
);
}
}
}
}
}
"evaluation" => {
// Handle array of evaluation rules
// Format: "evaluation": [{ "code": "...", "message": "...", "$evaluation": {...} }]
if let Value::Array(eval_array) = evaluated_rule {
for (idx, eval_item) in eval_array.iter().enumerate() {
if let Value::Object(eval_obj) = eval_item {
// Get the evaluated value (should be in "value" key after evaluation)
let eval_result = eval_obj.get("value").unwrap_or(&Value::Bool(true));
// Check if result is falsy
let is_falsy = match eval_result {
Value::Bool(false) => true,
Value::Null => true,
Value::Number(n) => n.as_f64() == Some(0.0),
Value::String(s) => s.is_empty(),
Value::Array(a) => a.is_empty(),
_ => false,
};
if is_falsy {
let eval_code = eval_obj
.get("code")
.and_then(|c| c.as_str())
.map(|s| s.to_string())
.or_else(|| Some(format!("{}.evaluation.{}", field_path, idx)));
let eval_message = eval_obj
.get("message")
.and_then(|m| m.as_str())
.unwrap_or("Validation failed")
.to_string();
let eval_data = eval_obj.get("data").cloned();
errors.insert(
field_path.to_string(),
ValidationError {
rule_type: "evaluation".to_string(),
message: eval_message,
code: eval_code,
pattern: None,
field_value: None,
data: eval_data,
},
);
// Stop at first failure
break;
}
}
}
}
}
_ => {
if rule_value_fails(rule_name, &rule_active, field_data, is_empty, schema_type) {
errors.insert(
field_path.to_string(),
ValidationError {
rule_type: "evaluation".to_string(),
message: rule_message,
code: error_code,
pattern: None,
field_value: None,
data: rule_data,
},
);
}
}
}
}
/// Returns `true` if `field_data` fails any of the dep field's schema rules.
///
/// Rules are evaluated on-demand: compiled `LogicId`s from `self.evaluations` (set at
/// construction time) are executed directly against `scope_data`, completely bypassing
/// `evaluated_schema`. This avoids stale-cache issues during table dependency checks.
/// Unlike `validate_field`, this also evaluates the `required` rule on-demand.
pub(crate) fn dep_fails_schema_rules(
&self,
field_path: &str,
field_data: &Value,
scope_data: &Value,
) -> bool {
let schema_pointer = path_utils::dot_notation_to_schema_pointer(field_path);
let pointer = schema_pointer.trim_start_matches('#');
let field_schema = match self.schema.pointer(pointer) {
Some(s) => s,
None => {
let alt_pointer = format!("/properties{}", pointer);
match self.schema.pointer(&alt_pointer) {
Some(s) => s,
None => return false,
}
}
};
let schema_map = match field_schema.as_object() {
Some(m) => m,
None => return false,
};
let rules = match schema_map.get("rules") {
Some(Value::Object(r)) => r,
_ => return false,
};
let schema_type = schema_map
.get("type")
.and_then(|t| t.as_str())
.unwrap_or("");
let is_empty = matches!(field_data, Value::Null)
|| field_data.as_str().map_or(false, |s| s.is_empty())
|| field_data.as_array().map_or(false, |a| a.is_empty());
for (rule_name, rule_value) in rules {
// Resolve the rule's active value on-demand.
// If a compiled LogicId exists in self.evaluations for this rule path, run it fresh
// against scope_data. Otherwise fall back to the static "value" from the raw schema.
let rule_eval_key = format!("#{}/rules/{}", pointer, rule_name);
let rule_active: Value = if let Some(logic_id) = self.evaluations.get(&rule_eval_key) {
let empty_ctx = Value::Object(serde_json::Map::new());
self.engine
.run_with_context(logic_id, scope_data, &empty_ctx)
.unwrap_or(Value::Null)
} else {
match rule_value {
Value::Object(obj) => obj.get("value").cloned().unwrap_or(Value::Null),
other => other.clone(),
}
};
if rule_value_fails(rule_name, &rule_active, field_data, is_empty, schema_type) {
return true;
}
}
false
}
}
/// Pure rule-check: returns `true` if `rule_active` indicates `field_data` fails the rule.
///
/// This is the shared comparison kernel used by both `validate_rule` (full validation path)
/// and `dep_fails_schema_rules` (on-demand dep checking). It is intentionally free of any
/// schema/cache lookups — callers are responsible for resolving `rule_active` beforehand.
///
/// Handles: `required`, `minLength`, `maxLength`, `minValue`, `maxValue`, and custom/dynamic.
/// Does NOT handle: `pattern` (needs regex cache), `evaluation` array format (complex structure).
fn rule_value_fails(
rule_name: &str,
rule_active: &Value,
field_data: &Value,
is_empty: bool,
schema_type: &str,
) -> bool {
let coerce_num = |v: &Value| -> Option<f64> {
if let Some(n) = v.as_f64() {
return Some(n);
}
if matches!(schema_type, "number" | "integer") {
if let Some(s) = v.as_str() {
return s.trim().parse::<f64>().ok();
}
}
None
};
match rule_name {
"required" => is_empty && matches!(rule_active, Value::Bool(true)),
"minLength" => {
if is_empty {
false
} else if let Some(min) = rule_active.as_u64() {
let len = match field_data {
Value::String(s) => s.len(),
Value::Array(a) => a.len(),
_ => 0,
};
len < min as usize
} else {
false
}
}
"maxLength" => {
if is_empty {
false
} else if let Some(max) = rule_active.as_u64() {
let len = match field_data {
Value::String(s) => s.len(),
Value::Array(a) => a.len(),
_ => 0,
};
len > max as usize
} else {
false
}
}
"minValue" => {
if is_empty {
false
} else if let Some(min) = rule_active.as_f64() {
coerce_num(field_data).map_or(false, |v| v < min)
} else {
false
}
}
"maxValue" => {
if is_empty {
false
} else if let Some(max) = rule_active.as_f64() {
coerce_num(field_data).map_or(false, |v| v > max)
} else {
false
}
}
// pattern and evaluation array are handled by their specific callers
"pattern" | "evaluation" => false,
_ => {
// Custom/dynamic rule: falsy rule_active = constraint not met = field invalid
if is_empty {
false
} else {
matches!(rule_active, Value::Bool(false) | Value::Null)
|| rule_active.as_f64() == Some(0.0)
|| rule_active.as_str().map_or(false, |s| s.is_empty())
|| rule_active.as_array().map_or(false, |a| a.is_empty())
}
}
}
}