jmespath_extensions 0.9.0

Extended functions for JMESPath queries - 400+ functions for strings, arrays, dates, hashing, encoding, geo, and more
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
//! Data validation functions.
//!
//! This module provides validation functions for JMESPath queries.
//!
//! For complete function reference with signatures and examples, see the
//! [`functions`](crate::functions) module documentation or use `jpx --list-category validation`.
//!
//! # Example
//!
//! ```rust
//! use jmespath::{Runtime, Variable};
//! use jmespath_extensions::validation;
//!
//! let mut runtime = Runtime::new();
//! runtime.register_builtin_functions();
//! validation::register(&mut runtime);
//! ```

use std::collections::HashSet;
use std::rc::Rc;

use crate::common::{
    ArgumentType, Context, ErrorReason, Function, JmespathError, Rcvar, Runtime, Variable,
};
use crate::define_function;
use crate::register_if_enabled;

#[cfg(feature = "regex")]
use regex::Regex;

/// Register all validation functions with the runtime.
pub fn register(runtime: &mut Runtime) {
    #[cfg(feature = "regex")]
    {
        runtime.register_function("is_email", Box::new(IsEmailFn::new()));
        runtime.register_function("is_url", Box::new(IsUrlFn::new()));
        runtime.register_function("is_uuid", Box::new(IsUuidFn::new()));
        runtime.register_function("is_phone", Box::new(IsPhoneFn::new()));
    }
    runtime.register_function("is_ipv4", Box::new(IsIpv4Fn::new()));
    runtime.register_function("is_ipv6", Box::new(IsIpv6Fn::new()));
    runtime.register_function("luhn_check", Box::new(LuhnCheckFn::new()));
    runtime.register_function("is_credit_card", Box::new(IsCreditCardFn::new()));
    runtime.register_function("is_jwt", Box::new(IsJwtFn::new()));
    runtime.register_function("is_iso_date", Box::new(IsIsoDateFn::new()));
    runtime.register_function("is_json", Box::new(IsJsonFn::new()));
    runtime.register_function("is_base64", Box::new(IsBase64Fn::new()));
    runtime.register_function("is_hex", Box::new(IsHexFn::new()));
}

/// Register validation functions with the runtime, filtered by the enabled set.
pub fn register_filtered(runtime: &mut Runtime, enabled: &HashSet<&str>) {
    #[cfg(feature = "regex")]
    {
        register_if_enabled!(runtime, enabled, "is_email", Box::new(IsEmailFn::new()));
        register_if_enabled!(runtime, enabled, "is_url", Box::new(IsUrlFn::new()));
        register_if_enabled!(runtime, enabled, "is_uuid", Box::new(IsUuidFn::new()));
        register_if_enabled!(runtime, enabled, "is_phone", Box::new(IsPhoneFn::new()));
    }
    register_if_enabled!(runtime, enabled, "is_ipv4", Box::new(IsIpv4Fn::new()));
    register_if_enabled!(runtime, enabled, "is_ipv6", Box::new(IsIpv6Fn::new()));
    register_if_enabled!(runtime, enabled, "luhn_check", Box::new(LuhnCheckFn::new()));
    register_if_enabled!(
        runtime,
        enabled,
        "is_credit_card",
        Box::new(IsCreditCardFn::new())
    );
    register_if_enabled!(runtime, enabled, "is_jwt", Box::new(IsJwtFn::new()));
    register_if_enabled!(
        runtime,
        enabled,
        "is_iso_date",
        Box::new(IsIsoDateFn::new())
    );
    register_if_enabled!(runtime, enabled, "is_json", Box::new(IsJsonFn::new()));
    register_if_enabled!(runtime, enabled, "is_base64", Box::new(IsBase64Fn::new()));
    register_if_enabled!(runtime, enabled, "is_hex", Box::new(IsHexFn::new()));
}

// =============================================================================
// is_email(string) -> boolean
// =============================================================================

#[cfg(feature = "regex")]
define_function!(IsEmailFn, vec![ArgumentType::String], None);

#[cfg(feature = "regex")]
impl Function for IsEmailFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        let email_re = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap();
        Ok(Rc::new(Variable::Bool(email_re.is_match(s))))
    }
}

// =============================================================================
// is_url(string) -> boolean
// =============================================================================

#[cfg(feature = "regex")]
define_function!(IsUrlFn, vec![ArgumentType::String], None);

#[cfg(feature = "regex")]
impl Function for IsUrlFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        let url_re = Regex::new(r"^https?://[^\s/$.?#].[^\s]*$").unwrap();
        Ok(Rc::new(Variable::Bool(url_re.is_match(s))))
    }
}

// =============================================================================
// is_uuid(string) -> boolean
// =============================================================================

#[cfg(feature = "regex")]
define_function!(IsUuidFn, vec![ArgumentType::String], None);

#[cfg(feature = "regex")]
impl Function for IsUuidFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        let uuid_re = Regex::new(
            r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$",
        )
        .unwrap();
        Ok(Rc::new(Variable::Bool(uuid_re.is_match(s))))
    }
}

// =============================================================================
// is_ipv4(string) -> boolean
// =============================================================================

define_function!(IsIpv4Fn, vec![ArgumentType::String], None);

impl Function for IsIpv4Fn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        let is_valid = s.parse::<std::net::Ipv4Addr>().is_ok();
        Ok(Rc::new(Variable::Bool(is_valid)))
    }
}

// =============================================================================
// is_ipv6(string) -> boolean
// =============================================================================

define_function!(IsIpv6Fn, vec![ArgumentType::String], None);

impl Function for IsIpv6Fn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        let is_valid = s.parse::<std::net::Ipv6Addr>().is_ok();
        Ok(Rc::new(Variable::Bool(is_valid)))
    }
}

// =============================================================================
// luhn_check(string) -> boolean - Generic Luhn algorithm check
// =============================================================================

define_function!(LuhnCheckFn, vec![ArgumentType::String], None);

impl Function for LuhnCheckFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        Ok(Rc::new(Variable::Bool(luhn_validate(s))))
    }
}

fn luhn_validate(s: &str) -> bool {
    // Remove spaces and dashes
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();

    if digits.is_empty() {
        return false;
    }

    let mut sum = 0;
    let mut double = false;

    for c in digits.chars().rev() {
        if let Some(digit) = c.to_digit(10) {
            let mut d = digit;
            if double {
                d *= 2;
                if d > 9 {
                    d -= 9;
                }
            }
            sum += d;
            double = !double;
        } else {
            return false;
        }
    }

    sum % 10 == 0
}

// =============================================================================
// is_credit_card(string) -> boolean - Validate credit card number
// =============================================================================

define_function!(IsCreditCardFn, vec![ArgumentType::String], None);

impl Function for IsCreditCardFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        // Remove spaces and dashes
        let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();

        // Credit cards are typically 13-19 digits
        if digits.len() < 13 || digits.len() > 19 {
            return Ok(Rc::new(Variable::Bool(false)));
        }

        // Must pass Luhn check
        Ok(Rc::new(Variable::Bool(luhn_validate(&digits))))
    }
}

// =============================================================================
// is_phone(string) -> boolean - Validate phone number format
// =============================================================================

#[cfg(feature = "regex")]
define_function!(IsPhoneFn, vec![ArgumentType::String], None);

#[cfg(feature = "regex")]
impl Function for IsPhoneFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        // Basic phone pattern: optional + followed by digits, spaces, dashes, parens
        // Minimum 7 digits for a valid phone number
        let phone_re = Regex::new(r"^\+?[\d\s\-\(\)\.]{7,}$").unwrap();
        if !phone_re.is_match(s) {
            return Ok(Rc::new(Variable::Bool(false)));
        }

        // Count actual digits - need at least 7
        let digit_count = s.chars().filter(|c| c.is_ascii_digit()).count();
        Ok(Rc::new(Variable::Bool((7..=15).contains(&digit_count))))
    }
}

// =============================================================================
// is_jwt(string) -> boolean - Check if valid JWT structure
// =============================================================================

define_function!(IsJwtFn, vec![ArgumentType::String], None);

impl Function for IsJwtFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        // JWT has 3 base64url-encoded parts separated by dots
        let parts: Vec<&str> = s.split('.').collect();
        if parts.len() != 3 {
            return Ok(Rc::new(Variable::Bool(false)));
        }

        // Check each part is valid base64url (alphanumeric, -, _, no padding required)
        let is_valid = parts.iter().all(|part| {
            !part.is_empty()
                && part
                    .chars()
                    .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '=')
        });

        Ok(Rc::new(Variable::Bool(is_valid)))
    }
}

// =============================================================================
// is_iso_date(string) -> boolean - Validate ISO 8601 date format
// =============================================================================

define_function!(IsIsoDateFn, vec![ArgumentType::String], None);

impl Function for IsIsoDateFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        // Try parsing as RFC3339 (subset of ISO 8601)
        if chrono::DateTime::parse_from_rfc3339(s).is_ok() {
            return Ok(Rc::new(Variable::Bool(true)));
        }

        // Try parsing as date only (YYYY-MM-DD)
        if chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").is_ok() {
            return Ok(Rc::new(Variable::Bool(true)));
        }

        // Try parsing as datetime without timezone
        if chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S").is_ok() {
            return Ok(Rc::new(Variable::Bool(true)));
        }

        Ok(Rc::new(Variable::Bool(false)))
    }
}

// =============================================================================
// is_json(string) -> boolean - Check if string is valid JSON
// =============================================================================

define_function!(IsJsonFn, vec![ArgumentType::String], None);

impl Function for IsJsonFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        let is_valid = serde_json::from_str::<serde_json::Value>(s).is_ok();
        Ok(Rc::new(Variable::Bool(is_valid)))
    }
}

// =============================================================================
// is_base64(string) -> boolean - Check if valid Base64 encoding
// =============================================================================

define_function!(IsBase64Fn, vec![ArgumentType::String], None);

impl Function for IsBase64Fn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        use base64::{Engine, engine::general_purpose::STANDARD};
        let is_valid = STANDARD.decode(s).is_ok();
        Ok(Rc::new(Variable::Bool(is_valid)))
    }
}

// =============================================================================
// is_hex(string) -> boolean - Check if valid hexadecimal string
// =============================================================================

define_function!(IsHexFn, vec![ArgumentType::String], None);

impl Function for IsHexFn {
    fn evaluate(&self, args: &[Rcvar], ctx: &mut Context<'_>) -> Result<Rcvar, JmespathError> {
        self.signature.validate(args, ctx)?;

        let s = args[0].as_string().ok_or_else(|| {
            JmespathError::new(
                ctx.expression,
                0,
                ErrorReason::Parse("Expected string argument".to_owned()),
            )
        })?;

        // Must be non-empty and all hex chars
        let is_valid = !s.is_empty() && s.chars().all(|c| c.is_ascii_hexdigit());
        Ok(Rc::new(Variable::Bool(is_valid)))
    }
}

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

    fn setup_runtime() -> Runtime {
        let mut runtime = Runtime::new();
        runtime.register_builtin_functions();
        register(&mut runtime);
        runtime
    }

    #[test]
    fn test_is_ipv4() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_ipv4(@)").unwrap();

        let data = Variable::String("192.168.1.1".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());

        let data = Variable::String("not an ip".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_ipv6() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_ipv6(@)").unwrap();

        let data = Variable::String("::1".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());

        let data = Variable::String("2001:db8::1".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[cfg(feature = "regex")]
    #[test]
    fn test_is_email() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_email(@)").unwrap();

        let data = Variable::String("test@example.com".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());

        let data = Variable::String("not-an-email".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_luhn_check_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("luhn_check(@)").unwrap();

        // Valid Luhn number
        let data = Variable::String("79927398713".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_luhn_check_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("luhn_check(@)").unwrap();

        let data = Variable::String("79927398710".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_credit_card_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_credit_card(@)").unwrap();

        // Test Visa number (passes Luhn)
        let data = Variable::String("4111111111111111".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_credit_card_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_credit_card(@)").unwrap();

        // Invalid number
        let data = Variable::String("1234567890123456".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_credit_card_too_short() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_credit_card(@)").unwrap();

        let data = Variable::String("123456".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[cfg(feature = "regex")]
    #[test]
    fn test_is_phone_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_phone(@)").unwrap();

        let data = Variable::String("+1-555-123-4567".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());

        let data = Variable::String("(555) 123-4567".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[cfg(feature = "regex")]
    #[test]
    fn test_is_phone_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_phone(@)").unwrap();

        let data = Variable::String("123".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_jwt_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_jwt(@)").unwrap();

        let data = Variable::String(
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U".to_string()
        );
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_jwt_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_jwt(@)").unwrap();

        // Only two parts - invalid
        let data = Variable::String("only.twoparts".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());

        // Contains invalid characters for base64url
        let data = Variable::String("abc.def!ghi.jkl".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_iso_date_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_iso_date(@)").unwrap();

        let data = Variable::String("2023-12-13T15:30:00Z".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());

        let data = Variable::String("2023-12-13".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_iso_date_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_iso_date(@)").unwrap();

        let data = Variable::String("12/13/2023".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_json_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_json(@)").unwrap();

        let data = Variable::String(r#"{"a": 1, "b": [2, 3]}"#.to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_json_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_json(@)").unwrap();

        let data = Variable::String("not json".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_base64_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_base64(@)").unwrap();

        let data = Variable::String("SGVsbG8gV29ybGQ=".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_base64_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_base64(@)").unwrap();

        let data = Variable::String("not valid base64!!!".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_hex_valid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_hex(@)").unwrap();

        let data = Variable::String("deadbeef".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());

        let data = Variable::String("ABCDEF0123456789".to_string());
        let result = expr.search(&data).unwrap();
        assert!(result.as_boolean().unwrap());
    }

    #[test]
    fn test_is_hex_invalid() {
        let runtime = setup_runtime();
        let expr = runtime.compile("is_hex(@)").unwrap();

        let data = Variable::String("not hex!".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());

        let data = Variable::String("".to_string());
        let result = expr.search(&data).unwrap();
        assert!(!result.as_boolean().unwrap());
    }
}