Skip to content

StraitJakt is a Kotlin library for performing sanity checks on your data by imposing constraints on it.

License

Notifications You must be signed in to change notification settings

r8vnhill/strait-jakt

Repository files navigation

StraitJakt: Validate Your Data with Precision

License Maven Central

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

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:

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:

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 for more information.

License

StraitJakt is licensed under the BSD 2-Clause License.

About

StraitJakt is a Kotlin library for performing sanity checks on your data by imposing constraints on it.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages