Skip to content

Commit

Permalink
Adjust notBlank to accept nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Dec 30, 2023
1 parent 612f4c2 commit b053e0d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sksamuel.tribune.core.strings

import arrow.core.Either
import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.Parser
Expand Down Expand Up @@ -41,16 +42,23 @@ fun <I, E> Parser<I, String?, E>.notNullOrBlank(ifError: () -> E): Parser<I, Str
}

/**
* Wraps an existing String -> String [Parser] to reject blank strings,
* Wraps an existing I -> String? [Parser] to reject blank strings,
* with the error message generated by the given function [ifBlank].
*
* Note: This parser accepts null strings as valid. If you wish to reject null inputs as well,
* then use nullOrNotBlank.
*
* @param ifBlank the error generating function
*
* @return invalid if the input string contains only whitespace, otherwise valid
*/
fun <I, E> Parser<I, String, E>.notBlank(ifBlank: () -> E): Parser<I, String, E> {
fun <I, E> Parser<I, String?, E>.notBlank(ifBlank: () -> E): Parser<I, String?, E> {
return flatMap {
if (it.isBlank()) ifBlank().leftNel() else it.right()
when {
it == null -> Either.Right(null)
it.isBlank() -> ifBlank().leftNel()
else -> it.right()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import arrow.core.leftNel
import arrow.core.right
import com.sksamuel.tribune.core.strings.minlen
import com.sksamuel.tribune.core.strings.notBlank
import com.sksamuel.tribune.core.strings.notNullOrBlank
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

Expand Down Expand Up @@ -82,7 +83,7 @@ class ValidatedTest : FunSpec() {

val nameParser: Parser<UpdateRequest, ValidName, String> = Parser<UpdateRequest>()
.map { it.name }
.notBlank { "Name cannot be blank" }
.notNullOrBlank { "Name cannot be blank" }
.minlen(6) { "Name must have 6 characters" }
.map { ValidName(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ class ZipTest : FunSpec() {
val p2 = Parser.from<String?>().notNullOrBlank { "bar" }
val p = Parser.zip(p1, p2)
p.parse(null) shouldBe nonEmptyListOf("foo", "bar").left()
// p.parse("a") shouldBe nonEmptyListOf("Must have length > 2").left()
// p.parse("ab") shouldBe Pair("ab", "ab").right()
p.parse("a") shouldBe nonEmptyListOf("Must have length > 2").left()
p.parse("ab") shouldBe Pair("ab", "ab").right()
}

xtest("zip 3") {
test("zip 3") {
val p1 = Parser.from<String?>().notNullOrBlank { "foo" }.minlen(2) { "Must have length > 2" }
val p2 = Parser.from<String?>().notNullOrBlank { "bar" }
val p3 = Parser.from<String?>().notNullOrBlank { "baz" }
val p = Parser.zip(p1, p2, p3)
// p.parse(null) shouldBe nonEmptyListOf("foo", "bar", "baz").left()
p.parse(null) shouldBe nonEmptyListOf("foo", "bar", "baz").left()
p.parse("a") shouldBe nonEmptyListOf("Must have length > 2").left()
p.parse("ab") shouldBe Triple("ab", "ab", "ab").right()
}
Expand Down

0 comments on commit b053e0d

Please sign in to comment.