Skip to content

shostko/validator

Repository files navigation

Validator

Foreman Logo

Maven Central API-level License

How to use

Create Validation entity

Simple way

val email by EmailValidator()  

Composition of validators

val username by validators(  
  NotEmptyValidator(),
  LengthOverOrEqualValidator(3), 
)

Your own validator

val username by Validator.predicate(
  predicate = repository::checkIfUsernameAvailable,
  reason = { th ->
    if (th == null) {
      "Username already taken"
    } else {
      th.message ?: "Error checking username on server"
    }
  },
)
val username by Validator.custom(
  block = { value ->
      if (repository.checkIfUsernameAvailable(value)) {
          ValidationResult.valid(value)
      } else {
          ValidationResult.invalid(value, "Username already taken")
      }
  },
  reason = { it.message ?: "Error checking username on server" },
)

Bind Validation to JetpackCompose TextField

@Composable  
private fun SampleTextField(  
  label: String,  
  validation: Validation<String, String>,  
  modifier: Modifier = Modifier,  
) {  
  val value by validation.valueFlow.collectAsState("")  
  val result by validation.resultFlow.collectAsState(ValidationResult.valid(""))  
  OutlinedTextField(  
    value = value,
    onValueChange = validation::onNewValue,
    label = { Text(label) },
    supportingText = { SampleErrorText(result) },
    isError = !result.isValid,
    modifier = modifier,
    singleLine = true,
  )  
}  
  
@Composable  
private fun SampleErrorText(result: ValidationResult<*, *>) {  
  if (result is ValidationResult.Invalid) {  
    val text = when (val reason = result.reason) {  
      is String -> reason  
      is Int -> stringResource(reason)  
      is ValidationException -> reason.reason  
      is Throwable -> reason.message  
      else -> null
      // any other optinal way to get reason from your custom Validator 
    }
    Text(  
      modifier = Modifier.fillMaxWidth(),  
      text = text ?: "Unknown error!",  
      textAlign = TextAlign.Start,  
    )  
  }
}

Integration

The library is available in Maven Central repository:

dependencies {
    implementation 'by.shostko:validator:0.+'
}

You can also use collections of ready-to-use validators:

dependencies {
    implementation 'by.shostko:validator-exceptions:0.+' // result is Exception
    implementation 'by.shostko:validator-strings:0.+'    // result is pure String
    implementation 'by.shostko:validator-android:0.+'    // result is android resource id (Int) 
}

License

Released under the Apache 2.0 license.

Copyright 2019 Sergey Shostko

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.