Skip to main content
Version: 0.6.0

Required Fields

A required field is both an input and output field whose value must be provided at creation (e.g. username). It's optional (and immutable unless explicitly allowed) on update.

  • Declared with the bare #[required] field-type attribute.
  • Must have a validator via #[validate]; may also have a #[re_validate].
  • May customize the missing-field error via #[required_error(...)] -- a static string or a |raw_input, opts| -> String closure.
  • May leverage #[ignore_update] (resolver form only) and #[readonly] to prevent further updates.
  • May have on_delete and on_success event handlers, and on_failure.

Example​

use ivo::ivo_schema;

#[ivo_schema(input(DataInput, derive(Debug, Clone, PartialEq)))]
mod required_schema {
struct Fields {
#[required]
#[validate(|v: String, _, _| {
if v.len() < 3 {
return Err(("username too short".into(), None));
}
Ok(None)
})]
pub username: String,
}
}

fn main() {
let (err, _ctx_options) = required_schema::DataInputModel
.create(required_schema::PartialDataInput { username: None }, ())
.unwrap_err();
println!("{:?}", err.get("username").unwrap().reason); // "field is required"

let (created, _ctx_options) = required_schema::DataInputModel
.create(
required_schema::PartialDataInput {
username: Some("jane".into()),
},
(),
)
.unwrap();
println!("{:?}", created); // DataInput { username: "jane" }
}

Custom required error​

#[required]
#[required_error(|_raw_input, _opts| "\"username\" was not provided!".to_string())]
#[validate(|v: String, _, _| Ok(Some(v)))]
pub username: String,

More examples​

Try it in the browser​

username is required with no other constraints - leave the input empty to see the required error, or provide a value to see it accepted.

Loading playground…