validator for kotlin json serialization

Overview

kvalidator

Why use kvalidator?

  • Readable and declarative validation rules.
  • Error messages with multilingual support.

Support platform:

  • JVM
  • Android

Installation

Required dependencies 
- kotlin 1.4
- kotlinx.serialization: min 1.0.0-RC

repositories {
    jcenter()
}

dependencies {
    implementation("com.github.marifeta:kvalidator:1.0.0")
}

Usage

Validate

val testJson = JsonObject(mapOf(
        "name" to JsonPrimitive("John"),
        "age" to JsonPrimitive(28),
        "isHuman" to JsonPrimitive(true),
))

val rules = mapOf<String, List<Rule>>(
        "name" to listOf(Alpha(), Required()),
        "age" to listOf(IsInteger(), Between(25, 48)),
        "isHuman" to listOf(IsBoolean()),
)

val validator = Validator(testJson, rules, /* lang = en */)
validator.validate() // true 

// or
if(!validator.validate()) {
    validator.errors.forEach {(attribute, messages)->
        messages.forEach { msg ->
            println("Error: $attribute has error by $msg")
        }
    }
}

Errors

val firstError = validator.errors.first() // firstError or null
val validator.errors // mutableListOf(reason) or null
val errorCount = validator.errors.count // errors.size (int)

Utils

parseRule("max:255"): Rule
parseRules("required|max:255"): List<Rule>
stringifyRules("required|email|max:15|min:8|size:8|boolean")
stringifyRule("max:15")

Available Rules

Alpha

The field under validation must be entirely alphabetic characters.

AlphaNum

The field under validation must be entirely alpha-numeric characters.

AlphaDash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

IsInteger

The field under validation must have an integer value.

IsString

The field under validation must be a string.

IsBoolean

The field under validation must be a boolean value of the form true, false (Not String).

IsArray

The field under validation must be an array.

IsNumeric

Validate that an attribute is numeric. The string representation of a number will pass.

IsDate

The field under validation must be a valid date format which is acceptable by Java's LocalDate object in format "yyyy-mm-ddThh:mm:ss".

AfterDate

The field under validation must be after the given date.

Required

Checks if the length of the String representation of the value is >

Confirmed

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

Accepted

The field under validation must be yes, on, 1 or true. This is useful for validating "Terms of Service" acceptance.

Between

The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.

Min

Validate that an attribute is at least a given size.

Max

Validate that an attribute is no greater than a given size

Size

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value.

Url

Validate that an attribute has a valid URL format

Email

The field under validation must be formatted as an e-mail address.

Links

License

MIT

You might also like...
Markdown renderer for Kotlin Compose Multiplatform (Android, Desktop)

Markdown renderer for Kotlin Compose Multiplatform (Android, Desktop)

Dagger Hilt, MVP Moxy, Retrofit, Kotlin coroutine, Sealed class

Dagger Hilt, MVP Moxy, Retrofit, Kotlin coroutine, Sealed class

Item Helper For Kotlin

Item Helper For Kotlin

kotlin mvvm+dataBinding+retrofit2+Arouter等BaseActivity、BaseFragment、BaseDialogFragment基类封装
kotlin mvvm+dataBinding+retrofit2+Arouter等BaseActivity、BaseFragment、BaseDialogFragment基类封装

kotlin-mvvm kotlin mvvm+dataBinding+retrofit2+ARouter等BaseActivity、BaseFragment、BaseDialogFragment基类封装 Android开发项目基本使用框架,封装了各类组件,在基类实现了沉浸式状态栏,可以自己更改颜色

 Bar Service Kotlin Client
Bar Service Kotlin Client

A simple starter service client written in Kotlin against generated models (protos)A simple starter service client written in Kotlin against generated models (protos)

Kotlin validation with a focus on readability
Kotlin validation with a focus on readability

kommodus Kotlin validation with a focus on readability import com.github.kommodus.constraints.* import com.github.kommodus.Validation data class Test

Kotlin to Dart compiler
Kotlin to Dart compiler

Dotlin is a Kotlin to Dart compiler. The aim is to integrate Kotlin as a language into the Dart ecosystem, combing best of both worlds: The Kotlin lan

Modern Kotlin version of com.example.semitop7.FireTVStyle keyboard

ftv-style-keyboard Modern Kotlin version of com.example.semitop7.FireTVStyle keyboard Manual activation on FireTV via adb shell: adb shell ime enable

A Kotlin-based testing/scraping/parsing library providing the ability to analyze and extract data from HTML
A Kotlin-based testing/scraping/parsing library providing the ability to analyze and extract data from HTML

A Kotlin-based testing/scraping/parsing library providing the ability to analyze and extract data from HTML (server & client-side rendered). It places particular emphasis on ease of use and a high level of readability by providing an intuitive DSL. It aims to be a testing lib, but can also be used to scrape websites in a convenient fashion.

Owner
marifeta
marifeta
📃 Turn Google Spreadsheet to JSON endpoint (for Android and JVM) for FREE (100%)

retrosheet ?? Turn Google Spreadsheet to JSON endpoint. [For Android and JVM]. Benefits ?? No worries about server health (because you're using Google

Sifar 687 Jan 4, 2023
Remove the dependency of compiled kotlin on kotlin-stdlib

Dekotlinify This project aims to take compiled Kotlin Java bytecode (compiled by the standard Kotlin compiler) and remove all references to the Kotlin

Joseph Burton 10 Nov 29, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
A DSL to handle soft keyboard visibility change event written in Kotlin.

About A DSL to handle soft keyboard visibility change event written in Kotlin. How to use? Step 1. Add it in your root build.gradle at the end of repo

Vinícius Oliveira 17 Jan 7, 2023
Kotlin matrix class which supports determinant, inverse matrix, matmul, etc.

Kotrix is a set of classes that helps people dealing with linear algebra in Kotlin.

Kanguk Lee 5 Dec 8, 2022
Fuzzy string matching for Kotlin (JVM, native, JS, Web Assembly) - port of Fuzzy Wuzzy Python lib

FuzzyWuzzy-Kotlin Fuzzy string matching for Kotlin (JVM, iOS) - fork of the Java fork of of Fuzzy Wuzzy Python lib. For use in on JVM, Android, or Kot

WillowTree, LLC 54 Nov 8, 2022
🐫🐍🍢🅿 Multiplatform Kotlin library to convert strings between various case formats including Camel Case, Snake Case, Pascal Case and Kebab Case

KaseChange Multiplatform Kotlin library to convert strings between various case formats Supported Case Formats SCREAMING_SNAKE_CASE snake_case PascalC

PearX Team 67 Dec 30, 2022
Multiplaform kotlin library for calculating text differences. Based on java-diff-utils, supports JVM, JS and native targets.

kotlin-multiplatform-diff This is a port of java-diff-utils to kotlin with multiplatform support. All credit for the implementation goes to original a

Peter Trifanov 51 Jan 3, 2023
Multi-module, Kotlin, MVI, Compose, Hilt, Navigation Component, Use-cases, Room, Retrofit

Work in progress Multi-module demo app that gets data from dota2 api. API https://docs.opendota.com/ Players by rank (GET) https://api.opendota.com/ap

Mitch Tabian 321 Dec 27, 2022
recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.

recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.

Sebastian Kaspari 565 Jan 2, 2023