diff --git a/app/src/components/create_user_attribute.rs b/app/src/components/create_user_attribute.rs index 3171447..569c457 100644 --- a/app/src/components/create_user_attribute.rs +++ b/app/src/components/create_user_attribute.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use crate::{ components::{ form::{checkbox::CheckBox, field::Field, select::Select, submit::Submit}, @@ -12,6 +14,7 @@ use crate::{ use anyhow::{bail, Result}; use gloo_console::log; use graphql_client::GraphQLQuery; +use validator::ValidationError; use validator_derive::Validate; use yew::prelude::*; use yew_form_derive::Model; @@ -37,13 +40,21 @@ pub struct CreateUserAttributeForm { pub struct CreateUserAttributeModel { #[validate(length(min = 1, message = "attribute_name is required"))] attribute_name: String, - #[validate(length(min = 1, message = "attribute_type is required"))] + #[validate(custom = "validate_attribute_type")] attribute_type: String, is_editable: bool, is_list: bool, is_visible: bool, } +fn validate_attribute_type(attribute_type: &str) -> Result<(), ValidationError> { + let result = AttributeType::from_str(attribute_type); + match result { + Ok(_) => Ok(()), + _ => Err(ValidationError::new("Invalid attribute type")), + } +} + pub enum Msg { Update, SubmitForm, diff --git a/app/src/components/user_schema_table.rs b/app/src/components/user_schema_table.rs index 2571b77..45e4446 100644 --- a/app/src/components/user_schema_table.rs +++ b/app/src/components/user_schema_table.rs @@ -53,19 +53,21 @@ impl CommonComponent for UserSchemaTable { Ok(true) } Msg::OnError(e) => Err(e), - Msg::OnAttributeDeleted(attribute_name) => match self.attributes { - None => { - log!("Attribute deleted but component has no attributes"); - Err(anyhow!("invalid state")) + Msg::OnAttributeDeleted(attribute_name) => { + match self.attributes { + None => { + log!(format!("Attribute {attribute_name} was deleted but component has no attributes")); + Err(anyhow!("invalid state")) + } + Some(_) => { + self.attributes + .as_mut() + .unwrap() + .retain(|a| a.name != attribute_name); + Ok(true) + } } - Some(_) => { - self.attributes - .as_mut() - .unwrap() - .retain(|a| a.name != attribute_name); - Ok(true) - } - }, + } } } @@ -158,7 +160,7 @@ impl UserSchemaTable { { if hardcoded { html!{} - } else { + } else { html!{