pub type ValidatorFunc = fn(_: &Value) -> Result<(), Error>;
Expand description

Type alias for a custom function used to validate the input to a resolver

Examples


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