theabhieye/Swift-Zod(新しいタブで開きます)
主要言語: Swift
A Zod-inspired schema validation library for Swift — type-safe parsing, coercion, defaults, transforms, and refinements.
- star 数
- 6
- watcher 数
- 0
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2026/05/08
主要言語: Swift
A Zod-inspired schema validation library for Swift — type-safe parsing, coercion, defaults, transforms, and refinements.
A super-fast, type-safe, composable validation library for Swift — inspired by Zod.
Swift-Zod is a modern validation library for Swift that makes it easy to parse, validate, and refine untyped data.
It brings the declarative, composable API of JavaScript’s Zod into Swift — ideal for apps, backend services, JSON validation, and request parsing.
parse() and safeParse().refine() for custom validation rules.transform() for output shaping.default() and optional chaining.coerce.* helpers (String, Number, Boolean)decodeAndValidate()Add this to your project's Package.swift:
.dependencies: [ .package(url: "https://github.com/theabhieye/Swift-Zod", from: "1.0.0") ]
Then import it:
import SwiftZod
let UserSchema = Z.object([ "id": Z.string().uuid(), "email": Z.string().email(), "age": Z.number().min(18), "isActive": Z.boolean() ]) let data: [String: Any] = [ "id": "9e1c9832-1ff8-4d1f-9e72-c2a894bb3ab1", "email": "user@example.com", "age": 24, "isActive": true ] let result = try UserSchema.parse(data)
switch UserSchema.safeParse(data) { case .success(let user): print("Valid:", user) case .failure(let error): print("Error:", error.message) }
let Schema = Z.object([ "age": Z.coerce.number().min(18), "active": Z.coerce.boolean() ]) try Schema.parse([ "age": "21", "active": "yes" ])
let PasswordSchema = Z.string() .min(8) .refine("hasUppercase", message: "Must include uppercase") { $0.contains { $0.isUppercase } } .transform { $0.trimmingCharacters(in: .whitespaces) }
struct User: Codable { let id: String let email: String let age: Int } let schema = Z.object([ "id": Z.string().uuid(), "email": Z.string().email(), "age": Z.number().min(18) ]) let user = try User.decodeAndValidate(from: jsonData, schema: schema)
Z.string()Z.number()Z.boolean()Z.array(T)Z.object([:])Z.enum([...])Z.optional()Z.union([...])Z.coerce.string()Z.coerce.number()Z.coerce.boolean()| Modifier | Description |
|---|---|
.min(n) |
Min length / value |
.max(n) |
Max length / value |
.refine(name, message) |
Custom validation |
.transform(fn) |
Map output |
.default(value) |
Default value when missing |
.optional() |
Makes field nullable |
.email() |
Email validation |
.uuid() |
UUID format |
.url() |
URL validation |
.phone() |
Phone number format |
.alphanumeric() |
Alphanumeric string |
Sources/
Swift-Zod/
Core/
Types/
Utils/
Tests/
Swift-ZodTests/
Run all tests:
swift test --parallelGenerate coverage:
swift test --enable-code-coveragePull requests are welcome! Before submitting, ensure:
Licensed under the MIT License.
See LICENSE for details.
Abhishek Kapoor
📧 Email: abhikapoor2000.ak@gmail.com 🔗 LinkedIn: https://www.linkedin.com/in/abhishekkapoorfullstack/
If you like this package, star the repo — it helps a lot!