diff --git a/Cargo.lock b/Cargo.lock index 339993ab4b..760337f8c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3781,6 +3781,7 @@ name = "enums" version = "0.0.1" dependencies = [ "log", + "regex", "serde", "serde_derive", "serde_json", diff --git a/crates/schema/public/enums/Cargo.toml b/crates/schema/public/enums/Cargo.toml index 8b300e8ce9..b74e9f60fc 100644 --- a/crates/schema/public/enums/Cargo.toml +++ b/crates/schema/public/enums/Cargo.toml @@ -47,3 +47,4 @@ strum.workspace = true # Test serialization serde_json.workspace = true toml.workspace = true +regex.workspace = true diff --git a/crates/schema/public/enums/src/common/generation/common_model_type.rs b/crates/schema/public/enums/src/common/generation/common_model_type.rs index acbec067b1..e8334bfb84 100644 --- a/crates/schema/public/enums/src/common/generation/common_model_type.rs +++ b/crates/schema/public/enums/src/common/generation/common_model_type.rs @@ -12,6 +12,9 @@ pub const MAX_LENGTH: usize = 24; /// Common model type enum used across image, video, 3D, and splat generation. /// +/// This is used in the MySQL database, the HTTP API, and throughout +/// ArtCraft (the Tauri app). +/// /// Stored in the `prompts` table (`maybe_model_type` column). /// Also used by other tables and the sqlite tasks table. /// @@ -650,5 +653,31 @@ mod tests { assert!(serialized.len() <= MAX_LENGTH, "variant {:?} is too long", variant); } } + + #[test] + fn serialized_names_must_not_contain_dots() { + for variant in CommonModelType::all_variants() { + let to_str_value = variant.to_str(); + assert!(!to_str_value.contains('.'), "to_str() for {:?} contains a dot: {:?}", variant, to_str_value); + + let json_value = serde_json::to_string(&variant).unwrap().replace('"', ""); + assert!(!json_value.contains('.'), "JSON serialization for {:?} contains a dot: {:?}", variant, json_value); + } + } + + #[test] + fn serialized_names_must_only_contain_lowercase_alphanumeric_and_underscore() { + let valid_pattern = regex::Regex::new(r"^[a-z0-9_]+$").unwrap(); + + for variant in CommonModelType::all_variants() { + let to_str_value = variant.to_str(); + assert!(valid_pattern.is_match(to_str_value), + "to_str() for {:?} contains invalid characters: {:?} (only a-z, 0-9, _ allowed)", variant, to_str_value); + + let json_value = serde_json::to_string(&variant).unwrap().replace('"', ""); + assert!(valid_pattern.is_match(&json_value), + "JSON serialization for {:?} contains invalid characters: {:?} (only a-z, 0-9, _ allowed)", variant, json_value); + } + } } }