EnumValidator

Struct EnumValidator 

Source
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§

Source§

impl EnumValidator

Source

pub fn new() -> Self

Make a new validator with the default configuration.

Source

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

Add a new enum to the set.

Source

pub fn build(self) -> Validator

Build this into a Validator enum.

Source

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

Iterate over all the enum variants.

Source

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

Iterate over all the validators in this enum.

Trait Implementations§

Source§

impl Clone for EnumValidator

Source§

fn clone(&self) -> EnumValidator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EnumValidator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EnumValidator

Source§

fn default() -> EnumValidator

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for EnumValidator

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for EnumValidator

Source§

fn eq(&self, other: &EnumValidator) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for EnumValidator

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for EnumValidator

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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