ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - Marifeta/kvalidator: validator for kotlin json serialization
validator for kotlin json serialization. Contribute to Marifeta/kvalidator development by creating an account on GitHub.
Visit Site

GitHub - Marifeta/kvalidator: validator for kotlin json serialization

GitHub - Marifeta/kvalidator: validator for kotlin json serialization

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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory