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
use crate::{span::Keys, Verify};
use schemars_crate::{
    schema::{RootSchema, Schema, SchemaObject},
    Map,
};

use super::errors::{Error, ErrorValue, Errors, InvalidSchema};

impl Verify for RootSchema {
    type Error = Errors<Keys>;

    fn verify(&self) -> Result<(), Self::Error> {
        let mut errors = Errors::new();

        for (k, s) in &self.definitions {
            if let Err(err) = verify_schema(s, &self.definitions, Keys::new() + "definitions" + k) {
                errors += err;
            }
        }

        if let Err(err) = verify_schema_object(&self.schema, &self.definitions, Keys::new()) {
            errors += err;
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

fn verify_schema(
    schema: &Schema,
    definitions: &Map<String, Schema>,
    span: Keys,
) -> Result<(), Errors<Keys>> {
    match schema {
        Schema::Bool(_) => Ok(()),
        Schema::Object(o) => verify_schema_object(o, definitions, span),
    }
}

fn verify_schema_object(
    schema: &SchemaObject,
    definitions: &Map<String, Schema>,
    span: Keys,
) -> Result<(), Errors<Keys>> {
    let mut errors = Errors::new();

    if let Some(r) = &schema.reference {
        match local_definition(r) {
            Some(s) => {
                if definitions.get(s).is_none() {
                    errors.0.push(Error::new(
                        None,
                        Some(span.clone()),
                        ErrorValue::InvalidSchema(InvalidSchema::MissingDefinition(s.to_string())),
                    ));
                    return Err(errors);
                }
            }
            None => {
                errors.0.push(Error::new(
                    None,
                    Some(span.clone()),
                    ErrorValue::InvalidSchema(InvalidSchema::ExternalReference(r.clone())),
                ));
                return Err(errors);
            }
        }
    }

    if let Some(subs) = &schema.subschemas {
        if let Some(all_ofs) = &subs.all_of {
            for (i, s) in all_ofs.iter().enumerate() {
                if let Err(err) = verify_schema(s, definitions, span.clone() + "allOf" + i) {
                    errors += err;
                }
            }
        }

        if let Some(one_ofs) = &subs.one_of {
            for (i, s) in one_ofs.iter().enumerate() {
                if let Err(err) = verify_schema(s, definitions, span.clone() + "oneOf" + i) {
                    errors += err;
                }
            }
        }

        if let Some(any_ofs) = &subs.any_of {
            for (i, s) in any_ofs.iter().enumerate() {
                if let Err(err) = verify_schema(s, definitions, span.clone() + "anyOf" + i) {
                    errors += err;
                }
            }
        }

        if let Some(s) = &subs.not {
            if let Err(err) = verify_schema(s, definitions, span.clone() + "not") {
                errors += err;
            }
        }

        if let Some(s) = &subs.if_schema {
            if let Err(err) = verify_schema(s, definitions, span.clone() + "if") {
                errors += err;
            }
        }

        if let Some(s) = &subs.then_schema {
            if let Err(err) = verify_schema(s, definitions, span.clone() + "then") {
                errors += err;
            }
        }

        if let Some(s) = &subs.else_schema {
            if let Err(err) = verify_schema(s, definitions, span.clone() + "else") {
                errors += err;
            }
        }
    }

    if let Some(o) = &schema.object {
        for (k, _) in &o.pattern_properties {
            if let Err(error) = regex::Regex::new(k) {
                errors.0.push(Error::new(
                    None,
                    Some(span.clone() + "patternProperties" + k),
                    ErrorValue::InvalidSchema(InvalidSchema::InvalidPattern {
                        pattern: k.clone(),
                        error,
                    }),
                ));
            }
        }

        for (k, s) in &o.properties {
            if let Err(err) = verify_schema(s, definitions, span.clone() + "properties" + k) {
                errors += err;
            }
        }

        if let Some(s) = &o.additional_properties {
            if let Err(err) = verify_schema(s, definitions, span.clone() + "additionalProperties") {
                errors += err;
            }
        }
    }

    if let Some(st) = &schema.string {
        if let Some(p) = &st.pattern {
            if let Err(error) = regex::Regex::new(&p) {
                errors.0.push(Error::new(
                    None,
                    Some(span.clone() + "pattern"),
                    ErrorValue::InvalidSchema(InvalidSchema::InvalidPattern {
                        pattern: p.clone(),
                        error,
                    }),
                ));
            }
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

fn local_definition(path: &str) -> Option<&str> {
    if !path.starts_with("#/definitions/") {
        return None;
    }

    Some(path.trim_start_matches("#/definitions/"))
}