Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoepelman committed Oct 31, 2024
1 parent 0a5275e commit 1f694be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,38 @@ result[Event::attendees, 0, Person::age]
result[Event::ticketPrices, "free"]
```

#### Subtypes

You can run validations only if the valuen is of a specific subtype, or require it to be specific subtype.

```kotlin
sealed interface Animal {
val name: String
}
data class Cat(override val name: String, val favoritePrey: String) : Animal
data class Dog(override val name: String) : Animal

val validateAnimal = Validation<Animal> {
Animal::name {
notBlank()
}
// Only run this validation if the current Animal is a Cat and not null
ifInstanceOf<Cat> {
Cat::favoritePrey {
notBlank()
}
}
}
val requireCat = Validation<Animal> {
// This will return an invalid result is the current Animal is not a Cat or null
requireInstanceOf<Cat> {
Cat::favoritePrey {
// ...
}
}
}
```

### Other validation libraries written in Kotlin

- Valikator: https://github.com/valiktor/valiktor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InstanceOfTest {
}

private val ifCatValidation =
Validation<Animal?> {
Validation<Animal> {
ifInstanceOf<Cat> {
run(catValidation)
}
Expand Down

0 comments on commit 1f694be

Please sign in to comment.