pub struct EnumValidator(pub BTreeMap<String, Option<Validator>>);Expand description
“Enum” validator that selects a validator based on the value’s enum variant.
This validator expects a serialized Rust enum. A serialized enum consists of either a single
string (a unit variant) or a map with a single key-value pair, where the key is the name of the
enum variant and the value is the associated data. The associated data is validated against the
matching validator in the contained BTreeMap. If there is no match, validation fails.
For unit variants, there is no validator, and they pass as long as their name is a key in the
BTreeMap.
§Query Checking
The query validator must be an Any or an Enum validator, and the maps are directly checked against
each other. The query validator may use a subset of the enum list. For unit variants, both the
query validator and schema validator must have None instead of a validator. As an example,
see the following:
// Say we have a enum like this:
enum ExampleEnum {
Empty,
Integer(Integer),
String(String),
}
// Let's use this as our schema-side validator
let entry_validator = EnumValidator::new()
.insert("Empty", None)
.insert("Integer", Some(IntValidator::new().build()))
.insert("String", Some(StrValidator::new().build()))
.build();
// We'll build a full schema, so we can do query validation
let schema_doc = SchemaBuilder::new(Validator::Null)
.entry_add("item", entry_validator, None)
.build()
.unwrap();
let schema = Schema::from_doc(&schema_doc).unwrap();
// This query is accepted because all enum validators match. Note how
// String isn't present, because the query doesn't need to have all
// possible enums.
let query_validator = EnumValidator::new()
.insert("Empty", None)
.insert("Integer", Some(IntValidator::new().build()))
.build();
let query = NewQuery::new("item", query_validator);
assert!(schema.encode_query(query).is_ok());
// This query, however, has a validator for "Empty", so it doesn't work:
let query_validator = EnumValidator::new()
.insert("Empty", Some(Validator::Null))
.insert("Integer", Some(IntValidator::new().build()))
.build();
let query = NewQuery::new("item", query_validator);
assert!(schema.encode_query(query).is_err());
Tuple Fields§
§0: BTreeMap<String, Option<Validator>>Implementations§
Trait Implementations§
Source§impl Clone for EnumValidator
impl Clone for EnumValidator
Source§fn clone(&self) -> EnumValidator
fn clone(&self) -> EnumValidator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more