Skip to main content
Version: 0.6.0

Lax Fields

A lax field is both an input and output field whose value may or may not be provided at creation (e.g. email, phone_number).

  • Declared with #[lax(default_or_resolver)] -- a static default, or a |ctx, opts| -> T resolver, used whenever the field is missing.
  • May have a validator and re-validator via #[validate]/#[re_validate].
  • May leverage #[ignore], #[ignore_init] and #[ignore_update] to skip processing under a condition.
  • May use #[readonly] to reject updates, if the default is static.
  • May have on_delete and on_success event handlers, and on_failure if it has a validator.

Example​

bio falls back to a static default when missing, and is validated when provided:

use ivo::ivo_schema;

#[ivo_schema(input(DataInput, derive(Debug, Clone, PartialEq)))]
mod lax_schema {
struct Fields {
#[lax("default_bio".to_string())]
#[validate(|v: String, _, _| {
if v.len() > 100 {
return Err(("bio too long".into(), None));
}
Ok(None)
})]
pub bio: String,
}
}

fn main() {
let (created, _ctx_options) = lax_schema::DataInputModel
.create(lax_schema::PartialDataInput { bio: None }, ())
.unwrap();

assert_eq!(created.bio, "default_bio");
println!("{:?}", created); // DataInput { bio: "default_bio" }
}

More examples​

Try it in the browser​

username has a static default and no validator - leave the input empty to see the default kick in.

Loading playground…