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| -> Stringclosure. - May leverage
#[ignore_update](resolver form only) and#[readonly]to prevent further updates. - May have
on_deleteandon_successevent handlers, andon_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 = required_schema::DataInputModel
.create(required_schema::PartialDataInput { username: None }, ())
.unwrap_err();
println!("{:?}", err.errors.get("username").unwrap().reason); // "field is required"
let created = required_schema::DataInputModel
.create(
required_schema::PartialDataInput {
username: Some("jane".into()),
},
(),
)
.unwrap();
println!("{:?}", created.data); // 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…