Skip to content

Commit

Permalink
Validate maps as records (#178)
Browse files Browse the repository at this point in the history
This is necessary for resolving maps as records in unions
  • Loading branch information
Emily Crandall Fleischman committed Feb 7, 2021
1 parent f0c2ac2 commit 8e5a8ef
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/types.rs
Expand Up @@ -358,6 +358,15 @@ impl Value {
},
)
}
(&Value::Map(ref items), &Schema::Record { ref fields, .. }) => {
fields.iter().all(|field| {
if let Some(item) = items.get(&field.name) {
item.validate(&field.schema)
} else {
false
}
})
}
_ => false,
}
}
Expand Down Expand Up @@ -920,6 +929,34 @@ mod tests {
("c".to_string(), Value::Null),
])
.validate(&schema));

assert!(Value::Map(
vec![
("a".to_string(), Value::Long(42i64)),
("b".to_string(), Value::String("foo".to_string())),
]
.into_iter()
.collect()
)
.validate(&schema));

let union_schema = Schema::Union(UnionSchema::new(vec![Schema::Null, schema]).unwrap());

assert!(Value::Union(Box::new(Value::Record(vec![
("a".to_string(), Value::Long(42i64)),
("b".to_string(), Value::String("foo".to_string())),
])))
.validate(&union_schema));

assert!(Value::Union(Box::new(Value::Map(
vec![
("a".to_string(), Value::Long(42i64)),
("b".to_string(), Value::String("foo".to_string())),
]
.into_iter()
.collect()
)))
.validate(&union_schema));
}

#[test]
Expand Down

0 comments on commit 8e5a8ef

Please sign in to comment.