刘光辉
13 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type {
  ControlType,
  DropdownOption,
  RawTypeConfig,
  TypeConfigValidationField,
  ValidatedDropdownConfig,
  ValidatedTypeConfig,
  ValidationResult,
} from './types';
 
const SUPPORTED_CONTROL_TYPES = ['text', 'dropdown', 'checkbox', 'date', 'multiline'] as const satisfies readonly ControlType[];
 
export const CONTROL_TYPES: readonly ControlType[] = Object.freeze([...SUPPORTED_CONTROL_TYPES]);
 
function isControlType(value: unknown): value is ControlType {
  return SUPPORTED_CONTROL_TYPES.some((controlType) => controlType === value);
}
 
export function normalizeControlType(value: unknown): ControlType {
  return isControlType(value) ? value : 'text';
}
 
function isStrictDate(value: string): boolean {
  const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
 
  if (!match) {
    return false;
  }
 
  const year = Number(match[1]);
  const month = Number(match[2]);
  const day = Number(match[3]);
  const date = new Date(0);
 
  if (year < 1) {
    return false;
  }
 
  date.setUTCHours(0, 0, 0, 0);
  date.setUTCFullYear(year, month - 1, day);
 
  return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
}
 
function validateDropdown(raw: RawTypeConfig, placeholder: string): ValidationResult<ValidatedDropdownConfig, TypeConfigValidationField> {
  const options: DropdownOption[] = (raw.options ?? []).map((item) => ({
    display: String(item.display || '').trim(),
    value: String(item.value || '').trim(),
  }));
 
  if (options.length < 2 || options.some((item) => !item.display || !item.value)) {
    return {
      ok: false,
      field: 'dropdown-options',
      message: '下拉框至少需要两个完整选项',
    };
  }
 
  const values = options.map((item) => item.value);
  const hasDuplicate = values.some((value, index) => values.indexOf(value) !== index);
 
  if (hasDuplicate) {
    return {
      ok: false,
      field: 'dropdown-options',
      message: '下拉选项业务值不能重复',
    };
  }
 
  const defaultValue = String(raw.defaultOptionValue || '').trim();
  const selectedIndex = defaultValue ? values.indexOf(defaultValue) : -1;
 
  if (defaultValue && selectedIndex === -1) {
    return {
      ok: false,
      field: 'dropdown-default',
      message: '默认项必须来自下拉选项',
    };
  }
 
  return {
    ok: true,
    value: {
      controlType: 'dropdown',
      placeholder,
      options,
      selectedIndex,
    },
  };
}
 
export function validateTypeConfig(raw: RawTypeConfig | null | undefined): ValidationResult<ValidatedTypeConfig, TypeConfigValidationField> {
  if (!raw) {
    return {
      ok: false,
      field: 'control-type',
      message: '请选择有效的字段类型',
    };
  }
 
  const type = raw.controlType;
 
  if (!isControlType(type)) {
    return {
      ok: false,
      field: 'control-type',
      message: '请选择有效的字段类型',
    };
  }
 
  const placeholder = String(raw.placeholder || '').trim();
  const defaultValue = String(raw.defaultValue || '');
 
  if (type === 'dropdown') {
    return validateDropdown(raw, placeholder);
  }
 
  if (type === 'text' && /[\r\n]/.test(defaultValue)) {
    return {
      ok: false,
      field: 'text-default',
      message: '单行文本默认值不能换行',
    };
  }
 
  if (type === 'date' && defaultValue && !isStrictDate(defaultValue)) {
    return {
      ok: false,
      field: 'date-default',
      message: '请输入有效的日期',
    };
  }
 
  return {
    ok: true,
    value: {
      controlType: type,
      placeholder,
      defaultValue,
      defaultChecked: Boolean(raw.defaultChecked),
    },
  };
}
 
function hasExactTag(control: unknown, tag: string): boolean {
  return typeof control === 'object' && control !== null && 'Tag' in control && control.Tag === tag;
}
 
export function countTag(controls: unknown, tag: string): number {
  if (!Array.isArray(controls)) {
    return 0;
  }
 
  return (controls as readonly unknown[]).filter((control) => hasExactTag(control, tag)).length;
}