Skip to main content

Module validate

Module validate 

Source
Expand description

Collect all problems of a request instead of failing on the first one

use ullun::validate::Validator;

fn check(title: &str, priority: u8) -> ullun::error::Result<()> {
    let mut v = Validator::new();
    v.len_between("title", title, 1, 120);
    v.check(priority <= 3, "priority", "must be 0-3");
    v.finish()
}

assert!(check("Write docs", 2).is_ok());
let err = check("", 9).unwrap_err();
assert_eq!(err.status_code(), 422);
assert_eq!(err.fields().len(), 2);

The client receives every problem at once: {"error":"Validation failed","status":422,"fields":{"title":["..."],"priority":["..."]}}.

Structsยง

Validator
Accumulates FieldErrors; call finish at the end