Skip to content

Commit

Permalink
Allow running visitors against pre-2020-12 schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed May 20, 2024
1 parent 3aa0e7f commit d32231c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion schemars/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub trait Visitor {
pub fn visit_schema<V: Visitor + ?Sized>(v: &mut V, schema: &mut Schema) {
if let Some(obj) = schema.as_object_mut() {
for (key, value) in obj {
// This is intentionally written to work with multiple JSON Schema versions, so that
// users can add their own visitors on the end of e.g. `SchemaSettings::draft07()` and
// they will still apply to all subschemas "as expected".
// This is why this match statement contains both `additionalProperties` (which was
// dropped in draft 2020-12) and `prefixItems` (which was added in draft 2020-12).
match key.as_str() {
"not"
| "if"
Expand All @@ -53,7 +58,7 @@ pub fn visit_schema<V: Visitor + ?Sized>(v: &mut V, schema: &mut Schema) {
| "contains"
| "additionalProperties"
| "propertyNames"
| "items" => {
| "additionalItems" => {
if let Ok(subschema) = value.try_into() {
v.visit_schema(subschema)
}
Expand All @@ -67,6 +72,18 @@ pub fn visit_schema<V: Visitor + ?Sized>(v: &mut V, schema: &mut Schema) {
}
}
}
// Support `items` array even though this is not allowed in draft 2020-12 (see above comment)
"items" => {
if let Some(array) = value.as_array_mut() {
for value in array {
if let Ok(subschema) = value.try_into() {
v.visit_schema(subschema)
}
}
} else if let Ok(subschema) = value.try_into() {
v.visit_schema(subschema)
}
}
"properties" | "patternProperties" => {
if let Some(obj) = value.as_object_mut() {
for value in obj.values_mut() {
Expand Down

0 comments on commit d32231c

Please sign in to comment.