From 8e5a8ef61b0623efc797ac9bbb2028678b279956 Mon Sep 17 00:00:00 2001 From: Emily Crandall Fleischman Date: Sun, 7 Feb 2021 10:57:55 -0500 Subject: [PATCH] Validate maps as records (#178) This is necessary for resolving maps as records in unions --- src/types.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/types.rs b/src/types.rs index b260366c6a0..4de8e01b64a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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, } } @@ -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]