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
//! Error definitions used during Schema-related validation.

use crate::span::Span;
use schemars_crate::schema::{InstanceType, Metadata, SingleOrVec};
use std::ops::AddAssign;

/// A validation error.
///
/// It contains an optional span of the invalid value and optional information about the schema that caused the error.
#[derive(Debug, Clone, PartialEq)]
pub struct Error<S: Span> {
    /// Information about the schema that caused the validation
    /// error.
    pub meta: Option<Box<Metadata>>,

    /// The span of the invalid value.
    pub span: Option<S>,

    /// The actual error details.
    pub value: ErrorValue<S>,
}

impl<S: Span> Error<S> {
    pub(crate) fn new(meta: Option<Box<Metadata>>, span: Option<S>, value: ErrorValue<S>) -> Self {
        Self { meta, span, value }
    }
}

impl<S: Span> core::fmt::Display for Error<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut start_paren = false;

        if f.alternate() {
            if let Some(span) = &self.span {
                write!(f, "({:?}", span)?;
                start_paren = true;
            }

            if let Some(meta) = &self.meta {
                if let Some(title) = &meta.title {
                    if start_paren {
                        write!(f, r#", schema: "{}""#, title)?;
                    } else {
                        write!(f, r#"(schema: "{}""#, title)?;
                    }
                    start_paren = true;
                }
            }
        }

        if start_paren {
            write!(f, ") ")?;
        }

        write!(f, "{}", self.value)
    }
}

/// All the validation errors that can occur.
#[derive(Debug, Clone, PartialEq)]
// TODO maybe prefix or group them by type?
pub enum ErrorValue<S: Span> {
    /// Indicates that the schema will never match any value.
    Never,

    /// Indicates that the schema denies unknown properties.
    UnknownProperty,

    /// Indicates that the schema itself is invalid.
    InvalidSchema(InvalidSchema),

    /// Indicates incompatible value that cannot be validated
    /// by a schema.
    UnsupportedValue(UnsupportedValue),

    /// Indicates invalid type.
    InvalidType {
        expected: SingleOrVec<InstanceType>,
        actual: InstanceType,
    },

    /// Indicates invalid enum value.
    InvalidEnumValue { expected: Vec<serde_json::Value> },

    /// Indicates that the number is not multiple of the given value.
    NotMultipleOf { multiple_of: f64 },

    /// Indicates that the number is less than the given minimum value.
    LessThanExpected { min: f64, exclusive: bool },

    /// Indicates that the number is more than the given maximum value.
    MoreThanExpected { max: f64, exclusive: bool },

    /// Indicates that the string doesn't match the given pattern.
    NoPatternMatch { pattern: String },

    /// Indicates that the string is too long.
    TooLong { max_length: u32 },

    /// Indicates that the string is too short.
    TooShort { min_length: u32 },

    /// Indicates that none of the subschemas matched.
    ///
    /// Exclusive indicates that exactly one of them must have matched.
    NoneValid {
        exclusive: bool,
        schemas: Vec<Option<Box<Metadata>>>,
        errors: Vec<Errors<S>>,
    },

    /// Indicates that more than one of the subschemas matched.
    MoreThanOneValid { schemas: Vec<Option<Box<Metadata>>>, matched: Vec<Option<Box<Metadata>>> },

    /// Indicates that a not schema matched.
    ValidNot { matched: Option<Box<Metadata>> },

    /// Indicates that the items in the array are not unique.
    NotUnique {
        first: Option<S>,
        duplicate: Option<S>,
    },

    /// Indicates that the array doesn't contain the value of a given schema.
    MustContain { schema: Option<Box<Metadata>> },

    /// Indicates that the array doesn't have enough items.
    NotEnoughItems { min: usize },

    /// Indicates that the array has too many items.
    TooManyItems { max: usize },

    /// Indicates that the object has too few properties.
    NotEnoughProperties { min: usize },

    /// Indicates that the object has too many properties.
    TooManyProperties { max: usize },

    /// Indicates that a required property is missing.
    RequiredProperty { name: String },

    /// Any error that does not originate from the validator.
    Custom(String),
}

/// Error that occurs when a value cannot be validated
/// by a schema.
#[derive(Debug, Clone, PartialEq)]
pub enum UnsupportedValue {
    /// Indicates that key of a map is not a string.
    KeyNotString,
}

impl core::fmt::Display for UnsupportedValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnsupportedValue::KeyNotString => write!(f, "map key must be a string"),
        }
    }
}

/// All errors related to the schema being invalid.
///
/// A schema must be valid in order to validate anything with it.
/// This error occurs if that is not the case.
///
/// It is also returned by calling [verify](crate::Verify::verify) on a schema.
#[derive(Debug, Clone, PartialEq)]
pub enum InvalidSchema {
    /// Indicates a missing local definition.
    MissingDefinition(String),

    /// Indicates an invalid regex pattern in the schema.
    InvalidPattern {
        pattern: String,
        error: regex::Error,
    },

    /// Indicates an unresolved external reference in the schema.
    ExternalReference(String),
}

impl core::fmt::Display for InvalidSchema {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            InvalidSchema::MissingDefinition(s) => write!(f, r#"missing local definition "{}""#, s),
            InvalidSchema::InvalidPattern { pattern, error } => {
                write!(f, r#"invalid regex pattern "{}": {}"#, pattern, error)
            }
            InvalidSchema::ExternalReference(r) => write!(
                f,
                r#"the schema contains unresolved external reference: "{}""#,
                r
            ),
        }
    }
}

impl<S: Span> core::fmt::Display for ErrorValue<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorValue::Never => write!(f, "no values allowed"),
            ErrorValue::UnknownProperty => write!(f, "unknown property"),
            ErrorValue::InvalidSchema(err) => write!(f, "invalid schema: {}", err),
            ErrorValue::UnsupportedValue(err) => write!(f, "unsupported value: {}", err),
            ErrorValue::InvalidType { expected, actual } => write!(
                f,
                r#"invalid type, expected {}, not "{:?}""#,
                match expected {
                    SingleOrVec::Single(s) => {
                        format!(r#""{:?}""#, s)
                    }
                    SingleOrVec::Vec(v) => {
                        let mut s = "one of {".into();

                        for (i, t) in v.iter().enumerate() {
                            s += format!(r#""{:?}""#, t).as_str();
                            if i != v.len() - 1 {
                                s += ", "
                            }
                        }
                        s += "}";

                        s
                    }
                },
                actual
            ),
            ErrorValue::InvalidEnumValue { expected } => {
                let enum_vals: Vec<String> = expected.iter().map(|v| v.to_string()).collect();
                write!(
                    f,
                    "invalid enum value, expected to be one of {{{}}}",
                    enum_vals.join(", ")
                )
            }
            ErrorValue::NotMultipleOf { multiple_of } => {
                write!(f, "the value is expected to be multiple of {}", multiple_of)
            }
            ErrorValue::LessThanExpected { min, exclusive } => {
                if *exclusive {
                    write!(f, "the value is expected to be more than {}", min)
                } else {
                    write!(f, "the value is expected to be at least {}", min)
                }
            }
            ErrorValue::MoreThanExpected { max, exclusive } => {
                if *exclusive {
                    write!(f, "the value is expected to be less than {}", max)
                } else {
                    write!(f, "the value is expected to be at most {}", max)
                }
            }
            ErrorValue::NoPatternMatch { pattern } => {
                write!(f, r#"the string must match the pattern "{}""#, pattern)
            }
            ErrorValue::TooLong { max_length } => write!(
                f,
                r#"the string must not be longer than {} characters"#,
                max_length
            ),
            ErrorValue::TooShort { min_length } => write!(
                f,
                r#"the string must must be at least {} characters long"#,
                min_length
            ),
            ErrorValue::NoneValid {
                exclusive: _,
                schemas: _,
                errors,
            } => {
                writeln!(f, r#"no subschema matched the value:"#)?;

                for (i, e) in errors.iter().enumerate() {
                    write!(f, "{}", e)?;

                    if i != errors.len() - 1 {
                        writeln!(f, "\n")?;
                    }
                }

                Ok(())
            }
            ErrorValue::MoreThanOneValid { schemas: _, matched } => writeln!(
                f,
                r#"expected exactly one schema to match, but {} schemas matched"#,
                matched.len()
            ),
            ErrorValue::ValidNot { matched } => {
                if let Some(meta) = matched {
                    if let Some(title) = &meta.title {
                        return writeln!(f, r#"the value must not be a "{}""#, title);
                    }
                }

                writeln!(f, r#"the value is disallowed by a "not" schema"#)
            }
            ErrorValue::NotUnique { first: _, duplicate: _ } => {
                writeln!(f, r#"all items in the array must be unique"#)
            }
            ErrorValue::MustContain { schema } => {
                if let Some(meta) = schema {
                    if let Some(title) = &meta.title {
                        return writeln!(
                            f,
                            r#"at least one of the items in the array must be "{}""#,
                            title
                        );
                    }
                }

                writeln!(
                    f,
                    r#"at least one of the items in the array must match the given schema"#
                )
            }
            ErrorValue::NotEnoughItems { min } => {
                write!(f, "the array must have at least {} items", min)
            }
            ErrorValue::TooManyItems { max } => {
                write!(f, "the array cannot have more than {} items", max)
            }
            ErrorValue::NotEnoughProperties { min } => {
                write!(f, "the object must have at least {} properties", min)
            }
            ErrorValue::TooManyProperties { max } => {
                write!(f, "the object cannot have more than {} properties", max)
            }
            ErrorValue::RequiredProperty { name } => {
                write!(f, r#"the required property "{}" is missing"#, name)
            }
            ErrorValue::Custom(err) => err.fmt(f),
        }
    }
}

#[cfg(feature = "smallvec")]
type SmallVecArray<S> = [Error<S>; 10];

#[cfg(feature = "smallvec")]
/// In a lot of cases there are only 1 or 2 errors
/// so using smallvec probably helps a bit by removing unnecessary allocations.
pub(super) type ErrorsInner<S> = smallvec_crate::SmallVec<SmallVecArray<S>>;

#[cfg(not(feature = "smallvec"))]
pub(super) type ErrorsInner<S> = Vec<Error<S>>;

/// A collection of [Errors](Error), this type is returned from validation.
#[derive(Debug, Clone, PartialEq)]
#[repr(transparent)]
pub struct Errors<S: Span>(pub(crate) ErrorsInner<S>);

impl<S: Span> Errors<S> {
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = &Error<S>> {
        self.0.iter()
    }

    pub(super) fn new() -> Self {
        Errors(ErrorsInner::new())
    }

    pub(super) fn one(error: Error<S>) -> Self {
        let mut v = ErrorsInner::new();
        v.push(error);
        Errors(v)
    }
}

impl<S: Span> IntoIterator for Errors<S> {
    type Item = <ErrorsInner<S> as IntoIterator>::Item;

    #[cfg(feature = "smallvec")]
    type IntoIter = smallvec_crate::IntoIter<SmallVecArray<S>>;

    #[cfg(not(feature = "smallvec"))]
    type IntoIter = std::vec::IntoIter<Error<S>>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<S: Span> core::fmt::Display for Errors<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for e in &self.0 {
            writeln!(f, "{}", e)?;
        }
        Ok(())
    }
}

impl<S: Span> std::error::Error for Errors<S> {}
impl<S: Span> crate::Error for Errors<S> {
    fn custom<T: core::fmt::Display>(error: T) -> Self {
        Self::one(Error::new(
            None,
            None,
            ErrorValue::Custom(error.to_string()),
        ))
    }
}

impl<S: Span> AddAssign for Errors<S> {
    fn add_assign(&mut self, rhs: Self) {
        self.0.extend(rhs.0.into_iter());
    }
}