https://github.com/r8vnhill/strait-jakt
StraitJakt is a Kotlin library for performing sanity checks on your data by imposing constraints on it.
https://github.com/r8vnhill/strait-jakt
constraints exceptions kotlin sanity-checks
Last synced: 7 months ago
JSON representation
StraitJakt is a Kotlin library for performing sanity checks on your data by imposing constraints on it.
- Host: GitHub
- URL: https://github.com/r8vnhill/strait-jakt
- Owner: r8vnhill
- License: bsd-2-clause
- Created: 2023-11-05T19:11:49.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-30T14:57:24.000Z (12 months ago)
- Last Synced: 2024-10-30T15:47:45.729Z (12 months ago)
- Topics: constraints, exceptions, kotlin, sanity-checks
- Language: Kotlin
- Homepage:
- Size: 864 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# StraitJakt: Validate Your Data with Precision
[](https://opensource.org/licenses/BSD-2-Clause)
[](https://search.maven.org/artifact/cl.ravenhill/strait-jakt)**StraitJakt** is a versatile Kotlin library designed to apply precise constraints to your data, ensuring it remains consistent and valid throughout your application.
## Features
- **Type-Safe Constraints**: Enforce rules on your data using Kotlin's powerful type system.
- **Declarative Syntax**: Easily define validation rules in a readable and expressive manner.
- **Comprehensive Validation**: Cover a wide array of data validation scenarios from simple to complex.
- **Customizable**: Extend or create custom constraints tailored to your needs.## Getting Started
### Installation
```kotlin
repositories {
mavenCentral()
}dependencies {
implementation("cl.ravenhill:strait-jakt:1.4.0") // Replace with latest version
}
```### Usage Example
Validate user registration data with StraitJakt by defining constraints within a `constraints` block:
```kotlin
fun registerUser(username: String, name: String, age: Int, password: String) {
constraints {
"Username should only contain alphanumeric characters" {
username must BeAlphanumeric
}
"Username should be between 5 to 20 characters in length." {
username must HaveLength { it in 5..20 }
}
"Username can only have underscores or hyphens as special characters" {
username must Match(Regex("^[a-zA-Z0-9_-]*$"))
}
"Name field cannot be left blank" {
name mustNot BeEmpty
}
"Users should be 18 years of age or older" {
age must BeAtLeast(18)
}
"Password needs to be a minimum of 8 characters" {
password must HaveLength { it >= 8 }
}
"Password should include at least one uppercase letter" {
password must Contain(Regex("[A-Z]"))
}
"Password should include at least one lowercase letter" {
password must Contain(Regex("[a-z]"))
}
"Password should include at least one digit" {
password must Contain(Regex("[0-9]"))
}
}
// Further implementation details...
}
```Here's how you can handle validation scenarios:
```kotlin
try {
registerUser("johndoe", "John Doe", 17, "password")
} catch (e: CompositeException) {
println(e.message)
// Handle multiple validation failures
}
```### Exception Handling
StraitJakt throws a `CompositeException` when multiple constraints fail, allowing you to handle exceptions in a granular and informative way.
## Contributing
We welcome contributions! Please see our [Contribution Guidelines](CONTRIBUTING.md) for more information.
## License
StraitJakt is licensed under the [BSD 2-Clause License](LICENSE).