innoave/valid

Calling context.into() does not convert to State type

Closed this issue · 0 comments

I'm following the docs and trying to call context.into to get the actual state object. But I just get unknown. Was I supposed to impl Into for my State type? Your docs don't seem to indicate that.

pub struct StreamConstraint;
pub struct StreamState;

impl StreamState {
    pub fn is_stream(val: Option<String>) -> bool {
        if let None = val {
            return false;
        }

        let stream = JsValue::from(val.clone());
        if stream.is_null() || !stream.is_object() {
            return false;
        }
        let emitter_result = serde_json::from_str::<EventEmitter>(val.unwrap().as_str());
        match emitter_result {
            Ok(_) => true,
            _ => false
        }
    }
}

impl<'a> Validate<StreamConstraint, State<&'a StreamState>> for String {
    fn validate(self, context: impl Into<State<&'a StreamState>>, constraint: &StreamConstraint) -> Validation<StreamConstraint, Self> {        
        if context.into().is_stream(Some(&self)) {
            return Validation::success(self);
        }
        Validation::failure(vec![invalid_state("invalid-stream", vec![])])
    }
}