1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//! This module provides types for custom input validation
use crate::engine::value::Value;
use crate::Error;
use std::collections::HashMap;
/// Type alias for a custom function used to validate the input to a resolver
///
/// # Examples
///
/// ```rust
/// # use warpgrapher::Error;
/// # use warpgrapher::engine::validators::{ValidatorFunc, Validators};
/// # use warpgrapher::engine::value::Value;
///
/// fn name_validator(value: &Value) -> Result<(), Error> {
/// let name = match value {
/// Value::Map(m) => match m.get("name") {
/// Some(n) => n,
/// None => return Err(Error::ValidationFailed {message: "Name missing.".to_string()}),
/// },
/// _ => return Err(Error::ValidationFailed {message: "Field map missing.".to_string()}),
/// };
///
/// match name {
/// Value::String(s) => if s == "KENOBI" {
/// return Err(Error::ValidationFailed {
/// message: "Cannot be named KENOBI.".to_string()
/// });
/// } else {
/// return Ok(())
/// },
/// _ => Err(Error::ValidationFailed {message: "Expected a string value.".to_string()}),
/// }
/// }
///
/// let f: Box<ValidatorFunc> = Box::new(name_validator);
/// ```
pub type ValidatorFunc = fn(&Value) -> Result<(), Error>;
/// Type alias for a custom function used to validate the input to a resolver
///
/// Examples
///
/// ```rust
/// # use warpgrapher::engine::validators::{ValidatorFunc, Validators};
/// # use warpgrapher::engine::value::Value;
/// # use warpgrapher::Error;
///
/// fn name_validator(value: &Value) -> Result<(), Error> {
/// let name = match value {
/// Value::Map(m) => match m.get("name") {
/// Some(n) => n,
/// None => return Err(Error::ValidationFailed {message: "Name missing.".to_string()}),
/// },
/// _ => return Err(Error::ValidationFailed {message: "Field map missing.".to_string()}),
/// };
///
/// match name {
/// Value::String(s) => if s == "KENOBI" {
/// return Err(Error::ValidationFailed {
/// message: "Cannot be named KENOBI.".to_string()
/// });
/// } else {
/// return Ok(());
/// },
/// _ => Err(Error::ValidationFailed {message: "Expected a string value.".to_string()}),
/// }
/// }
///
/// let mut validators = Validators::new();
/// validators.insert("name_validator".to_string(), Box::new(name_validator));
/// ```
pub type Validators = HashMap<String, Box<ValidatorFunc>>;