From 5c049911ee5cbe7f943cc6a6802761bbce1ec336 Mon Sep 17 00:00:00 2001 From: icarus Date: Mon, 20 Oct 2025 00:30:08 +0800 Subject: [PATCH] refactor(ocr): replace manual type check with zod schema for provider ids Use zod schema validation for BuiltinOcrProviderId type to improve type safety and maintainability --- src/renderer/src/types/ocr.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/types/ocr.ts b/src/renderer/src/types/ocr.ts index 66e0f9616f..44a27f9de8 100644 --- a/src/renderer/src/types/ocr.ts +++ b/src/renderer/src/types/ocr.ts @@ -1,4 +1,5 @@ import type Tesseract from 'tesseract.js' +import * as z from 'zod' import type { FileMetadata, ImageFileMetadata, TranslateLanguageCode } from '.' import { isImageFileMetadata } from '.' @@ -10,10 +11,12 @@ export const BuiltinOcrProviderIds = { ovocr: 'ovocr' } as const -export type BuiltinOcrProviderId = keyof typeof BuiltinOcrProviderIds +export const BuiltinOcrProviderIdSchema = z.enum(['tesseract', 'system', 'paddleocr', 'ovocr']) + +export type BuiltinOcrProviderId = z.infer export const isBuiltinOcrProviderId = (id: string): id is BuiltinOcrProviderId => { - return Object.hasOwn(BuiltinOcrProviderIds, id) + return BuiltinOcrProviderIdSchema.safeParse(id).success } // extensible