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
use itertools::Itertools;
use serde_json::Value;
use std::error;
use std::fmt;
use std::io::prelude::*;
use url;
use context::Context;
#[derive(Default, Debug)]
pub struct ValidationError {
msg: String,
instance_path: Option<Vec<Value>>,
schema_path: Option<Vec<Value>>,
}
fn simple_to_string(value: &Value) -> String {
match value {
Value::String(v) => v.as_str().to_string(),
_ => value.to_string(),
}
}
fn path_to_string(path: &[Value]) -> String {
if path.is_empty() {
".".to_string()
} else {
path.iter().map(|x| simple_to_string(x)).join("/")
}
}
impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let (Some(instance_path), Some(schema_path)) = (&self.instance_path, &self.schema_path) {
write!(
f,
"At {} with schema at {}: {}",
path_to_string(&instance_path),
path_to_string(&schema_path),
self.msg
)
} else if let Some(schema_path) = &self.schema_path {
write!(
f,
"At schema {}: {}",
path_to_string(&schema_path),
self.msg
)
} else {
write!(f, "{}", self.msg)
}
}
}
impl error::Error for ValidationError {
fn cause(&self) -> Option<&error::Error> {
None
}
}
impl From<url::ParseError> for ValidationError {
fn from(err: url::ParseError) -> ValidationError {
ValidationError::new(&format!("Invalid URL: {:?}", err))
}
}
impl ValidationError {
pub fn new(msg: &str) -> ValidationError {
ValidationError {
msg: String::from(msg),
..Default::default()
}
}
pub fn new_with_schema_context(msg: &str, schema_ctx: &Context) -> ValidationError {
ValidationError {
msg: String::from(msg),
instance_path: None,
schema_path: Some(schema_ctx.flatten()),
}
}
pub fn new_with_context(
msg: &str,
instance_ctx: &Context,
schema_ctx: &Context,
) -> ValidationError {
ValidationError {
msg: String::from(msg),
instance_path: Some(instance_ctx.flatten()),
schema_path: Some(schema_ctx.flatten()),
}
}
}
pub trait ErrorRecorder {
fn record_error(&mut self, error: ValidationError) -> Option<()>;
fn has_errors(&self) -> bool;
}
#[derive(Default)]
pub struct ValidationErrors {
errors: Vec<ValidationError>,
}
impl ErrorRecorder for ValidationErrors {
fn record_error(&mut self, error: ValidationError) -> Option<()> {
self.errors.push(error);
Some(())
}
fn has_errors(&self) -> bool {
!self.errors.is_empty()
}
}
impl ValidationErrors {
pub fn new() -> ValidationErrors {
ValidationErrors {
..Default::default()
}
}
pub fn get_errors(&self) -> &[ValidationError] {
&self.errors
}
}
#[derive(Default)]
pub struct FastFailErrorRecorder {
error: Option<ValidationError>,
}
impl ErrorRecorder for FastFailErrorRecorder {
fn record_error(&mut self, err: ValidationError) -> Option<()> {
self.error = Some(err);
None
}
fn has_errors(&self) -> bool {
self.error.is_some()
}
}
impl FastFailErrorRecorder {
pub fn new() -> FastFailErrorRecorder {
FastFailErrorRecorder {
..Default::default()
}
}
}
pub struct ErrorRecorderStream<'a> {
stream: &'a mut Write,
has_error: bool,
}
impl<'a> ErrorRecorder for ErrorRecorderStream<'a> {
fn record_error(&mut self, err: ValidationError) -> Option<()> {
self.has_error = true;
if writeln!(self.stream, "{}", err.to_string()).is_err() {
None
} else {
Some(())
}
}
fn has_errors(&self) -> bool {
self.has_error
}
}
impl<'a> ErrorRecorderStream<'a> {
pub fn new(stream: &'a mut Write) -> ErrorRecorderStream<'a> {
ErrorRecorderStream {
stream,
has_error: false,
}
}
}