Struct fog_pack::validator::EnumValidator [−][src]
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
Add a new enum to the set.
Trait Implementations
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
This method tests for self and other values to be equal, and is used
by ==. Read more
This method tests for !=.
Auto Trait Implementations
impl RefUnwindSafe for EnumValidator
impl Send for EnumValidator
impl Sync for EnumValidator
impl Unpin for EnumValidator
impl UnwindSafe for EnumValidator
Blanket Implementations
Mutably borrows from an owned value. Read more
type Output = T
type Output = T
Should always be Self