Skip to content

Schema API

v3sf Schema 的完整类型定义参考。

Schema

SchemaPartial<SchemaBase> 的类型别名,所有属性均为可选。

ts
type Schema = Partial<SchemaBase>

SchemaBase

SchemaBase 是 Schema 的完整接口定义,包含所有可用属性。

ts
interface SchemaBase {
  type: ValueType
  title: string
  required: boolean
  placeholder: string
  disabled: boolean
  readonly: boolean
  hidden: boolean
  displayType: 'row' | 'column'
  className: string
  widget: string
  properties: Record<string, Schema>
  enum: Array<string | number>
  enumNames: Array<string | number>
  rules: ValidatorRule | ValidatorRule[]
  props: Record<string, any>
  border: boolean
}

属性说明

属性类型说明
typeValueType字段值类型
titlestring字段显示标签
requiredboolean是否必填。在 Schema 中也可以使用 {{ }} 表达式
placeholderstring占位提示文本
disabledboolean是否禁用。在 Schema 中也可以使用 {{ }} 表达式
readonlyboolean是否只读。在 Schema 中也可以使用 {{ }} 表达式
hiddenboolean是否隐藏。在 Schema 中也可以使用 {{ }} 表达式
displayType'row' | 'column'标签与输入框的布局方式。row 为水平排列,column 为垂直排列
classNamestring自定义 CSS 类名,添加到字段容器上
widgetstring渲染组件名称。未指定时根据 type 自动推断
propertiesRecord<string, Schema>嵌套字段定义,当 typeobject 时使用
enumArray<string | number>选项值数组,用于 select/radio/checkbox 等组件
enumNamesArray<string | number>选项标签数组,与 enum 一一对应
rulesValidatorRule | ValidatorRule[]校验规则,支持单条或数组
propsRecord<string, any>透传给底层 UI 组件的额外属性,值支持 {{ }} 表达式
borderboolean是否显示边框

SchemaRaw

SchemaRaw 是 Schema 在原始输入时的类型,允许属性值为 {{ }} 表达式字符串。

ts
type Stringify<T extends Record<string, any>> = {
  [K in keyof T]?: T[K] extends Record<string, unknown> ? Stringify<T[K]> : T[K] | `{{${string}}}`
}

type SchemaRaw = Stringify<SchemaBase>

SchemaRawSchema 的区别在于:SchemaRaw 中的属性值可以是 {{ }} 表达式字符串,而 Schema 中的属性值是表达式求值后的实际值。

ValueType

ts
type ValueType = 'string' | 'object' | 'array' | 'number' | 'boolean' | 'date' | (string & {}) // 允许自定义类型
说明默认 widget
'string'字符串input
'number'数字number
'boolean'布尔值switch
'array'数组checkbox
'date'日期date
'object'嵌套对象递归渲染 properties

ValueType 使用 (string & {}) 扩展允许传入自定义类型字符串,同时保留类型提示。

ValidatorRule

校验规则对象的类型定义。

ts
interface ValidatorRule {
  required?: boolean
  pattern?: RegExp | string
  min?: number
  max?: number
  len?: number
  type?: string
  message?: string
  custom?: (value: any, formData: FormData) => string | true
}

详见 校验规则 API

FormData

ts
type FormData = Record<string, any>

表单数据对象,key 为字段名,value 为字段值。

Deps

ts
type Deps = Record<string, any>

外部依赖数据对象,通过组件 deps prop 传入,在表达式中通过 $deps 访问。

ErrorMessage

ts
interface ErrorMessage {
  name: string // 字段名
  error: string[] // 错误信息列表
}

校验返回的错误信息结构。name 对应字段名,error 为该字段的所有错误信息。

Options

ts
interface Options {
  label: string
  value: string | number
  props?: Record<string, any>
}

选项数据结构,内部将 enum / enumNames 转换为此格式。

Released under the MIT License.