diff --git a/packages/core/src/collections/settings.ts b/packages/core/src/collections/settings.ts index 5ed292c7d..ca5092bcb 100644 --- a/packages/core/src/collections/settings.ts +++ b/packages/core/src/collections/settings.ts @@ -105,6 +105,8 @@ export class Settings implements ICollection { value: SettingItemMap[TKey] ) { const id = KEY_IDS[key]; + if (!id) throw new Error(`Invalid settings key: ${key}.`); + const oldItem = this.collection.get(id); if (oldItem && oldItem.key !== key) throw new Error("Key conflict."); diff --git a/packages/core/src/migrations.ts b/packages/core/src/migrations.ts index 830cd6166..d140e18cc 100644 --- a/packages/core/src/migrations.ts +++ b/packages/core/src/migrations.ts @@ -25,14 +25,14 @@ import { makeId } from "./utils/id"; import { Color, ContentItem, - GroupingKey, HistorySession, Item, ItemMap, ItemType, MaybeDeletedItem, ToolbarConfigPlatforms, - isDeleted + isDeleted, + isGroupingKey } from "./types"; import { isCipher } from "./database/crypto"; import { IndexedCollection } from "./database/indexed-collection"; @@ -390,9 +390,10 @@ const migrations: Migration[] = [ if (item.groupOptions) { for (const key in item.groupOptions) { - const value = item.groupOptions[key as GroupingKey]; + if (!isGroupingKey(key)) continue; + const value = item.groupOptions[key]; if (!value) continue; - await db.settings.setGroupOptions(key as GroupingKey, value); + await db.settings.setGroupOptions(key, value); } } if (item.toolbarConfig) { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index b2e0d4d15..f4733a463 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -40,14 +40,16 @@ export type GroupOptions = SortOptions & { export type GroupedItems = (T | GroupHeader)[]; -export type GroupingKey = - | "home" - | "notes" - | "notebooks" - | "tags" - | "trash" - | "favorites" - | "reminders"; +export const GroupingKey = [ + "home", + "notes", + "notebooks", + "tags", + "trash", + "favorites", + "reminders" +] as const; +export type GroupingKey = (typeof GroupingKey)[number]; export type ValueOf = T[keyof T]; export type Optional = Pick, K> & Omit; @@ -552,6 +554,10 @@ export function isGroupHeader(item: any): item is GroupHeader { return item.type === "header"; } +export function isGroupingKey(key: any): key is GroupingKey { + return GroupingKey.includes(key); +} + export type ContentBlock = { content: string; type: string;