config-overlay.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. export interface ConfigValidationIssue {
  2. readonly code:
  3. | "required-field-missing"
  4. | "duplicate-key"
  5. | "duplicate-model"
  6. | "invalid-option-spec"
  7. | "invalid-config"
  8. | "invalid-reasoning-mapping"
  9. | "invalid-pattern"
  10. | "invalid-url"
  11. | "missing-template";
  12. readonly path: readonly string[];
  13. readonly message: string;
  14. }
  15. /**
  16. * 稀疏配置层和覆盖完成后的配置使用同一类型。
  17. *
  18. * 子类显式列出字段;本基类只统一“缺省继承、Config 递归覆盖、其他值整体替换”的语义。
  19. */
  20. export abstract class ConfigOverlay<TSelf extends ConfigOverlay<TSelf>> {
  21. abstract overlay(next: TSelf): TSelf;
  22. abstract validateComplete(path?: readonly string[]): readonly ConfigValidationIssue[];
  23. protected overlayValue<T>(base: T | undefined, next: T | undefined): T | undefined {
  24. return next === undefined ? base : next;
  25. }
  26. protected overlayConfig<T extends ConfigOverlay<T>>(
  27. base: T | null | undefined,
  28. next: T | null | undefined,
  29. ): T | null | undefined {
  30. if (next === undefined) return base;
  31. if (next === null || base === null || base === undefined) return next;
  32. return base.overlay(next);
  33. }
  34. }
  35. export function requiredFieldIssue(path: readonly string[], field: string): ConfigValidationIssue {
  36. return {
  37. code: "required-field-missing",
  38. path: [...path, field],
  39. message: `缺少必填配置 ${[...path, field].join(".")}`,
  40. };
  41. }