Skip to main content
Version: 0.7.2

Lax Fields

laxinput and output

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 (and trimmed) when provided:

main.rs
use ivo::ivo_schema;

#[ivo_schema(input(Profile, ivo_attrs(derive(Debug, Clone, PartialEq))))]
mod profile_schema {
struct Fields {
#[lax("default bio".to_string())]
#[validate(|bio: &String, _, _| {
if bio.len() > 100 {
return Err(("bio too long".into(), None));
}
Ok(Some(bio.trim().to_string())) // the stored value is trimmed
})]
pub bio: String,
}
}

fn main() {
use profile_schema::{PartialProfile, ProfileModel};

// missing: the default is used
let (profile, _ctx_options) = ProfileModel.create(PartialProfile { bio: None }, ()).unwrap();
println!("{:?}", profile); // Profile { bio: "default bio" }

// provided: validated, and replaces the default
let (profile, _ctx_options) = ProfileModel
.create(PartialProfile { bio: Some(" Hello! ".into()) }, ())
.unwrap();
println!("{:?}", profile); // Profile { bio: "Hello!" }

let (errors, _ctx_options) = ProfileModel
.create(PartialProfile { bio: Some("x".repeat(101)) }, ())
.unwrap_err();
println!("{:?}", errors.get("bio").unwrap().reason); // "bio too long"
}

Readonly​

#[readonly] (with a static default) locks the field once its value differs from its default:

main.rs
use ivo::ivo_schema;

#[ivo_schema(input(Account, ivo_attrs(derive(Debug, Clone, PartialEq))))]
mod account_schema {
struct Fields {
#[lax("PENDING".to_string())]
#[readonly]
pub code: String,
}
}

fn main() {
use account_schema::{AccountModel, PartialAccount};

let (account, _ctx_options) = AccountModel.create(PartialAccount { code: None }, ()).unwrap();
println!("{:?}", account); // Account { code: "PENDING" }

// at its default, the field can still change...
let (updated, _ctx_options) = AccountModel
.update(account, PartialAccount { code: Some("DEF".into()) }, ())
.unwrap();
println!("{:?}", updated); // PartialAccount { code: Some("DEF") }

// ...but once it differs from it, it's locked
let changed = account_schema::Account { code: "DEF".into() };
let (errors, _ctx_options) = AccountModel
.update(changed, PartialAccount { code: Some("GHI".into()) }, ())
.unwrap_err();
println!("{:?}", errors); // None
}

Conditional required​

#[required(|ctx, opts| ...)] makes the field required when the handler returns Some(reason):

main.rs
use ivo::ivo_schema;

#[ivo_schema(input(Contact, ivo_attrs(derive(Debug, Clone, PartialEq))))]
mod contact_schema {
struct Fields {
#[lax(None)]
pub email: Option<String>,

// required when there's no email
#[lax(None)]
#[required(|ctx, _| {
ctx.values()
.email
.clone()
.flatten()
.is_none()
.then(|| "Provide a phone number when email is missing".into())
})]
pub phone_number: Option<String>,
}
}

fn main() {
use contact_schema::{ContactModel, PartialContact};

let (errors, _ctx_options) = ContactModel
.create(PartialContact { email: None, phone_number: None }, ())
.unwrap_err();
println!("{:?}", errors.get("phone_number").unwrap().reason); // "Provide a phone number when email is missing"

let (contact, _ctx_options) = ContactModel
.create(
PartialContact { email: Some(Some("ada@example.com".into())), phone_number: None },
(),
)
.unwrap();
println!("{:?}", contact); // Contact { email: Some("ada@example.com"), phone_number: None }
}

Ignore rules​

An ignore rule drops a provided value, so the field keeps its current value (its default on create): #[ignore(|ctx, opts| ...)] on create and on update when the handler returns true, #[ignore_init] always on create, #[ignore_update] always on update.

#[ignore]​

main.rs
use ivo::ivo_schema;

#[derive(Debug, Clone)]
pub struct Caller {
pub is_admin: bool,
}

#[ivo_schema(input(User, ivo_attrs(derive(Debug, Clone, PartialEq))), ctx_options(super::Caller))]
mod user_schema {
struct Fields {
#[lax(String::new())]
pub name: String,

// only admins may set a role: anyone else's is ignored
#[lax("guest".to_string())]
#[ignore(|_ctx, opts| !opts.read_sync().is_admin)]
pub role: String,
}
}

fn main() {
use user_schema::{PartialUser, UserModel};

let input = || PartialUser { name: Some("Ada".into()), role: Some("admin".into()) };

let (user, _ctx_options) = UserModel.create(input(), Caller { is_admin: false }).unwrap();
println!("{:?}", user); // User { name: "Ada", role: "guest" }

let (admin, _ctx_options) = UserModel.create(input(), Caller { is_admin: true }).unwrap();
println!("{:?}", admin); // User { name: "Ada", role: "admin" }
}

#[ignore_init]​

main.rs
use ivo::ivo_schema;

#[ivo_schema(input(Profile, ivo_attrs(derive(Debug, Clone, PartialEq))))]
mod profile_schema {
struct Fields {
#[lax(String::new())]
pub name: String,

// the bio can only be set by an update
#[lax(String::new())]
#[ignore_init]
pub bio: String,
}
}

fn main() {
use profile_schema::{PartialProfile, ProfileModel};

let (profile, _ctx_options) = ProfileModel
.create(PartialProfile { name: Some("Ada".into()), bio: Some("Hi!".into()) }, ())
.unwrap();
println!("{:?}", profile); // Profile { name: "Ada", bio: "" }

let (updated, _ctx_options) = ProfileModel
.update(profile, PartialProfile { name: None, bio: Some("Hi!".into()) }, ())
.unwrap();
println!("{:?}", updated); // PartialProfile { name: None, bio: Some("Hi!") }
}

#[ignore_update]​

main.rs
use ivo::ivo_schema;

#[ivo_schema(input(Signup, ivo_attrs(derive(Debug, Clone, PartialEq))))]
mod signup_schema {
struct Fields {
#[lax(String::new())]
pub email: String,

// the referrer can only be set on create
#[lax(None)]
#[ignore_update]
pub referrer: Option<String>,
}
}

fn main() {
use signup_schema::{PartialSignup, SignupModel};

let (signup, _ctx_options) = SignupModel
.create(
PartialSignup {
email: Some("ada@example.com".into()),
referrer: Some(Some("grace".into())),
},
(),
)
.unwrap();
println!("{:?}", signup); // Signup { email: "ada@example.com", referrer: Some("grace") }

let (errors, _ctx_options) = SignupModel
.update(signup, PartialSignup { email: None, referrer: Some(Some("alan".into())) }, ())
.unwrap_err();
println!("{:?}", errors); // None
}

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…