刘光辉
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
import { validateTypeConfig } from './field-types';
import { buildTag, normalizeId } from './tags';
import type { FieldDefinition, FieldDefinitionInput, FieldDefinitionValidationField, ValidationResult } from './types';
 
export function createFieldDefinition(input: FieldDefinitionInput): ValidationResult<FieldDefinition, FieldDefinitionValidationField> {
  const fieldCode = normalizeId(input.fieldCode);
  const alias = input.alias.trim();
  const groupCode = normalizeId(input.groupCode);
  const rowId = normalizeId(input.rowId);
 
  if (!fieldCode) {
    return { ok: false, field: 'field-code', message: '请输入有效的字段编码' };
  }
 
  if (!alias) {
    return { ok: false, field: 'field-alias', message: '请输入字段名称' };
  }
 
  if (input.mode === 'repeat' && !groupCode) {
    return { ok: false, field: 'group-code', message: '请输入有效的重复组编码' };
  }
 
  if (input.mode === 'repeat' && !rowId) {
    return { ok: false, field: 'row-id', message: '请生成或输入行 ID' };
  }
 
  const typeResult = validateTypeConfig(input.typeConfig);
 
  if (!typeResult.ok) {
    return typeResult;
  }
 
  const typeConfig = typeResult.value;
 
  return {
    ok: true,
    value: {
      mode: input.mode,
      fieldCode,
      alias,
      groupCode,
      rowId,
      tag: buildTag(input.mode, fieldCode, groupCode, rowId),
      color: input.mode === 'repeat' ? { R: 171, G: 126, B: 24 } : { R: 23, G: 109, B: 92 },
      ...typeConfig,
      placeholder: typeConfig.placeholder || alias,
    },
  };
}