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
use serde_json::{Value};
use std::cmp;
use url;

use super::super::errors;
use super::super::scope;

#[derive(Debug)]
pub enum ItemsKind {
    Schema(url::Url),
    Array(Vec<url::Url>)
}

#[derive(Debug)]
pub enum AdditionalKind {
    Boolean(bool),
    Schema(url::Url)
}

#[allow(missing_copy_implementations)]
pub struct Items {
    pub items: Option<ItemsKind>,
    pub additional: Option<AdditionalKind>
}

impl super::Validator for Items {
    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
        let array = nonstrict_process!(val.as_array(), path);

        let mut state = super::ValidationState::new();

        match self.items {
            Some(ItemsKind::Schema(ref url)) => {
                // Just validate all items against the schema

                let schema = scope.resolve(url);
                if schema.is_some() {
                    let schema = schema.unwrap();
                    for (idx, item) in array.iter().enumerate() {
                        let item_path = [path, idx.to_string().as_ref()].join("/");
                        state.append(schema.validate_in(item, item_path.as_ref()));
                    }
                } else {
                    state.missing.push(url.clone());
                }
            },
            Some(ItemsKind::Array(ref urls)) => {
                let min = cmp::min(urls.len(), array.len());

                // Validate against schemas
                for idx in 0..min {
                    let schema = scope.resolve(&urls[idx]);
                    let item = &array[idx];

                    if schema.is_some() {
                        let item_path = [path, idx.to_string().as_ref()].join("/");
                        state.append(schema.unwrap().validate_in(item, item_path.as_ref()))
                    } else {
                        state.missing.push(urls[idx].clone())
                    }
                }

                // Validate agains additional items
                if array.len() > urls.len() {
                    match self.additional {
                        Some(AdditionalKind::Boolean(allow)) if allow == false => {
                            state.errors.push(Box::new(
                                errors::Items {
                                    path: path.to_string(),
                                    detail: "Additional items are not allowed".to_string()
                                }
                            ))
                        },
                        Some(AdditionalKind::Schema(ref url)) => {
                            let schema = scope.resolve(url);
                            if schema.is_some() {
                                let schema = schema.unwrap();
                                for (idx, item) in array[urls.len()..].iter().enumerate() {
                                    let item_path = [path, idx.to_string().as_ref()].join("/");
                                    state.append(schema.validate_in(item, item_path.as_ref()))
                                }
                            } else {
                                state.missing.push(url.clone())
                            }
                        },
                        _ => ()
                    }
                }
            }
            _ => ()
        }

        state
    }
}