MapValidator

Struct MapValidator 

Source
pub struct MapValidator {
Show 14 fields pub comment: String, pub max_len: u32, pub min_len: u32, pub keys: Option<Box<StrValidator>>, pub values: Option<Box<Validator>>, pub req: BTreeMap<String, Validator>, pub opt: BTreeMap<String, Validator>, pub in_list: Vec<BTreeMap<String, Value>>, pub nin_list: Vec<BTreeMap<String, Value>>, pub same_len: Vec<String>, pub query: bool, pub size: bool, pub map_ok: bool, pub same_len_ok: bool,
}
Expand description

Validator for maps.

This validator will only pass maps, whose keys are strings and values are any valid fog-pack value. Validation passes if:

  • If the in list is not empty, the map must be among the maps in the list.
  • The map must not be among the maps in the nin list.
  • The number of key-value pairs in the map is less than or equal to the value in max_len.
  • The number of key-value pairs in the map is greater than or equal to the value in min_len.
  • There must be a matching key-value in the map for each key-validator pair in req .
  • For each key-value pair in the map:
    1. If the key is in req, the corresponding validator is used to validate the value.
    2. If the key is not in req but is in opt, the corresponding validator is used to validate the value.
    3. if the key is not in req or opt, the validator for values is used to validate the value, and the validator for keys (if present) is used to validate the key.
      1. If no validator is present for keys, the key passes.
      2. If there is no validator for values, validation does not pass.
  • If same_len is not empty, the keys it lists must either all not exist, or if any of them exist, they must all exist and their values must all be arrays with the same lengths.

Note how each key-value pair must be validated, so an unlimited collection of key-value pairs isn’t allowed unless there is a validator present in values.

§Defaults

Fields that aren’t specified for the validator use their defaults instead. The defaults for each field are:

  • comment: “”
  • max_len: u32::MAX
  • min_len: u32::MIN
  • keys: None
  • values: None
  • req: empty
  • opt: empty
  • same_len: empty
  • in_list: empty
  • nin_list: empty
  • query: false
  • size: false
  • map_ok: false
  • same_len_ok: false

§Query Checking

Queries for maps are only allowed to use non-default values for each field if the corresponding query permission is set in the schema’s validator:

  • query: in and nin lists
  • size: max_len and min_len
  • map_ok: req, opt, keys, and values
  • same_len_ok: same_len

In addition, sub-validators in the query are matched against the schema’s sub-validators:

  • The values validator is checked against the schema’s values validator. If no schema validator is present, the query is invalid.
  • The keys string validator is checked against the schema’s keys string validator. If no schema validator is present, the query is invalid.
  • The req validators are checked against the schema’s req/opt/values validators, choosing whichever validator is found first. If no validator is found, the check fails.
  • The opt validators are checked against the schema’s req/opt/values validators, choosing whichever validator is found first. If no validator is found, the check fails.

Fields§

§comment: String

An optional comment explaining the validator.

§max_len: u32

The maximum allowed number of key-value pairs in the map.

§min_len: u32

The minimum allowed number of key-value pairs in the map.

§keys: Option<Box<StrValidator>>

The optional sub-validator for unknown keys in the map.

§values: Option<Box<Validator>>

An optional validator that each value in the map must pass, unless it is instead checked by a validator in req or opt. Unchecked values cause the map to fail validation.

§req: BTreeMap<String, Validator>

A map whose keys must all be present in a passing map, and whose validators are used to check the value held by a matching key in the map.

§opt: BTreeMap<String, Validator>

A map whose keys may be present in a map, and whose validators are used to check the value held by a matching key in the map, unless it is first checked by a validator in req.

§in_list: Vec<BTreeMap<String, Value>>

A vector of specific allowed values, stored under the in field. If empty, this vector is not checked against.

§nin_list: Vec<BTreeMap<String, Value>>

A vector of specific unallowed values, stored under the nin field.

§same_len: Vec<String>

A vector of which keys must either not exist, or must all exist and contain arrays of the same lengths.

§query: bool

If true, queries against matching spots may have values in the in or nin lists.

§size: bool

If true, queries against matching spots may use max_len and min_len.

§map_ok: bool

If true, queries against matching spots may use req, opt, keys, and values.

§same_len_ok: bool

If true, queries against matching spots may use same_len.

Implementations§

Source§

impl MapValidator

Source

pub fn new() -> Self

Make a new validator with the default configuration.

Source

pub fn comment(self, comment: impl Into<String>) -> Self

Set a comment for the validator.

Source

pub fn values(self, values: Validator) -> Self

Set the values validator.

Source

pub fn req_add(self, key: impl Into<String>, req: Validator) -> Self

Add a new validator to the req map.

Source

pub fn opt_add(self, key: impl Into<String>, opt: Validator) -> Self

Add a new validator to the opt map.

Source

pub fn keys(self, keys: StrValidator) -> Self

Set the Key Validator.

Source

pub fn max_len(self, max_len: u32) -> Self

Set the maximum number of allowed bytes.

Source

pub fn min_len(self, min_len: u32) -> Self

Set the minimum number of allowed bytes.

Source

pub fn in_add(self, add: impl Into<BTreeMap<String, Value>>) -> Self

Add a value to the in list.

Source

pub fn nin_add(self, add: impl Into<BTreeMap<String, Value>>) -> Self

Add a value to the nin list.

Source

pub fn same_len_add(self, add: impl Into<String>) -> Self

Add a key to the same_len list.

Source

pub fn query(self, query: bool) -> Self

Set whether or not queries can use the in and nin lists.

Source

pub fn size(self, size: bool) -> Self

Set whether or not queries can use the max_len and min_len values.

Source

pub fn map_ok(self, map_ok: bool) -> Self

Set whether or not queries can use the req, opt, ban, and values values.

Source

pub fn same_len_ok(self, same_len_ok: bool) -> Self

Set whether or not queries can use the same_len value.

Source

pub fn build(self) -> Validator

Build this into a Validator enum.

Trait Implementations§

Source§

impl Clone for MapValidator

Source§

fn clone(&self) -> MapValidator

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 MapValidator

Source§

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

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

impl Default for MapValidator

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for MapValidator

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 MapValidator

Source§

fn eq(&self, other: &MapValidator) -> 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 MapValidator

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 MapValidator

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>,