Skip to content

coenttb/swift-form-coding

swift-form-coding

CI Development Status

A convenience umbrella package that re-exports all form coding functionality for Swift.

Overview

This package provides a single import for all form data encoding/decoding needs in Swift. It re-exports two independent packages:

Installation

Add this package to your Package.swift:

dependencies: [
    .package(url: "https://github.com/coenttb/swift-form-coding", from: "0.1.0")
]

Then add the product to your target:

.target(
    name: "YourTarget",
    dependencies: [
        .product(name: "FormCoding", package: "swift-form-coding")
    ]
)

Supported Platforms

  • macOS 14.0+
  • iOS 17.0+
  • tvOS 17.0+
  • watchOS 10.0+
  • Swift 6.1+

Usage

import FormCoding

// URL Form Encoding
struct LoginForm: Codable {
    let username: String
    let password: String
}

let encoder = Form.Encoder()
let form = LoginForm(username: "john", password: "secret")
let formData = try encoder.encode(form)
// Result: "username=john&password=secret"

// Multipart File Upload
let imageUpload = Multipart.FileUpload(
    fieldName: "avatar",
    filename: "profile.jpg",
    fileType: .image(.jpeg),
    maxSize: 5 * 1024 * 1024
)

When to Use Each Package

Use URL Form Coding when:

  • Submitting simple form data without files
  • Working with REST APIs that expect application/x-www-form-urlencoded
  • You need PHP/Rails-style bracket notation for arrays

Use Multipart Form Coding when:

  • Uploading files (images, documents, etc.)
  • Mixing file uploads with form fields
  • You need per-field content-type specification

Individual Packages

If you only need one type of form coding, import the specific package instead:

// Just URL form encoding
dependencies: [
    .package(url: "https://github.com/coenttb/swift-url-form-coding", from: "0.1.0")
]

// Just multipart file uploads
dependencies: [
    .package(url: "https://github.com/coenttb/swift-multipart-form-coding", from: "0.1.0")
]

URLRouting Integration

Both underlying packages support optional URLRouting integration via Swift Package Manager traits:

// In your Package.swift
dependencies: [
    .package(
        url: "https://github.com/coenttb/swift-form-coding",
        from: "0.1.0",
        traits: ["URLRouting"]  // Enable URLRouting trait
    )
]

When the URLRouting trait is enabled:

  • Form.Conversion<T> and Multipart.Conversion<T> conform to URLRouting.Conversion
  • URLRouting is re-exported for convenient access
  • Convenience methods like .form(_:) and .multipart(_:) are available

Architecture

This is a minimal umbrella package with no code of its own. It simply re-exports:

  • URLFormCoding - URL-encoded form data support
  • MultipartFormCoding - Multipart form data with file uploads
  • URLRouting - Conditionally exported when URLRouting trait is enabled

The underlying packages are completely independent - they share no dependencies and can be used separately.

Features

URL Form Coding

  • ✅ Codable integration for form data
  • ✅ Multiple array encoding strategies (accumulate, brackets, indexed)
  • ✅ Custom date/data encoding strategies
  • ✅ URLRouting integration
  • ✅ RFC 2388 compliant

Multipart Form Coding

  • ✅ Secure file upload validation
  • ✅ Magic number (file signature) checking
  • ✅ Configurable size limits
  • ✅ Built-in file type support (images, documents, etc.)
  • ✅ RFC 2045/2046/7578 compliant
  • ✅ URLRouting integration

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Related Packages

About

The Swift package for type-safe web form encoding and decoding.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Contributors 2

  •  
  •  

Languages