FOSMVVM 字段生成器:Swift 表单校验 - Openclaw Skills

作者:互联网

2026-03-30

AI教程

什么是 FOSMVVM 字段生成器?

FOSMVVM 字段生成器是 Openclaw Skills 生态系统中的一个高效工具,旨在为用户输入实现单一事实来源模式。通过一次性定义表单规范,开发者可以在用于 HTTP 传输的 ServerRequestBodies、用于 UI 渲染的 ViewModels 以及用于持久化校验的 Models 中采用相同的协议。这种方法确保了校验规则和本地化字符串在整个应用栈中保持一致。

该技能专为与 FOSMVVM 架构无缝集成而构建,可自动创建符合 ValidatableModel 的复杂 Swift 协议。它处理 FormField 定义的样板代码,包括键盘类型、自动填充语义和基于范围的约束,使其成为开发者使用 Openclaw Skills 加速 Swift 开发工作流的重要组件。

下载入口:https://github.com/openclaw/skills/tree/main/skills/foscomputerservices/fosmvvm-fields-generator

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install fosmvvm-fields-generator

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 fosmvvm-fields-generator。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

FOSMVVM 字段生成器 应用场景

  • 为创建、编辑、过滤或搜索功能定义新的输入表单。
  • 在客户端 ViewModel 和服务器端请求体之间实现共享校验逻辑。
  • 使现有数据类型符合 ValidatableModel 协议,以进行自动化错误处理。
  • 生成本地化表单资产,包括标题、占位符和特定的校验错误消息。
FOSMVVM 字段生成器 工作原理
  1. 该技能分析对话上下文,以确定表单的用途、实体关系和所需字段。
  2. 它确定协议、消息结构体和校验方法的适当命名规范。
  3. 生成包含 FormField 定义和校验逻辑(例如长度范围、必填状态)的 Swift 协议。
  4. 使用 @FieldValidationModel 属性创建配套的 Messages 结构体,以管理 @LocalizedString 属性。
  5. 最后,生成 YAML 本地化文件,将技术键映射为人类可读的标题和错误消息。

FOSMVVM 字段生成器 配置指南

要有效使用此技能,请确保您的项目结构遵循预期的 FOSMVVM 布局。在您的 AI 代理设置中配置目标路径:

# 示例项目结构设置
mkdir -p Sources/{ViewModelsTarget}/FieldModels
mkdir -p Sources/{ResourcesPath}/FieldModels

确保您的共享 ViewModels 目标被正确引用,以便 Openclaw Skills 可以将生成的 Swift 和 YAML 文件放置在适当的目录中。

FOSMVVM 字段生成器 数据架构与分类体系

生成器产生三个一组的文件来维护表单规范:

文件类型 用途 扩展名
字段协议 定义属性、FormField 和校验逻辑 .swift
消息结构体 将本地化字符串桥接到校验系统 .swift
本地化 YAML 包含标题和错误的实际字符串值 .yml

数据按 {Name}Fields 组织,其中属性映射到具有关联 ValidationResult 方法的 FormField 类型。

name: fosmvvm-fields-generator
description: Generate FOSMVVM Fields protocols with validation rules, FormField definitions, and localized messages. Define form contracts once, validate everywhere.
homepage: https://github.com/foscomputerservices/FOSUtilities
metadata: {"clawdbot": {"emoji": "??", "os": ["darwin", "linux"]}}

FOSMVVM Fields Generator

Generate Form Specifications following FOSMVVM patterns.

Conceptual Foundation

For full architecture context, see FOSMVVMArchitecture.md | OpenClaw reference

A Form Specification (implemented as a {Name}Fields protocol) is the single source of truth for user input. It answers:

  1. What data can the user provide? (properties)
  2. How should it be presented? (FormField with type, keyboard, autofill semantics)
  3. What constraints apply? (validation rules)
  4. What messages should be shown? (localized titles, placeholders, errors)

Why This Matters

The Form Specification is defined once, used everywhere:

// Same protocol adopted by different consumers:
struct CreateIdeaRequestBody: ServerRequestBody, IdeaFields { ... }  // HTTP transmission
@ViewModel struct IdeaFormViewModel: IdeaFields { ... }              // Form rendering
final class Idea: Model, IdeaFields { ... }                          // Persistence validation

This ensures:

  • Consistent validation - Same rules on client and server
  • Shared localization - One YAML file, used everywhere
  • Single source of truth - Change once, applies everywhere

Connection to FOSMVVM

Form Specifications integrate with:

  • Localization System - FormField titles/placeholders and validation messages use LocalizableString
  • Validation System - Implements ValidatableModel protocol
  • Request System - RequestBody types adopt Fields for validated transmission
  • ViewModel System - ViewModels adopt Fields for form rendering

When to Use This Skill

  • Defining a new form (create, edit, filter, search)
  • Adding validation to a request body
  • Any type that needs to conform to ValidatableModel
  • When fosmvvm-fluent-datamodel-generator needs form fields for a DataModel

What This Skill Generates

A complete Form Specification consists of 3 files:

File Purpose
{Name}Fields.swift Protocol + FormField definitions + validation methods
{Name}FieldsMessages.swift @FieldValidationModel struct with @LocalizedString properties
{Name}FieldsMessages.yml YAML localization (titles, placeholders, error messages)

Project Structure Configuration

Replace placeholders with your project's actual paths:

Placeholder Description Example
{ViewModelsTarget} Shared ViewModels SPM target ViewModels, SharedViewModels
{ResourcesPath} Localization resources path Sources/Resources

Expected Structure:

Sources/
  {ViewModelsTarget}/
    FieldModels/
      {Name}Fields.swift
      {Name}FieldsMessages.swift
  {ResourcesPath}/
    FieldModels/
      {Name}FieldsMessages.yml

How to Use This Skill

Invocation: /fosmvvm-fields-generator

Prerequisites:

  • Form purpose understood from conversation context
  • Field requirements discussed (names, types, constraints)
  • Entity relationship identified (what is this form creating/editing)

Workflow integration: This skill is used when defining form validation and user input contracts. The skill references conversation context automatically—no file paths or Q&A needed. Often precedes fosmvvm-fluent-datamodel-generator for form-backed models.

Pattern Implementation

This skill references conversation context to determine Fields protocol structure:

Form Analysis

From conversation context, the skill identifies:

  • Form purpose (create, edit, filter, login, settings)
  • Entity relation (User, Idea, Document - what's being created/edited)
  • Protocol naming (CreateIdeaFields, UpdateProfile, LoginCredentials)

Field Design

For each field from requirements:

  • Property specification (name, type, optional vs required)
  • Presentation type (FormFieldType: text, textArea, select, checkbox)
  • Input semantics (FormInputType: email, password, tel, date)
  • Constraints (required, length range, value range, date range)
  • Localization (title, placeholder, validation error messages)

File Generation Order

  1. Fields protocol with FormField definitions and validation
  2. FieldsMessages struct with @LocalizedString properties
  3. FieldsMessages YAML with localized strings

Context Sources

Skill references information from:

  • Prior conversation: Form requirements, field specifications discussed
  • Specification files: If Claude has read form specs into context
  • Existing patterns: From codebase analysis of similar Fields protocols

Key Patterns

Protocol Structure

public protocol {Name}Fields: ValidatableModel, Codable, Sendable {
    var fieldName: FieldType { get set }
    var {name}ValidationMessages: {Name}FieldsMessages { get }
}

FormField Definition

static var contentField: FormField { .init(
    fieldId: .init(id: "content"),
    title: .localized(for: {Name}FieldsMessages.self, propertyName: "content", messageKey: "title"),
    placeholder: .localized(for: {Name}FieldsMessages.self, propertyName: "content", messageKey: "placeholder"),
    type: .textArea(inputType: .text),
    options: [
        .required(value: true)
    ] + FormInputOption.rangeLength(contentRange)
) }

FormField Types Reference

FormFieldType Use Case
.text(inputType:) Single-line input
.textArea(inputType:) Multi-line input
.checkbox Boolean toggle
.select Dropdown selection
.colorPicker Color selection

FormInputType Reference (common ones)

FormInputType Keyboard/Autofill
.text Default keyboard
.emailAddress Email keyboard, email autofill
.password Secure entry
.tel Phone keyboard
.url URL keyboard
.date, .datetimeLocal Date picker
.givenName, .familyName Name autofill

Validation Method Pattern

internal func validateContent(_ fields: [FormFieldBase]?) -> [ValidationResult]? {
    guard fields == nil || (fields?.contains(Self.contentField) == true) else {
        return nil
    }

    var result = [ValidationResult]()

    if content.isEmpty {
        result.append(.init(
            status: .error,
            field: Self.contentField,
            message: {name}ValidationMessages.contentRequiredMessage
        ))
    } else if !Self.contentRange.contains(NSString(string: content).length) {
        result.append(.init(
            status: .error,
            field: Self.contentField,
            message: {name}ValidationMessages.contentOutOfRangeMessage
        ))
    }

    return result.isEmpty ? nil : result
}

Messages Struct Pattern

@FieldValidationModel public struct {Name}FieldsMessages {
    @LocalizedString("content", messageGroup: "validationMessages", messageKey: "required")
    public var contentRequiredMessage

    @LocalizedString("content", messageGroup: "validationMessages", messageKey: "outOfRange")
    public var contentOutOfRangeMessage
}

YAML Structure

en:
  {Name}FieldsMessages:
    content:
      title: "Content"
      placeholder: "Enter your content..."
      validationMessages:
        required: "Content is required"
        outOfRange: "Content must be between 1 and 10,000 characters"

Naming Conventions

Concept Convention Example
Protocol {Name}Fields IdeaFields, CreateIdeaFields
Messages struct {Name}FieldsMessages IdeaFieldsMessages
Messages property {name}ValidationMessages ideaValidationMessages
Field definition {fieldName}Field contentField
Range constant {fieldName}Range contentRange
Validate method validate{FieldName} validateContent
Required message {fieldName}RequiredMessage contentRequiredMessage
OutOfRange message {fieldName}OutOfRangeMessage contentOutOfRangeMessage

See Also

  • FOSMVVMArchitecture.md - Full FOSMVVM architecture reference
  • fosmvvm-viewmodel-generator - For ViewModels that adopt Fields
  • fosmvvm-fluent-datamodel-generator - For Fluent DataModels that implement Fields
  • reference.md - Complete file templates

Version History

Version Date Changes
1.0 2024-12-24 Initial skill
2.0 2024-12-26 Rewritten with conceptual foundation; generalized from Kairos-specific
2.1 2026-01-24 Update to context-aware approach (remove file-parsing/Q&A). Skill references conversation context instead of asking questions or accepting file paths.