From 40050983e542258f971560ab40727cfdbc8671f2 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Mon, 22 Dec 2025 13:26:12 +0100 Subject: [PATCH] Add static type reference model generation to all apps to prevent using magic strings (#1404) --- .../app/vaultstore/models/FieldType.kt | 67 +++ .../app/vaultstore/models/ItemType.kt | 42 ++ .../ios/VaultModels/FieldType.swift | 44 ++ .../mobile-app/ios/VaultModels/ItemType.swift | 29 ++ .../Main/Components/Fields/FieldBlock.razor | 10 +- .../Main/Components/Items/ItemCard.razor | 5 +- .../Main/Components/Items/ItemIcon.razor | 8 +- .../Components/Items/ItemTypeSelector.razor | 24 +- .../Widgets/CreateNewIdentityWidget.razor | 49 +-- .../Main/Pages/Items/AddEdit.razor | 14 +- .../Main/Pages/Items/Home.razor | 9 +- .../Main/Pages/Items/View.razor | 14 +- .../AliasClientDb/Models/FieldType.cs | 73 ++++ .../AliasClientDb/Models/ItemType.cs} | 25 +- core/models/scripts/generate-field-keys.cjs | 396 +++++++++++++++++- 15 files changed, 730 insertions(+), 79 deletions(-) create mode 100644 apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/FieldType.kt create mode 100644 apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/ItemType.kt create mode 100644 apps/mobile-app/ios/VaultModels/FieldType.swift create mode 100644 apps/mobile-app/ios/VaultModels/ItemType.swift create mode 100644 apps/server/Databases/AliasClientDb/Models/FieldType.cs rename apps/server/{AliasVault.Client/Main/Models/ItemTypes.cs => Databases/AliasClientDb/Models/ItemType.cs} (55%) diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/FieldType.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/FieldType.kt new file mode 100644 index 000000000..2aa7b5589 --- /dev/null +++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/FieldType.kt @@ -0,0 +1,67 @@ +// +// This file is auto-generated from core/models/src/vault/Item.ts +// Do not edit this file directly. Run 'npm run generate:models' to regenerate. + +package net.aliasvault.app.vaultstore.models + +/** + * Field types for rendering and validation. + */ +object FieldType { + /** + * Text field type. + */ + const val TEXT = "Text" + + /** + * Password field type. + */ + const val PASSWORD = "Password" + + /** + * Hidden field type. + */ + const val HIDDEN = "Hidden" + + /** + * Email field type. + */ + const val EMAIL = "Email" + + /** + * URL field type. + */ + const val U_R_L = "URL" + + /** + * Date field type. + */ + const val DATE = "Date" + + /** + * Number field type. + */ + const val NUMBER = "Number" + + /** + * Phone field type. + */ + const val PHONE = "Phone" + + /** + * TextArea field type. + */ + const val TEXT_AREA = "TextArea" + + /** + * All available field types. + */ + val all = listOf(TEXT, PASSWORD, HIDDEN, EMAIL, U_R_L, DATE, NUMBER, PHONE, TEXT_AREA) + + /** + * Checks if a string value is a valid field type. + */ + fun isValid(value: String?): Boolean { + return value in all + } +} diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/ItemType.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/ItemType.kt new file mode 100644 index 000000000..fa0cbe00e --- /dev/null +++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/ItemType.kt @@ -0,0 +1,42 @@ +// +// This file is auto-generated from core/models/src/vault/Item.ts +// Do not edit this file directly. Run 'npm run generate:models' to regenerate. + +package net.aliasvault.app.vaultstore.models + +/** + * Item types supported by the vault. + */ +object ItemType { + /** + * Login item type. + */ + const val LOGIN = "Login" + + /** + * Alias item type. + */ + const val ALIAS = "Alias" + + /** + * CreditCard item type. + */ + const val CREDIT_CARD = "CreditCard" + + /** + * Note item type. + */ + const val NOTE = "Note" + + /** + * All available item types. + */ + val all = listOf(LOGIN, ALIAS, CREDIT_CARD, NOTE) + + /** + * Checks if a string value is a valid item type. + */ + fun isValid(value: String?): Boolean { + return value in all + } +} diff --git a/apps/mobile-app/ios/VaultModels/FieldType.swift b/apps/mobile-app/ios/VaultModels/FieldType.swift new file mode 100644 index 000000000..88ca2a977 --- /dev/null +++ b/apps/mobile-app/ios/VaultModels/FieldType.swift @@ -0,0 +1,44 @@ +// +// This file is auto-generated from core/models/src/vault/Item.ts +// Do not edit this file directly. Run 'npm run generate:models' to regenerate. + +import Foundation + +/// Field types for rendering and validation. +public struct FieldType { + /// Text field type. + public static let text = "Text" + + /// Password field type. + public static let password = "Password" + + /// Hidden field type. + public static let hidden = "Hidden" + + /// Email field type. + public static let email = "Email" + + /// URL field type. + public static let uRL = "URL" + + /// Date field type. + public static let date = "Date" + + /// Number field type. + public static let number = "Number" + + /// Phone field type. + public static let phone = "Phone" + + /// TextArea field type. + public static let textArea = "TextArea" + + /// All available field types. + public static let all = [text, password, hidden, email, uRL, date, number, phone, textArea] + + /// Checks if a string value is a valid field type. + public static func isValid(_ value: String?) -> Bool { + guard let value = value else { return false } + return all.contains(value) + } +} diff --git a/apps/mobile-app/ios/VaultModels/ItemType.swift b/apps/mobile-app/ios/VaultModels/ItemType.swift new file mode 100644 index 000000000..ce09b3bbf --- /dev/null +++ b/apps/mobile-app/ios/VaultModels/ItemType.swift @@ -0,0 +1,29 @@ +// +// This file is auto-generated from core/models/src/vault/Item.ts +// Do not edit this file directly. Run 'npm run generate:models' to regenerate. + +import Foundation + +/// Item types supported by the vault. +public struct ItemType { + /// Login item type. + public static let login = "Login" + + /// Alias item type. + public static let alias = "Alias" + + /// CreditCard item type. + public static let creditCard = "CreditCard" + + /// Note item type. + public static let note = "Note" + + /// All available item types. + public static let all = [login, alias, creditCard, note] + + /// Checks if a string value is a valid item type. + public static func isValid(_ value: String?) -> Bool { + guard let value = value else { return false } + return all.contains(value) + } +} diff --git a/apps/server/AliasVault.Client/Main/Components/Fields/FieldBlock.razor b/apps/server/AliasVault.Client/Main/Components/Fields/FieldBlock.razor index fcc36995e..9b87e4b7d 100644 --- a/apps/server/AliasVault.Client/Main/Components/Fields/FieldBlock.razor +++ b/apps/server/AliasVault.Client/Main/Components/Fields/FieldBlock.razor @@ -5,8 +5,8 @@ @* FieldBlock component - renders a single field based on its type *@ @switch (Field.FieldType) { - case "Password": - case "Hidden": + case FieldType.Password: + case FieldType.Hidden:
break; - case "TextArea": + case FieldType.TextArea:
@@ -22,7 +22,7 @@
break; - case "URL": + case FieldType.URL:
@if (!string.IsNullOrEmpty(Field.Value)) @@ -40,7 +40,7 @@ }
break; - case "Date": + case FieldType.Date:
private string GetDisplayText() { - if (Obj.ItemType == ItemTypes.CreditCard) + if (Obj.ItemType == ItemType.CreditCard) { // For credit cards, show masked card number if available if (!string.IsNullOrEmpty(Obj.CardNumber) && Obj.CardNumber.Length >= 4) @@ -78,7 +79,7 @@ return string.Empty; } - if (Obj.ItemType == ItemTypes.Note) + if (Obj.ItemType == ItemType.Note) { // For notes, no secondary text needed return string.Empty; diff --git a/apps/server/AliasVault.Client/Main/Components/Items/ItemIcon.razor b/apps/server/AliasVault.Client/Main/Components/Items/ItemIcon.razor index 6f0cca183..2d686285c 100644 --- a/apps/server/AliasVault.Client/Main/Components/Items/ItemIcon.razor +++ b/apps/server/AliasVault.Client/Main/Components/Items/ItemIcon.razor @@ -1,12 +1,14 @@ @using AliasVault.Client.Main.Models +@using AliasClientDb.Models @using AliasVault.Client.Main.Utilities +@using ItemTypeClass = AliasClientDb.Models.ItemType @* ItemIcon component - displays contextually appropriate icons based on item type *@ @* For Login/Alias: Uses the Logo field if available, falls back to key placeholder *@ @* For CreditCard: Shows card brand icons (Visa, MC, Amex, Discover) based on card number *@ @* For Note: Shows a document/note icon *@ -@if (ItemType == ItemTypes.Note) +@if (ItemType == ItemTypeClass.Note) { @* Note icon - document style *@ @@ -17,7 +19,7 @@ } -else if (ItemType == ItemTypes.CreditCard) +else if (ItemType == ItemTypeClass.CreditCard) { @* Credit card icon - detect brand and show appropriate icon *@ @switch (CardBrandDetector.Detect(CardNumber)) @@ -92,7 +94,7 @@ else /// Gets or sets the item type (Login, Alias, CreditCard, Note). /// [Parameter] - public string ItemType { get; set; } = ItemTypes.Login; + public string ItemType { get; set; } = ItemTypeClass.Login; /// /// Gets or sets the logo bytes for Login/Alias items. diff --git a/apps/server/AliasVault.Client/Main/Components/Items/ItemTypeSelector.razor b/apps/server/AliasVault.Client/Main/Components/Items/ItemTypeSelector.razor index 98ee97574..8c1e0938b 100644 --- a/apps/server/AliasVault.Client/Main/Components/Items/ItemTypeSelector.razor +++ b/apps/server/AliasVault.Client/Main/Components/Items/ItemTypeSelector.razor @@ -1,4 +1,6 @@ @using AliasVault.Client.Main.Models +@using AliasClientDb.Models +@using ItemTypeClass = AliasClientDb.Models.ItemType @using Microsoft.Extensions.Localization @inject IStringLocalizerFactory LocalizerFactory @@ -22,7 +24,7 @@ - @if (SelectedType == ItemTypes.Alias && !IsEditMode && OnRegenerateAlias.HasDelegate) + @if (SelectedType == ItemType.Alias && !IsEditMode && OnRegenerateAlias.HasDelegate) {
- @foreach (var itemType in ItemTypes.All) + @foreach (var itemType in ItemType.All) {
- @if (Model.ItemType == ItemTypes.Login || Model.ItemType == ItemTypes.Alias) + @if (Model.ItemType == ItemType.Login || Model.ItemType == ItemType.Alias) {
@@ -52,7 +53,7 @@ }