All Projects → konform-kt → Konform

konform-kt / Konform

Licence: mit
Portable validations for Kotlin

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Konform

magic-bytes
A library for detecting file types.
Stars: ✭ 20 (-93.29%)
Mutual labels:  validation
Async Validate
Asynchronous type validation for node and the browser
Stars: ✭ 257 (-13.76%)
Mutual labels:  validation
Laravel Postal Code Validation
Worldwide postal code validation for Laravel and Lumen
Stars: ✭ 278 (-6.71%)
Mutual labels:  validation
ngx-messages
Angular directives for displaying validation messages similar to these from AngularJs
Stars: ✭ 17 (-94.3%)
Mutual labels:  validation
Email inquire
Validate email for common typos and one-time email providers
Stars: ✭ 257 (-13.76%)
Mutual labels:  validation
Validate
Professional data validation for the R environment
Stars: ✭ 268 (-10.07%)
Mutual labels:  validation
js-form-validator
Javascript form validation. Pure JS. No jQuery
Stars: ✭ 38 (-87.25%)
Mutual labels:  validation
Ow
Function argument validation for humans
Stars: ✭ 3,415 (+1045.97%)
Mutual labels:  validation
Creditcard.js
A simple credit cards validation library in JavaScript
Stars: ✭ 259 (-13.09%)
Mutual labels:  validation
Forms
📝 Generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.
Stars: ✭ 272 (-8.72%)
Mutual labels:  validation
FluentValidation.Extensions.Br
An extension of the fluent validation with a set of Brazilian validations
Stars: ✭ 23 (-92.28%)
Mutual labels:  validation
Pt Br Validator
Uma biblioteca contendo validações de formatos Brasileiros, para o Laravel
Stars: ✭ 255 (-14.43%)
Mutual labels:  validation
Valiktor
Valiktor is a type-safe, powerful and extensible fluent DSL to validate objects in Kotlin
Stars: ✭ 267 (-10.4%)
Mutual labels:  validation
valid-data-url
Detect if a string is a data URL
Stars: ✭ 17 (-94.3%)
Mutual labels:  validation
Structure
A simple schema/attributes library built on top of modern JavaScript
Stars: ✭ 292 (-2.01%)
Mutual labels:  validation
validate
Modern lightweight library without dependencies for the data validation from single input tag
Stars: ✭ 24 (-91.95%)
Mutual labels:  validation
Vscode Stylelint
A Visual Studio Code extension to lint CSS/SCSS/Less with stylelint
Stars: ✭ 260 (-12.75%)
Mutual labels:  validation
Validate
A simple jQuery plugin to validate forms.
Stars: ✭ 298 (+0%)
Mutual labels:  validation
Hamsters
A mini Scala utility library
Stars: ✭ 292 (-2.01%)
Mutual labels:  validation
Fluentresults
A generalised Result object implementation for .NET/C#
Stars: ✭ 266 (-10.74%)
Mutual labels:  validation

Build Status Bintray Maven Central

Portable validations for Kotlin

  • ✅ Type-safe DSL
  • 🔗 Multi-platform support (JVM, JS)
  • 🐥 Zero dependencies

Installation

Add the konform bintray repository to your build.gradle.kts

repositories {
    maven("https://dl.bintray.com/konform-kt/konform")
}

Depending on your type of Kotlin project add one of these dependencies:

  • JVM:
    implementation("io.konform:konform-jvm:0.2.0")
  • JS:
    implementation("io.konform:konform-js:0.2.0")
  • MultiPlatform/commonMain:
    implementation("io.konform:konform:0.2.0")

Use

Suppose you have a data class like this:

data class UserProfile(
    val fullName: String,
    val age: Int?
)

Using the Konform type-safe DSL you can quickly write up a validation

val validateUser = Validation<UserProfile> {
    UserProfile::fullName {
        minLength(2)
        maxLength(100)
    }

    UserProfile::age ifPresent {
        minimum(0)
        maximum(150)
    }
}

and apply it to your data

val invalidUser = UserProfile("A", -1)
val validationResult = validateUser(invalidUser)

since the validation fails the validationResult will be of type Invalid and you can get a list of validation errors by indexed access:

validationResult[UserProfile::fullName]
// yields listOf("must have at least 2 characters")

validationResult[UserProfile::age]
// yields listOf("must be at least '0'")

or you can get all validation errors with details as a list:

validationResult.errors
// yields listOf(
//     ValidationError(dataPath=.fullName, message=must have at least 2 characters),
//     ValidationError(dataPath=.age, message=must be at least '0'
// )

In case the validation went through successfully you get a result of type Valid with the validated value in the value field.

val validUser = UserProfile("Alice", 25)
val validationResult = validateUser(validUser)
// yields Valid(UserProfile("Alice", 25))

Advanced use

You can define validations for nested classes and use them for new validations

val ageCheck = Validation<UserProfile> {
    UserProfile::age required {
        minimum(18)
    }
}

val validateUser = Validation<UserProfile> {
    UserProfile::fullName {
        minLength(2)
        maxLength(100)
    }
    
    run(ageCheck)
}

It is also possible to validate nested data classes and properties that are collections (List, Map, etc...)

data class Person(val name: String, val email: String?, val age: Int)

data class Event(
    val organizer: Person,
    val attendees: List<Person>,
    val ticketPrices: Map<String, Double?>
)

val validateEvent = Validation<Event> {
    Event::organizer {
        // even though the email is nullable you can force it to be set in the validation
        Person::email required {
            pattern("[email protected]") hint "Organizers must have a BigCorp email address"
        }
    }

    // validation on the attendees list
    Event::attendees {
        maxItems(100)
    }

    // validation on individual attendees
    Event::attendees onEach {
        Person::name {
            minLength(2)
        }
        Person::age {
            minimum(18) hint "Attendees must be 18 years or older"
        }
        // Email is optional but if it is set it must be valid
        Person::email ifPresent {
            pattern("[email protected]+\..+") hint "Please provide a valid email address (optional)"
        }
    }

    // validation on the ticketPrices Map as a whole
    Event::ticketPrices {
        minItems(1) hint "Provide at least one ticket price"
    }

    // validations for the individual entries
    Event::ticketPrices onEach {
        // Tickets may be free in which case they are null
        Entry<String, Double?>::value ifPresent {
            minimum(0.01)
        }
    }
}

Errors in the ValidationResult can also be accessed using the index access method. In case of Iterables and Arrays you use the numerical index and in case of Maps you use the key as string.

// get the error messages for the first attendees age if any
result[Event::attendees, 0, Person::age]

// get the error messages for the free ticket if any
result[Event::ticketPrices, "free"]

Other validation libraries written in Kotlin

Integration with testing libraries

  • Kotest provides various matchers for use with Konform. They can be used in your tests to assert that a given object is validated successfully or fails validation with specific error messages. See usage documentation here.
Author

Niklas Lochschmidt

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].