Struct fog_pack::validator::EnumValidator[][src]

pub struct EnumValidator(pub BTreeMap<String, Option<Validator>>);

“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());

Implementations

impl EnumValidator[src]

pub fn new() -> Self[src]

Make a new validator with the default configuration.

pub fn insert(
    self,
    variant: impl Into<String>,
    validator: Option<Validator>
) -> Self
[src]

Add a new enum to the set.

pub fn build(self) -> Validator[src]

Build this into a Validator enum.

pub fn iter(&self) -> Iter<'_, String, Option<Validator>>[src]

pub fn values(&self) -> Values<'_, String, Option<Validator>>[src]

Trait Implementations

impl Clone for EnumValidator[src]

impl Debug for EnumValidator[src]

impl Default for EnumValidator[src]

impl<'de> Deserialize<'de> for EnumValidator[src]

impl PartialEq<EnumValidator> for EnumValidator[src]

impl Serialize for EnumValidator[src]

impl StructuralPartialEq for EnumValidator[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,