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
use json_eval_rs::JSONEval;
use serde_json::json;
#[test]
fn test_selective_eval_params_root() {
// Test selective evaluation of $params fields
let schema = json!({
"type": "object",
"$params": {
"constants": {
"RATE": 0.05
},
"othersr": {
"HAS_BEEN_PAID": {
"value": {
"$evaluation": {
"*": [
{
"$ref": "$params.constants.RATE"
},
100
]
}
}
}
}
}
});
let data = json!({});
let schema_str = serde_json::to_string(&schema).unwrap();
let data_str = serde_json::to_string(&data).unwrap();
let mut eval = JSONEval::new(&schema_str, None, Some(&data_str)).unwrap();
// Initial full evaluation
eval.evaluate(&data_str, None, None, None).unwrap();
let result = eval.get_evaluated_schema(false);
println!("value_evaluations count: {}", eval.value_evaluations.len());
println!("value_evaluations: {:?}", eval.value_evaluations);
println!(
"Full schema after initial eval: {}",
serde_json::to_string_pretty(&result).unwrap()
);
let has_been_paid_value = result.pointer("/$params/othersr/HAS_BEEN_PAID/value");
println!(
"HAS_BEEN_PAID value pointer result: {:?}",
has_been_paid_value
);
assert_eq!(
*result
.pointer("/$params/othersr/HAS_BEEN_PAID/value")
.unwrap(),
json!(5)
);
// Update $params and selectively re-evaluate
let updated_schema = json!({
"type": "object",
"$params": {
"constants": {
"RATE": 0.10 // Changed rate
},
"othersr": {
"HAS_BEEN_PAID": {
"value": {
"$evaluation": {
"*": [
{
"$ref":"$params.constants.RATE"
},
100
]
}
}
}
}
}
});
let updated_schema_str = serde_json::to_string(&updated_schema).unwrap();
let mut eval2 = JSONEval::new(&updated_schema_str, None, Some(&data_str)).unwrap();
// First do a full evaluation to process the $evaluation objects
eval2.evaluate(&data_str, None, None, None).unwrap();
let result2 = eval2.get_evaluated_schema(false);
assert_eq!(
*result2
.pointer("/$params/othersr/HAS_BEEN_PAID/value")
.unwrap(),
json!(10)
);
}
#[test]
fn test_selective_eval_explicit_properties() {
// Test with explicit "properties" in dotted notation
let schema = json!({
"type": "object",
"properties": {
"illustration": {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"firstName": {
"value": {
"$evaluation": {
"$ref": "first"
}
}
},
"fullname": {
"value": {
"$evaluation": {
"CONCAT": [
{
"$ref": "first"
},
" ",
{
"$ref": "last"
}
]
}
}
}
}
}
}
}
}
});
let data = json!({
"first": "John",
"last": "Doe"
});
let schema_str = serde_json::to_string(&schema).unwrap();
let data_str = serde_json::to_string(&data).unwrap();
let mut eval = JSONEval::new(&schema_str, None, Some(&data_str)).unwrap();
// Initial evaluation
eval.evaluate(&data_str, None, None, None).unwrap();
let result = eval.get_evaluated_schema(false);
assert_eq!(
*result
.pointer("/properties/illustration/properties/user/properties/fullname/value")
.unwrap(),
json!("John Doe")
);
// Update data and selectively re-evaluate only fullname
let updated_data = json!({
"first": "Jane",
"last": "Smith"
});
let updated_data_str = serde_json::to_string(&updated_data).unwrap();
// Test both path formats
let paths_dot = vec!["illustration.user.fullname".to_string()];
eval.evaluate(&updated_data_str, None, Some(&paths_dot), None)
.unwrap();
let result2 = eval.get_evaluated_schema(false);
let fullname = result2
.pointer("/properties/illustration/properties/user/properties/fullname/value")
.unwrap();
assert_eq!(*fullname, json!("Jane Smith"), "fullname should be updated");
// firstName should NOT be updated (kept old value from schema)
let firstname = result2
.pointer("/properties/illustration/properties/user/properties/firstName/value")
.unwrap();
assert_eq!(*firstname, json!("John"), "firstName should NOT be updated");
}
#[test]
fn test_selective_eval_explicit_properties_path() {
// Test with explicit "properties" keywords in the path
let schema = json!({
"type": "object",
"properties": {
"illustration": {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"fullname": {
"value": {
"$evaluation": {
"CONCAT": [
{
"$ref": "first"
},
" ",
{
"$ref": "last"
}
]
}
}
}
}
}
}
}
}
});
let data = json!({
"first": "John",
"last": "Doe"
});
let schema_str = serde_json::to_string(&schema).unwrap();
let data_str = serde_json::to_string(&data).unwrap();
let mut eval = JSONEval::new(&schema_str, None, Some(&data_str)).unwrap();
// Initial evaluation
eval.evaluate(&data_str, None, None, None).unwrap();
// Update data and use explicit properties path (as user specified)
let updated_data = json!({
"first": "Jane",
"last": "Smith"
});
let updated_data_str = serde_json::to_string(&updated_data).unwrap();
// User's exact path format
let paths = vec!["illustration.properties.user.properties.fullname".to_string()];
eval.evaluate(&updated_data_str, None, Some(&paths), None)
.unwrap();
let result = eval.get_evaluated_schema(false);
let fullname = result
.pointer("/properties/illustration/properties/user/properties/fullname/value")
.unwrap();
assert_eq!(*fullname, json!("Jane Smith"));
}