A convenient way to define JSON Schema in Swift.
JSONSchema
provides a Swift-native way to define JSON Schema definitions programmatically. This package leverages Swift's type system to create clear, concise, and type-safe JSON Schema definitions.
You can add JSONSchema
as a dependency to your project using Swift Package Manager by adding it to the dependencies value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/kevinhermawan/swift-json-schema.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
/// ...
dependencies: [.product(name: "JSONSchema", package: "swift-json-schema")])
]
Alternatively, in Xcode:
- Open your project in Xcode.
- Click on
File
->Swift Packages
->Add Package Dependency...
- Enter the repository URL:
https://github.com/kevinhermawan/swift-json-schema.git
- Choose the version you want to add. You probably want to add the latest version.
- Click
Add Package
.
You can find the documentation here: https://kevinhermawan.github.io/swift-json-schema/documentation/jsonschema
import JSONSchema
let emailSchema = JSONSchema.string(
description: "User's email address",
pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
)
let priceSchema = JSONSchema.number(
description: "Product price",
minimum: 0.01,
exclusiveMaximum: 1000000
)
let ageSchema = JSONSchema.integer(
description: "User's age",
minimum: 0,
maximum: 120
)
let statusSchema = JSONSchema.enum(
description: "User's status",
values: [
.string("active"),
.string("inactive"),
.string("pending")
]
)
let userSchema = JSONSchema.object(
description: "User object",
properties: [
"id": .integer(minimum: 1),
"name": .string(minLength: 1),
"email": emailSchema,
"age": ageSchema,
"status": statusSchema
],
required: ["id", "name", "email", "status"]
)
let tagsSchema = JSONSchema.array(
description: "User's tags",
items: .string(minLength: 1),
minItems: 1,
maxItems: 10,
uniqueItems: true
)
For more complex schemas, you can nest schemas within each other:
let productSchema = JSONSchema.object(
properties: [
"id": .integer(minimum: 1),
"name": .string(minLength: 1, maxLength: 100),
"price": priceSchema,
"tags": .array(
items: .string(),
uniqueItems: true
),
"settings": .object(
properties: [
"inStock": .boolean(),
"size": .enum(values: [.string("small"), .string("medium"), .string("large")])
]
)
],
required: ["id", "name", "price"]
)
You can also create the same schema using a JSON string:
do {
let jsonString = """
{
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"price": {
"type": "number",
"minimum": 0.01,
"exclusiveMaximum": 1000000
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
},
"settings": {
"type": "object",
"properties": {
"inStock": {
"type": "boolean"
},
"size": {
"type": "string",
"enum": ["small", "medium", "large"]
}
}
}
},
"required": ["id", "name", "price"]
}
"""
let productSchema = try JSONSchema(jsonString: jsonString)
} catch {
print(String(describing: error))
}
If you find JSONSchema
helpful and would like to support its development, consider making a donation. Your contribution helps maintain the project and develop new features.
Your support is greatly appreciated! ❤️
Contributions are welcome! Please open an issue or submit a pull request if you have any suggestions or improvements.
This repository is available under the Apache License 2.0.