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

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

Make a new validator with the default configuration.

Add a new enum to the set.

Build this into a Validator enum.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

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 !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.