GithubHelp home page GithubHelp logo

romainbitard / kaval Goto Github PK

View Code? Open in Web Editor NEW

This project forked from monkeypatchio/kaval

0.0 0.0 0.0 409 KB

A POJO validation DSL in Kotlin

Home Page: https://monkeypatch.io/kaval/

License: Apache License 2.0

Kotlin 100.00%

kaval's Introduction

Kaval Kaval

GitHub Workflow Status GitHub ktlint Awesome Kotlin Badge

This is a Kotlin multi-platform library to validate your model.

Why

The goal is to find the right balance between conciseness, expressiveness, and composability to validate POJO.

Or you can see it as a validation DSL.

Example

We want to validate this model

data class User(
    val firstName: String,
    val lastName: String,
    val address: Address?
)

data class Address(
    val line1: String,
    val line2: String,
    val zipCode: Int,
    val city: String
)

We can validate the Address with these constraints:

  • line1: not blank, and length <= 255
  • line2: length <= 255
  • zipCode: > 0
  • city: not blank
val addressValidator: Validator<Address> =
    reflectValidator {
        Address::line1 { notBlank and maxLength(255) }
        Address::line2 { maxLength(255) }
        Address::zipCode { greaterThan(0) }
        Address::city { notBlank }
    }

And the User with these constraints:

  • firstName: not blank, and length <= 128
  • lastName: length <= 255
  • address: see above
val userValidator: Validator<User> =
    reflectValidator {
        User::firstName { notBlank and maxLength(128) }
        User::lastName { maxLength(255) }
        User::address { nullOr { Address.validator } }
    }

Now we can use the validators:

val user = User(
   firstName = "",
   lastName = "x".repeat(500),
   address = Address(
       line1 = "",
       line2 = "",
       zipCode = -1,
       city = ""
   )
)

val result: ValidationResult = userValidator.validate(user)
println(result)
// Invalid:
//  - [firstName] requires to be not blank
//  - [lastName.length] requires to be lower or equals to 255, got 500
//  - [address.line1] requires to be not blank
//  - [address.zipCode] requires to be greater than 0, got -1
//  - [address.city] requires to be not blank should be Valid

Modules

kaval's People

Contributors

ilaborie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.