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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use schemars::schema::InstanceType;
use serde::Serialize;
use thiserror::Error;
/// An "atomic" change made to the JSON schema in question, going from LHS to RHS.
///
/// Just a wrapper container for `ChangeKind`
#[derive(Debug, PartialEq, Serialize)]
pub struct Change {
/// JSON path for the given change. `""` for "root schema". `".foo"` for property foo.
pub path: String,
/// Data specific to the kind of change.
pub change: ChangeKind,
}
/// The kind of change + data relevant to the change.
#[derive(Debug, PartialEq, Serialize)]
pub enum ChangeKind {
/// A type has been added and is now additionally allowed.
TypeAdd {
/// The type in question.
added: JsonSchemaType,
},
/// A type has been removed and is no longer allowed.
TypeRemove {
/// The type in question.
removed: JsonSchemaType,
},
/// A const value has been added as an allowed value.
ConstAdd {
/// The value of the added const.
added: serde_json::Value,
},
/// A const has been removed as an allowed value.
ConstRemove {
/// The value of the removed const.
removed: serde_json::Value,
},
/// A property has been added and (depending on additionalProperties) is now additionally
/// allowed.
PropertyAdd {
/// The value of additionalProperties within the current JSON object.
lhs_additional_properties: bool,
/// The name of the added property.
added: String,
},
/// A property has been removed and (depending on additionalProperties) might now no longer be
/// allowed.
PropertyRemove {
/// The value of additionalProperties within the current JSON object.
lhs_additional_properties: bool,
/// The name of the added property.
removed: String,
},
/// A minimum/maximum constraint has been added.
RangeAdd {
/// The value of the added constraint.
added: Range,
},
/// A minimum/maximum constraint has been removed.
RangeRemove {
/// The value of the removed constraint.
removed: Range,
},
/// A minimum/maximum constraint has been updated.
RangeChange {
/// The old constraint value.
old_value: Range,
/// The new constraint value.
new_value: Range,
},
/// An array-type item has been changed from tuple validation to array validation.
///
/// See https://json-schema.org/understanding-json-schema/reference/array.html
///
/// Changes will still be emitted for inner items.
TupleToArray {
/// The length of the (old) tuple
old_length: usize,
},
/// An array-type item has been changed from array validation to tuple validation.
///
/// See https://json-schema.org/understanding-json-schema/reference/array.html
///
/// Changes will still be emitted for inner items.
ArrayToTuple {
/// The length of the (new) tuple
new_length: usize,
},
/// An array-type item with tuple validation has changed its length ("items" array got longer
/// or shorter.
///
/// See https://json-schema.org/understanding-json-schema/reference/array.html
///
/// Changes will still be emitted for inner items.
TupleChange {
/// The new length of the tuple
new_length: usize,
},
/// A previously required property has been removed
RequiredRemove {
/// The property that is no longer required
property: String,
},
/// A previously optional property has been made required
RequiredAdd {
/// The property that is now required
property: String,
},
/// A format constraint has been added.
FormatAdd {
/// The format that was added.
added: String,
},
/// A format constraint has been removed.
FormatRemove {
/// The format that was removed.
removed: String,
},
/// A format constraint has been changed.
FormatChange {
/// The old format value.
old_format: String,
/// The new format value.
new_format: String,
},
/// An enum value has been added to the allowed values.
EnumAdd {
/// The value that was added to the enum.
added: serde_json::Value,
/// Whether the enum constraint was added (lhs had no enum).
/// If true, this is breaking as it adds a new constraint.
lhs_has_no_enum: bool,
},
/// An enum value has been removed from the allowed values.
EnumRemove {
/// The value that was removed from the enum.
removed: serde_json::Value,
/// Whether the entire enum constraint was removed (rhs has no enum).
/// If true, this is non-breaking as it relaxes the constraint.
rhs_has_no_enum: bool,
},
/// A pattern constraint has been added.
PatternAdd {
/// The pattern that was added.
added: String,
},
/// A pattern constraint has been removed.
PatternRemove {
/// The pattern that was removed.
removed: String,
},
/// A pattern constraint has been changed.
PatternChange {
/// The old pattern value.
old_pattern: String,
/// The new pattern value.
new_pattern: String,
},
/// A minLength constraint has been added.
MinLengthAdd {
/// The minLength value that was added.
added: u32,
},
/// A minLength constraint has been removed.
MinLengthRemove {
/// The minLength value that was removed.
removed: u32,
},
/// A minLength constraint has been changed.
MinLengthChange {
/// The old minLength value.
old_value: u32,
/// The new minLength value.
new_value: u32,
},
/// A maxLength constraint has been added.
MaxLengthAdd {
/// The maxLength value that was added.
added: u32,
},
/// A maxLength constraint has been removed.
MaxLengthRemove {
/// The maxLength value that was removed.
removed: u32,
},
/// A maxLength constraint has been changed.
MaxLengthChange {
/// The old maxLength value.
old_value: u32,
/// The new maxLength value.
new_value: u32,
},
}
impl ChangeKind {
/// Whether the change is breaking.
///
/// What is considered breaking is WIP. Changes are intentionally exposed as-is in public API
/// so that the user can develop their own logic as to what they consider breaking.
///
/// Currently the rule of thumb is, a change is breaking if it would cause messages that used
/// to validate fine under RHS to no longer validate under LHS.
pub fn is_breaking(&self) -> bool {
match self {
Self::TypeAdd { .. } => false,
Self::TypeRemove { .. } => true,
Self::ConstAdd { .. } => true,
Self::ConstRemove { .. } => false,
Self::PropertyAdd {
lhs_additional_properties,
..
} => *lhs_additional_properties,
Self::PropertyRemove {
lhs_additional_properties,
..
} => !*lhs_additional_properties,
Self::RangeAdd { .. } => true,
Self::RangeRemove { .. } => false,
Self::RangeChange {
old_value,
new_value,
} => match (old_value, new_value) {
(Range::ExclusiveMinimum(exc), Range::Minimum(min)) if exc >= min => false,
(Range::ExclusiveMaximum(exc), Range::Maximum(max)) if exc <= max => false,
(Range::Minimum(l), Range::Minimum(r)) if l >= r => false,
(Range::ExclusiveMinimum(l), Range::ExclusiveMinimum(r)) if l >= r => false,
(Range::Maximum(l), Range::Maximum(r)) if l <= r => false,
(Range::ExclusiveMaximum(l), Range::ExclusiveMaximum(r)) if l <= r => false,
_ => true,
},
Self::TupleToArray { .. } => false,
Self::ArrayToTuple { .. } => true,
Self::TupleChange { .. } => true,
Self::RequiredRemove { .. } => false,
Self::RequiredAdd { .. } => true,
Self::FormatAdd { .. } => true,
Self::FormatRemove { .. } => false,
Self::FormatChange { .. } => true,
// EnumAdd is breaking only if it adds a new enum constraint (lhs had no enum).
// Adding values to an existing enum is non-breaking (accepts more data).
Self::EnumAdd {
lhs_has_no_enum, ..
} => *lhs_has_no_enum,
// EnumRemove is breaking if removing values from a surviving enum constraint.
// Removing the entire enum constraint is non-breaking (accepts more data).
Self::EnumRemove {
rhs_has_no_enum, ..
} => !rhs_has_no_enum,
// Pattern changes are conservatively treated as breaking.
// Determining if one regex is a subset of another requires complex analysis.
Self::PatternAdd { .. } => true,
Self::PatternRemove { .. } => false,
Self::PatternChange { .. } => true,
// MinLength: increasing restricts (breaking), decreasing relaxes (non-breaking)
Self::MinLengthAdd { .. } => true,
Self::MinLengthRemove { .. } => false,
Self::MinLengthChange {
old_value,
new_value,
} => new_value > old_value,
// MaxLength: decreasing restricts (breaking), increasing relaxes (non-breaking)
Self::MaxLengthAdd { .. } => true,
Self::MaxLengthRemove { .. } => false,
Self::MaxLengthChange {
old_value,
new_value,
} => new_value < old_value,
}
}
}
/// The errors that can happen in this crate.
#[derive(Error, Debug)]
pub enum Error {
/// Failed to parse the JSON schema.
///
/// Any deserialization errors from serde that happen while converting the value into our AST
/// end up here.
#[error("failed to parse schema")]
Serde(#[from] serde_json::Error),
}
/// All primitive types defined in JSON schema.
#[derive(Serialize, Clone, Ord, Eq, PartialEq, PartialOrd, Debug)]
#[allow(missing_docs)]
pub enum JsonSchemaType {
#[serde(rename = "string")]
String,
#[serde(rename = "number")]
Number,
#[serde(rename = "integer")]
Integer,
#[serde(rename = "object")]
Object,
#[serde(rename = "array")]
Array,
#[serde(rename = "boolean")]
Boolean,
#[serde(rename = "null")]
Null,
}
impl From<JsonSchemaType> for InstanceType {
fn from(t: JsonSchemaType) -> Self {
match t {
JsonSchemaType::String => InstanceType::String,
JsonSchemaType::Number => InstanceType::Number,
JsonSchemaType::Integer => InstanceType::Integer,
JsonSchemaType::Object => InstanceType::Object,
JsonSchemaType::Array => InstanceType::Array,
JsonSchemaType::Boolean => InstanceType::Boolean,
JsonSchemaType::Null => InstanceType::Null,
}
}
}
impl From<InstanceType> for JsonSchemaType {
fn from(t: InstanceType) -> Self {
match t {
InstanceType::String => JsonSchemaType::String,
InstanceType::Number => JsonSchemaType::Number,
InstanceType::Integer => JsonSchemaType::Integer,
InstanceType::Object => JsonSchemaType::Object,
InstanceType::Array => JsonSchemaType::Array,
InstanceType::Boolean => JsonSchemaType::Boolean,
InstanceType::Null => JsonSchemaType::Null,
}
}
}
/// Range constraints in JSON schema.
#[derive(Serialize, Clone, PartialEq, PartialOrd, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(missing_docs)]
pub enum Range {
Minimum(f64),
Maximum(f64),
ExclusiveMinimum(f64),
ExclusiveMaximum(f64),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_range_change_breaking() {
assert!(!ChangeKind::RangeChange {
old_value: Range::Minimum(1.0),
new_value: Range::Minimum(1.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Minimum(1.0),
new_value: Range::Minimum(2.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::Minimum(2.0),
new_value: Range::Minimum(1.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Minimum(1.0),
new_value: Range::ExclusiveMinimum(1.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Minimum(1.0),
new_value: Range::ExclusiveMinimum(2.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Minimum(2.0),
new_value: Range::ExclusiveMinimum(1.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::ExclusiveMinimum(1.0),
new_value: Range::ExclusiveMinimum(1.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::ExclusiveMinimum(1.0),
new_value: Range::ExclusiveMinimum(2.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::ExclusiveMinimum(2.0),
new_value: Range::ExclusiveMinimum(1.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::Maximum(1.0),
new_value: Range::Maximum(1.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::Maximum(1.0),
new_value: Range::Maximum(2.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Maximum(2.0),
new_value: Range::Maximum(1.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Maximum(1.0),
new_value: Range::ExclusiveMaximum(1.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Maximum(1.0),
new_value: Range::ExclusiveMaximum(2.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::Maximum(2.0),
new_value: Range::ExclusiveMaximum(1.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::ExclusiveMaximum(1.0),
new_value: Range::ExclusiveMaximum(1.0),
}
.is_breaking());
assert!(!ChangeKind::RangeChange {
old_value: Range::ExclusiveMaximum(1.0),
new_value: Range::ExclusiveMaximum(2.0),
}
.is_breaking());
assert!(ChangeKind::RangeChange {
old_value: Range::ExclusiveMaximum(2.0),
new_value: Range::ExclusiveMaximum(1.0),
}
.is_breaking());
}
}