All files / src/utils ConfigValidator.ts

100% Statements 5/5
100% Branches 8/8
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                      3x         3x 2x 2x                 1x    
import { EngineError } from "../errors/EngineError.js";
import { EngineErrorCode, IEngineConfig } from "../types.js";
import { createI18nKey } from "../protocol/ProtocolValidator.js";
 
/**
 * エンジンのソース設定を結合・検証し、必須となる "main" ソースの存在を保証します。
 * 複数のアダプターファクトリ間でロジックを共有するために使用されます。
 * (2026 Zenith Tier: Centralized Validation)
 *
 * @param registrySources - レジストリから取得したデフォルトのリソース設定
 * @param config - ユーザーから渡されたエンジン設定
 * @param defaultEngineId - IDが指定されなかった場合のフォールバックID
 * @returns 検証済みで必須の sources.main を含むリソース設定
 */
export function normalizeAndValidateSources(
  registrySources: IEngineConfig["sources"] | null | undefined,
  config: IEngineConfig,
  defaultEngineId: string,
): NonNullable<IEngineConfig["sources"]> {
  const sources = {
    ...(registrySources || {}),
    ...(config.sources || {}),
  } as NonNullable<IEngineConfig["sources"]>;
 
  if (!sources.main) {
    const engineId = config.id || defaultEngineId;
    throw new EngineError({
      code: EngineErrorCode.VALIDATION_ERROR,
      message: `[createEngine] Engine "${engineId}" requires a "main" source, but it was not found in the registry or config.`,
      engineId,
      i18nKey: createI18nKey("factory.requiresMainSource"),
      i18nParams: { id: engineId },
    });
  }
 
  return sources as NonNullable<IEngineConfig["sources"]>;
}