From 05b8a1670f02480791fd7f7f46ea7dc74f6e70ef Mon Sep 17 00:00:00 2001 From: yveskalume Date: Tue, 25 Jun 2024 23:32:10 +0300 Subject: [PATCH] explain how to create a custom validable --- README.md | 4 +--- docs/create_validable.md | 21 +++++++++++++++++++++ docs/index.md | 4 +--- mkdocs.yml | 1 + 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 docs/create_validable.md diff --git a/README.md b/README.md index 3daff75..ed35634 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,7 @@ ![Maven Central Version](https://img.shields.io/maven-central/v/tech.devscast/validable?color=blue) [![build](https://github.com/devscast/validable/actions/workflows/build.yaml/badge.svg)](https://github.com/devscast/validable/actions/workflows/build.yaml) -Validating text fields when using jetpack compose can sometimes be challenging and verbose. - -Validable is an extensible library that allows you to validate your text fields in a simpler way while having a reusable code. +Validable is an extensible library that simplifies text field validation for Jetpack Compose by providing abstraction and reusable validation logic. This is what it looks like : diff --git a/docs/create_validable.md b/docs/create_validable.md new file mode 100644 index 0000000..7de916f --- /dev/null +++ b/docs/create_validable.md @@ -0,0 +1,21 @@ +# Create a validable + +You can create a validable with a custom validation logic. + +All you need to do is to extend the `BaseValidable` class, which takes two functions as parameters: + +- `validator`: This function checks if the value meets a specified condition and returns a boolean. +- `errorFor`: Is called when the value does not meet the condition and returns a string as the error message. + +Let's see an example of a custom validable that checks if an input contains the word "devscast" + +```kotlin +class DevscastValidable : BaseValidable( + validator = { value -> + value.contains("devscast", ignoreCase = true) + }, + errorFor = { value -> + "$value does not contain the word 'devscast'." + } +) +``` \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 51d8e9d..12760c5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -3,9 +3,7 @@ ![Maven Central Version](https://img.shields.io/maven-central/v/tech.devscast/validable?color=blue) [![build](https://github.com/devscast/validable/actions/workflows/build.yaml/badge.svg)](https://github.com/devscast/validable/actions/workflows/build.yaml) -Validating text fields when using jetpack compose can sometimes be challenging and verbose. - -Validable is an extensible library that allows you to validate your text fields in a simpler way while having a reusable code. +Validable is an extensible library that simplifies text field validation for Jetpack Compose by providing abstraction and reusable validation logic. ```kotlin @Composable diff --git a/mkdocs.yml b/mkdocs.yml index 0014765..9ea3f8d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -62,6 +62,7 @@ markdown_extensions: nav: - 'Get Started': index.md + - 'Create a validable': create_validable.md - 'Built in validables': - 'CardSchemeValidable' : validables/cardscheme.md - 'Email' : validables/email.md