Harmonize

🎤 Harmonize is coming to a conference near you! We'll be presenting it at NSSpain (Logroño, September 16–18, 2026) and Swift Connection (Paris, November 2–3, 2026). Come say hi!
Harmonize is a modern linter for Swift that allows you to assert, validate, and harmonize your code’s structure and architecture by writing lint rules as unit tests—using Quick, XCTest, or Swift Testing.
This allows your team to keep your codebase clean, maintainable, and consistent as it grows, without relying on manual code reviews.
Harmonize aims to solve the limitations of regex-based linters such as SwiftLint, which focus primarily on Swift style and simple conventions. Inspired by Konsist for Kotlin and ArchUnit for Java, Harmonize provides a richer, semantic way to enforce your project's architecture and structural guidelines.
Architectural linters in the era of AI-generated code
AI-generated code can help teams move faster, but it can also introduce architectural flaws and subtle bugs that are hard to spot in manual code reviews. Harmonize gives your codebase deterministic guardrails by turning your team’s architectural and structural rules into unit tests. When AI-generated code violates those rules, the tests fail, giving your AI agent clear feedback to fix its own mistakes.
Harmonize can also provide the rules to AI agents before agents write any code. See Integrate Harmonize with AI agents.
Usage
With Harmonize, you can write a lint rule similarly as you would write a unit test:
Example using Quick:
import Harmonize
import Quick
final class ViewModelsInheritBaseViewModelSpec: QuickSpec {
override func spec() {
describe("ViewModels") {
let viewModels = Harmonize.productionCode().classes()
.withNameEndingWith("ViewModel")
it("should inherit from BaseViewModel") {
viewModels.assertTrue(rule: Self.rule) {
$0.inherits(from: "BaseViewModel")
}
}
}
}
private static let rule = Rule(
title: "ViewModels must inherit from BaseViewModel.",
category: "viewmodel",
rationale: "BaseViewModel owns the lifecycle and the error handling.",
howToFix: "Declare the class as `final class MyViewModel: BaseViewModel`.",
badExample: "final class MyViewModel: ObservableObject { }",
goodExample: "final class MyViewModel: BaseViewModel { }"
)
}
Example using XCTest:
import Harmonize
import XCTest
final class ViewModelsInheritBaseViewModelSpec: XCTestCase {
func testViewModels() throws {
let viewModels = Harmonize.productionCode().classes()
.withNameEndingWith("ViewModel")
viewModels.assertTrue(rule: Self.rule) {
$0.inherits(from: "BaseViewModel")
}
}
private static let rule = Rule(/* the same fields as in the Quick example */)
}
Example using Swift Testing:
import Harmonize
import Testing
private let rule = Rule(/* the same fields as in the Quick example */)
@Test
func viewModelsInheritBaseViewModel() {
let viewModels = Harmonize.productionCode().classes()
.withNameEndingWith("ViewModel")
viewModels.assertTrue(rule: rule) {
$0.inherits(from: "BaseViewModel")
}
}
This lint rule enforces all ViewModels to inherit from BaseViewModel. Since it runs as a unit test, it will fail once it detects a violation. The failure shows the Rule metadata: the title, the rationale, the fix and the two examples (see What a failure shows). To exclude a declaration from a rule, use the withoutName function:
let viewModels = Harmonize.productionCode().classes()
.withNameEndingWith("ViewModel")
.withoutName(["LegacyViewModel"])
To accept known violations, pass their names to the baseline: parameter of the assertion. Harmonize reports an error when a baseline entry passes the rule. This prevents stale entries:
viewModels.assertTrue(rule: Self.rule, baseline: ["LegacyViewModel"]) {
$0.inherits(from: "BaseViewModel")
}
You can create similar rules for any architectural or structural pattern that you want to enforce.