ZodKmp: Kotlin Multiplatform Validation¶
ZodKmp is a Kotlin Multiplatform implementation of the popular Zod TypeScript validation library. It provides a declarative, type-safe way to validate data in your Kotlin Multiplatform projects.
Platform Support¶
ZodKmp supports the following platforms:
- Android (JVM)
- iOS (Native)
- JVM
- JS (JavaScript)
- Native (Linux, Windows, macOS)
Features¶
- ✅ Declarative Schema Definition - Define validation rules upfront
- ✅ Type Inference - Automatic type inference from schemas
- ✅ Immutable Architecture - Immutable schemas that return new instances
- ✅ Kotlin Multiplatform - Works on Android, iOS, and other Kotlin targets
- ✅ Comprehensive API - Supports all major Zod validation features
- ✅ Extensible - Easy to extend with custom validations
- ✅ Zero Dependencies - Lightweight with minimal footprint
- ✅ Excellent Error Messages - Detailed, customizable error reporting
Getting Started¶
Installation¶
Gradle¶
Add the following to your build.gradle.kts
:
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.piashcse:zodkmp:1.2.0")
}
}
}
}
Version Catalog (libs.versions.toml)¶
[versions]
zodkmp = "1.2.0"
[libraries]
zodkmp = { module = "io.github.piashcse:zodkmp", version.ref = "zodkmp" }
Basic Usage¶
ZodKmp allows you to define validation schemas and use them to validate data:
import com.piashcse.zodkmp.Zod
// Define a schema
val userSchema = Zod.objectSchema<User>({
string("name", Zod.string().min(2))
string("email", Zod.string().email())
number("age", Zod.number().min(0).max(120))
}) { map ->
User(
name = map["name"] as String,
email = map["email"] as String,
age = (map["age"] as Number).toDouble()
)
}
// Use the schema
val userData = mapOf(
"name" to "John Doe",
"email" to "john@example.com",
"age" to 30.0
)
val result = userSchema.safeParse(userData)
when (result) {
is ZodResult.Success -> println("Valid user: ${result.data}")
is ZodResult.Failure -> println("Validation errors: ${result.error.errors}")
}
Contributing¶
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
👨 Developed By¶
Mehedi Hassan Piash
License¶
MIT License
Copyright (c) 2025 Mehedi Hassan Piash
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.