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

use super::super::schema;
use super::super::validators;
use super::super::helpers;

macro_rules! of_keyword{
    ($name:ident, $kw:expr) => {

        #[allow(missing_copy_implementations)]
        pub struct $name;
        impl super::Keyword for $name {
            fn compile(&self, def: &Value, ctx: &schema::WalkContext) -> super::KeywordResult {
                let of = keyword_key_exists!(def, $kw);

                if of.is_array() {
                    let of = of.as_array().unwrap();

                    if of.len() == 0 {
                        return Err(schema::SchemaError::Malformed {
                            path: ctx.fragment.join("/"),
                            detail: "This array MUST have at least one element.".to_string()
                        })
                    }

                    let mut schemes = vec![];
                    for (idx, scheme) in of.iter().enumerate() {
                        if scheme.is_object() {
                            schemes.push(
                                helpers::alter_fragment_path(ctx.url.clone(), [
                                    ctx.escaped_fragment().as_ref(),
                                    $kw,
                                    idx.to_string().as_ref()
                                ].join("/"))
                            )
                        } else {
                            return Err(schema::SchemaError::Malformed {
                                path: ctx.fragment.join("/"),
                                detail: "Elements of the array MUST be objects.".to_string()
                            })
                        }
                    }

                    Ok(Some(Box::new(validators::$name {
                        schemes: schemes
                    })))
                } else {
                    Err(schema::SchemaError::Malformed {
                        path: ctx.fragment.join("/"),
                        detail: "The value of this keyword MUST be an array.".to_string()
                    })
                }
            }
        }

    }
}

of_keyword!(AllOf, "allOf");
of_keyword!(AnyOf, "anyOf");
of_keyword!(OneOf, "oneOf");

#[cfg(test)] use super::super::scope;
#[cfg(test)] use super::super::builder;
#[cfg(test)] use serde_json::to_value;

#[test]
fn validate_all_of() {
    let mut scope = scope::Scope::new();
    let schema = scope.compile_and_return(builder::schema(|s| {
        s.all_of(|all_of| {
            all_of.push(|schema| {
                schema.minimum(5f64, false);
            });
            all_of.push(|schema| {
                schema.maximum(10f64, false);
            });
        });
    }).into_json(), true).ok().unwrap();

    assert_eq!(schema.validate(&to_value(&7).unwrap()).is_valid(), true);
    assert_eq!(schema.validate(&to_value(&4).unwrap()).is_valid(), false);
    assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), false);
}

#[test]
fn validate_any_of() {
    let mut scope = scope::Scope::new();
    let schema = scope.compile_and_return(builder::schema(|s| {
        s.any_of(|all_of| {
            all_of.push(|schema| {
                schema.maximum(5f64, false);
            });
            all_of.push(|schema| {
                schema.maximum(10f64, false);
            });
        });
    }).into_json(), true).ok().unwrap();

    assert_eq!(schema.validate(&to_value(&5).unwrap()).is_valid(), true);
    assert_eq!(schema.validate(&to_value(&10).unwrap()).is_valid(), true);
    assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), false);
}

#[test]
fn validate_one_of() {
    let mut scope = scope::Scope::new();
    let schema = scope.compile_and_return(builder::schema(|s| {
        s.one_of(|all_of| {
            all_of.push(|schema| {
                schema.maximum(5f64, false);
            });
            all_of.push(|schema| {
                schema.maximum(10f64, false);
            });
        });
    }).into_json(), true).ok().unwrap();

    assert_eq!(schema.validate(&to_value(&5).unwrap()).is_valid(), false);
    assert_eq!(schema.validate(&to_value(&6).unwrap()).is_valid(), true);
    assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), false);
}