jsonparser 0.2.1

A Rust crate for efficient parsing and validation of JSON data into strongly typed Rust data structures, enabling data integrity checks.
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
use crate::{JSONValue, OrderedMap};

pub struct JSONSchema<'a> {
	rules: OrderedMap<Box<dyn Validator + 'a>>
}

impl<'a> JSONSchema<'a> {
    /// Create a new JSONSchema instance with the given rules.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use jsonparser::{JSONSchema, StringType, NumberType};
    ///
    /// let schema = JSONSchema::new([
    ///   ("name", StringType::new().min_length(6).boxed()),
    ///   ("age", NumberType::new().gt(18.0).boxed())
    /// ]);
    /// ```
    pub fn new<T: IntoIterator<Item = (&'a str, Box<dyn Validator + 'a>)>>(rules: T) -> Self {
        let mut ordered_rules = OrderedMap::new();

        for (key, rule) in rules {
            ordered_rules.insert(key, rule);
        }

        Self { rules: ordered_rules }
    }

    /// Validate the given JSONValue against the schema.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use jsonparser::{JSONValue, JSONSchema, StringType, NumberType};
    ///
    /// let schema = JSONSchema::new([
    ///   ("name", StringType::new().min_length(3).trim().boxed()),
    ///   ("age", NumberType::new().gt(18.0).boxed())
    /// ]);
    ///
    /// let json = JSONValue::Object({ /* ... */ });
    ///
    /// match schema.validate(&json) {
    ///   Ok(value: JSONValue) => println!("{:?}", value),
    ///   Err(e) => eprintln!("Invalid JSON: {}", e)
    /// }
    /// ```
    pub fn validate(&self, value: &JSONValue) -> Result<JSONValue, String> {
        let transformed = &self.transform(value)?;

        match transformed {
            JSONValue::Object(obj) => {
                for (key, rule) in self.rules.iter() {
                    match obj.get(key as &str) {
                        Some(value) => rule.validate(key, value)?,
                        None => return Err(format!("Key '{}' not found", key))
                    }
                }
                Ok(transformed.clone())
            },
            _ => Err("Expected an object for validation".to_string()),
        }
    }

    /// Transform the given JSONValue according to the schema.
    fn transform(&self, value: &JSONValue) -> Result<JSONValue, String> {
        match value {
            JSONValue::Object(obj) => {
                let mut transformed = obj.clone();

                for (key, rule) in self.rules.iter() {
                    if let Some(value) = obj.get(key as &str) {
                        transformed.insert(key, rule.transform(key, value)?);
                    }
                }
                Ok(JSONValue::Object(transformed))
            },
            _ => Err("Expected an object for transformation".to_string()),
        }
    }
}

pub trait Validator {
    fn validate(&self, name: &str, value: &JSONValue) -> Result<(), String>;
    fn transform(&self, _: &str, value: &JSONValue) -> Result<JSONValue, String> {
        Ok(value.clone())
    }
}

pub struct StringType {
    min_length: Option<usize>,
    max_length: Option<usize>,
    length: Option<usize>,
    starts_with: Option<String>,
    ends_with: Option<String>,
    includes: Option<String>,
    trim: bool,
    trim_start: bool,
    trim_end: bool,
    lowercase: bool,
    uppercase: bool,
    transform: Option<Box<dyn Fn(&str) -> String>>
}

impl StringType {
    /// Create a new StringType instance.
    pub fn new() -> Self {
        Self {
            min_length: None,
            max_length: None,
            length: None,
            starts_with: None,
            ends_with: None,
            includes: None,
            trim: false,
            trim_start: false,
            trim_end: false,
            lowercase: false,
            uppercase: false,
            transform: None
        }
    }

    /// Set the minimum length of the string.
    pub fn min_length(mut self, min: usize) -> Self {
        self.min_length = Some(min);
        self
    }

    /// Set the maximum length of the string.
    pub fn max_length(mut self, max: usize) -> Self {
        self.max_length = Some(max);
        self
    }

    /// Set the exact length of the string.
    pub fn length(mut self, length: usize) -> Self {
        self.length = Some(length);
        self
    }

    /// Set the expected starting of the string.
    pub fn starts_with(mut self, value: &str) -> Self {
        self.starts_with = Some(value.to_string());
        self
    }

    /// Set the expected ending of the string.
    pub fn ends_with(mut self, value: &str) -> Self {
        self.ends_with = Some(value.to_string());
        self
    }

    /// Set the string to include a specific substring.
    pub fn includes(mut self, value: &str) -> Self {
        self.includes = Some(value.to_string());
        self
    }

    /// Trim the string before validation.
    pub fn trim(mut self) -> Self {
        self.trim = true;
        self
    }

    /// Trim the start of the string before validation.
    pub fn trim_start(mut self) -> Self {
        self.trim_start = true;
        self
    }

    /// Trim the end of the string before validation.
    pub fn trim_end(mut self) -> Self {
        self.trim_end = true;
        self
    }

    /// Convert the string to lowercase before validation.
    pub fn to_lowercase(mut self) -> Self {
        self.lowercase = true;
        self
    }

    /// Convert the string to uppercase before validation.
    pub fn to_uppercase(mut self) -> Self {
        self.uppercase = true;
        self
    }

    /// Set a custom transformation function for the string.
    pub fn transform<F: 'static + Fn(&str) -> String>(mut self, transform: F) -> Self {
        self.transform = Some(Box::new(transform));
        self
    }

    /// Convert the StringType to a Box<dyn Validator>.
    pub fn boxed(self) -> Box<dyn Validator> {
        Box::new(self)
    }
}

impl Validator for StringType {
    fn validate(&self, key: &str, value: &JSONValue) -> Result<(), String> {
        match value {
            JSONValue::String(s) => {
                if let Some(min) = self.min_length {
                    if s.len() < min {
                        return Err(format!("{} is too short (min: {})", key, min));
                    }
                }

                if let Some(max) = self.max_length {
                    if s.len() > max {
                        return Err(format!("{} is too long (max: {})", key, max));
                    }
                }

                if let Some(length) = self.length {
                    if s.len() != length {
                        return Err(format!("{} is not the correct length (length: {})", key, length));
                    }
                }

                if let Some(starts_with) = &self.starts_with {
                    if !s.starts_with(starts_with) {
                        return Err(format!("{} does not start with '{}'", key, starts_with));
                    }
                }

                if let Some(ends_with) = &self.ends_with {
                    if !s.ends_with(ends_with) {
                        return Err(format!("{} does not end with '{}'", key, ends_with));
                    }
                }

                if let Some(includes) = &self.includes {
                    if !s.contains(includes) {
                        return Err(format!("{} does not include '{}'", key, includes));
                    }
                }

                Ok(())
            },
            _ => Err(format!("Type of {} mismatch, expected String", key))
        }
    }

    fn transform(&self, key: &str, value: &JSONValue) -> Result<JSONValue, String> {
        match value {
            JSONValue::String(s) => {
                let mut transformed = s.clone();

                if self.trim {
                    transformed = transformed.trim().to_string();
                }

                if self.trim_start {
                    transformed = transformed.trim_start().to_string();
                }

                if self.trim_end {
                    transformed = transformed.trim_end().to_string();
                }

                if self.lowercase {
                    transformed = transformed.to_lowercase();
                }

                if self.uppercase {
                    transformed = transformed.to_uppercase();
                }

                if let Some(transform) = &self.transform {
                    transformed = transform(&transformed);
                }

                Ok(JSONValue::String(transformed))
            },
            _ => Err(format!("Type of {} mismatch, expected String", key))
        }
    }
}

pub struct NumberType {
    min: Option<f64>,
    max: Option<f64>,
    integer: Option<bool>,
    floor: bool,
    ceil: bool,
    round: bool,
    transform: Option<Box<dyn Fn(f64) -> f64>>
}

impl NumberType {
    /// Create a new NumberType instance.
    pub fn new() -> Self {
        Self {
            min: None,
            max: None,
            integer: None,
            floor: false,
            ceil: false,
            round: false,
            transform: None
        }
    }

    /// Set the minimum value of the number.
    pub fn gt(mut self, value: f64) -> Self {
        self.min = Some(value);
        self
    }

    /// Set the maximum value of the number.
    pub fn lt(mut self, value: f64) -> Self {
        self.max = Some(value);
        self
    }

    /// Set whether the number should be an integer.
    pub fn integer(mut self) -> Self {
        self.integer = Some(true);
        self
    }

    /// Round the number down before validation.
    pub fn floor(mut self) -> Self {
        self.floor = true;
        self
    }

    /// Round the number up before validation.
    pub fn ceil(mut self) -> Self {
        self.ceil = true;
        self
    }

    /// Round the number to the nearest integer before validation.
    pub fn round(mut self) -> Self {
        self.round = true;
        self
    }

    /// Set a custom transformation function for the number.
    pub fn transform<F: 'static + Fn(f64) -> f64>(mut self, transform: F) -> Self {
        self.transform = Some(Box::new(transform));
        self
    }

    /// Convert the NumberType to a Box<dyn Validator>.
    pub fn boxed(self) -> Box<dyn Validator> {
        Box::new(self)
    }
}

impl Validator for NumberType {
    fn validate(&self, key: &str, value: &JSONValue) -> Result<(), String> {
        match value {
            JSONValue::Number(n) => {
                if let Some(min) = self.min {
                    if n < &min {
                        return Err(format!("{} is too small (min: {})", key, min));
                    }
                }

                if let Some(max) = self.max {
                    if n > &max {
                        return Err(format!("{} is too large (max: {})", key, max));
                    }
                }

                if let Some(integer) = self.integer {
                    if integer && !n.fract().eq(&0.0) {
                        return Err(format!("{} is not an integer", key));
                    }
                }

                Ok(())
            },
            _ => Err(format!("Type of {} mismatch, expected Number", key))
        }
    }

    fn transform(&self, key: &str, value: &JSONValue) -> Result<JSONValue, String> {
        match value {
            JSONValue::Number(n) => {
                let mut transformed = n.clone();

                if self.floor {
                    transformed = transformed.floor();
                }

                if self.ceil {
                    transformed = transformed.ceil();
                }

                if self.round {
                    transformed = transformed.round();
                }

                if let Some(transform) = &self.transform {
                    transformed = transform(transformed);
                }

                Ok(JSONValue::Number(transformed))
            },
            _ => Err(format!("Type of {} mismatch, expected Number", key))
        }
    }
}

pub struct ArrayType {
    min_length: Option<usize>,
    max_length: Option<usize>,
    length: Option<usize>,
    empty: Option<bool>,
    every: Option<Box<dyn Validator>>,
    some: Option<Box<dyn Validator>>,
    at: Option<(usize, Box<dyn Validator>)>,
    truncate: Option<usize>,
    transform: Option<Box<dyn Fn(Vec<JSONValue>) -> Vec<JSONValue>>>
}

impl ArrayType {
    /// Create a new ArrayType instance.
    pub fn new() -> Self {
        Self {
            min_length: None,
            max_length: None,
            length: None,
            empty: None,
            every: None,
            some: None,
            at: None,
            truncate: None,
            transform: None
        }
    }

    /// Set the minimum length of the array.
    pub fn min_length(mut self, min: usize) -> Self {
        self.min_length = Some(min);
        self
    }

    /// Set the maximum length of the array.
    pub fn max_length(mut self, max: usize) -> Self {
        self.max_length = Some(max);
        self
    }

    /// Set the exact length of the array.
    pub fn length(mut self, length: usize) -> Self {
        self.length = Some(length);
        self
    }

    /// Set whether the array can be empty.
    pub fn empty(mut self) -> Self {
        self.empty = Some(true);
        self
    }

    /// Set a rule for every items in the array.
    pub fn every(mut self, rule: Box<dyn Validator>) -> Self {
        self.every = Some(rule);
        self
    }

    /// Set a rule for at least one item in the array.
    pub fn some(mut self, rule: Box<dyn Validator>) -> Self {
        self.some = Some(rule);
        self
    }

    /// Set a rule for a specific item in the array.
    pub fn at(mut self, index: usize, rule: Box<dyn Validator>) -> Self {
        self.at = Some((index, rule));
        self
    }

    /// Truncate the array before validation.
    pub fn truncate(mut self, length: usize) -> Self {
        self.truncate = Some(length);
        self
    }

    /// Set a custom transformation function for the array.
    pub fn transform<F: 'static + Fn(Vec<JSONValue>) -> Vec<JSONValue>>(mut self, transform: F) -> Self {
        self.transform = Some(Box::new(transform));
        self
    }

    /// Convert the ArrayType to a Box<dyn Validator>.
    pub fn boxed(self) -> Box<dyn Validator> {
        Box::new(self)
    }
}

impl Validator for ArrayType {
    fn validate(&self, key: &str, value: &JSONValue) -> Result<(), String> {
        match value {
            JSONValue::Array(arr) => {
                if let Some(min) = self.min_length {
                    if arr.len() < min {
                        return Err(format!("{} is too short (min: {})", key, min));
                    }
                }

                if let Some(max) = self.max_length {
                    if arr.len() > max {
                        return Err(format!("{} is too long (max: {})", key, max));
                    }
                }

                if let Some(length) = self.length {
                    if arr.len() != length {
                        return Err(format!("{} is not the correct length (length: {})", key, length));
                    }
                }

                if let Some(empty) = self.empty {
                    if empty && arr.is_empty() {
                        return Err(format!("{} is empty", key));
                    }
                }

                if let Some(rule) = &self.every {
                    for item in arr {
                        rule.validate(key, item)?;
                    }
                }

                if let Some(rule) = &self.some {
                    for item in arr {
                        if rule.validate(key, item).is_ok() {
                            return Ok(());
                        }
                    }
                    return Err(format!("No items in the {} match the rule", key));
                }

                if let Some((index, rule)) = &self.at {
                    if let Some(item) = arr.get(*index) {
                        rule.validate(key, item)?;
                    } else {
                        return Err(format!("In {}, index {} not found", key, index));
                    }
                }

                Ok(())
            },
            _ => Err(format!("Type of {} mismatch, expected Array", key))
        }
    }

    fn transform(&self, key: &str, value: &JSONValue) -> Result<JSONValue, String> {
        match value {
            JSONValue::Array(arr) => {
                let mut transformed = arr.clone();

                if let Some(len) = self.truncate {
                    transformed.truncate(len);
                }

                if let Some(transform) = &self.transform {
                    transformed = transform(transformed);
                }

                Ok(JSONValue::Array(transformed))
            },
            _ => Err(format!("Type of {} mismatch, expected Array", key))
        }
    }
}

pub struct BooleanType {
    value: Option<bool>,
    transform: Option<Box<dyn Fn(bool) -> bool>>
}

impl BooleanType {
    /// Create a new BooleanType instance.
    pub fn new() -> Self {
        Self {
            value: None,
            transform: None
        }
    }

    /// Set the expected value to true.
    pub fn truthy(mut self) -> Self {
        self.value = Some(true);
        self
    }

    /// Set the expected value to false.
    pub fn falsy(mut self) -> Self {
        self.value = Some(false);
        self
    }

    /// Set a custom transformation function for the boolean.
    pub fn transform<F: 'static + Fn(bool) -> bool>(mut self, transform: F) -> Self {
        self.transform = Some(Box::new(transform));
        self
    }

    /// Convert the BooleanType to a Box<dyn Validator>.
    pub fn boxed(self) -> Box<dyn Validator> {
        Box::new(self)
    }
}

impl Validator for BooleanType {
    fn validate(&self, key: &str, value: &JSONValue) -> Result<(), String> {
        match value {
            JSONValue::Boolean(b) => {
                if let Some(expected) = self.value {
                    if b != &expected {
                        return Err(format!("For {}, expected {}", key, expected));
                    }
                }

                Ok(())
            },
            _ => Err(format!("Type of {} mismatch, expected Boolean", key))
        }
    }

    fn transform(&self, key: &str,value: &JSONValue) -> Result<JSONValue, String> {
        match value {
            JSONValue::Boolean(b) => {
                let mut transformed = *b;

                if let Some(transform) = &self.transform {
                    transformed = transform(transformed);
                }

                Ok(JSONValue::Boolean(transformed))
            },
            _ => Err(format!("Type of {} mismatch, expected Boolean", key))
        }
    }
}

pub struct ObjectType<'a> {
    rules: OrderedMap<Box<dyn Validator + 'a>>
}

impl<'a> ObjectType<'a> {
    /// Create a new ObjectType instance.
    pub fn new() -> Self {
        Self {
            rules: OrderedMap::new()
        }
    }

    /// Add a rule for a property in the object.
    pub fn property(mut self, key: &str, rule: Box<dyn Validator>) -> Self {
        self.rules.insert(key, rule);
        self
    }

    /// Convert the ObjectType to a Box<dyn Validator>.
    pub fn boxed(self) -> Box<dyn Validator + 'a> {
        Box::new(self)
    }
}

impl<'a> Validator for ObjectType<'a> {
    fn validate(&self, key: &str, value: &JSONValue) -> Result<(), String> {
        match value {
            JSONValue::Object(obj) => {
                for (subkey, rule) in self.rules.iter() {
                    match obj.get(subkey as &str) {
                        Some(value) => rule.validate(subkey, value)?,
                        None => return Err(format!("In {}, key '{}' not found", key, subkey))
                    }
                }
                Ok(())
            },
            _ => Err(format!("Type of {} mismatch, expected Object", key))
        }
    }
}

pub struct NullType;

impl NullType {
    /// Create a new NullType instance.
    pub fn new() -> Self {
        Self
    }

    /// Convert the NullType to a Box<dyn Validator>.
    pub fn boxed(self) -> Box<dyn Validator> {
        Box::new(self)
    }
}

impl Validator for NullType {
    fn validate(&self, key: &str, value: &JSONValue) -> Result<(), String> {
        match value {
            JSONValue::Null => Ok(()),
            _ => Err(format!("Type of {} mismatch, expected Null", key)),
        }
    }
}