Skip to content

Commit

Permalink
Simplify validator
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Dec 22, 2024
1 parent 388f40d commit 0e69d88
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/src/files/tutorials/Validation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object Validation extends TutorialPage {
.derived[ValidatedData]
.positive(_.num)
.notBlank(_.str)
.notEmptySeq(_.seq)
.minItems(_.seq, 1)
```

The `ValidatedData` can be any `case class`: json data, form data, query params..
Expand Down
2 changes: 1 addition & 1 deletion examples/api/src/requests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object CreateProductReq:
given Validator[CreateProductReq] = Validator
.derived[CreateProductReq]
.notBlank(_.name)
.nonnegative(_.quantity)
.nonNegative(_.quantity)

// query params
case class ProductsQuery(name: Set[String], minQuantity: Option[Int]) derives QueryStringRW
4 changes: 2 additions & 2 deletions examples/scala-cli/validation.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.4.2"
//> using dep ba.sake::sharaf:0.7.0
//> using dep ba.sake::sharaf:0.7.6

import io.undertow.Undertow
import ba.sake.querson.QueryStringRW
Expand All @@ -13,7 +13,7 @@ object Car:
.derived[Car]
.notBlank(_.brand)
.notBlank(_.model)
.nonnegative(_.quantity)
.nonNegative(_.quantity)

case class CarQuery(brand: String) derives QueryStringRW
object CarQuery:
Expand Down
25 changes: 4 additions & 21 deletions validson/src/ba/sake/validson/Validator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ trait Validator[T] {
def negative[F: Numeric](getter: T => sourcecode.Text[F]): Validator[T] =
validatorImpl(getter, _ < summon[Numeric[F]].zero, s"must be negative")

def nonpositive[F: Numeric](getter: T => sourcecode.Text[F]): Validator[T] =
def nonPositive[F: Numeric](getter: T => sourcecode.Text[F]): Validator[T] =
validatorImpl(getter, _ <= summon[Numeric[F]].zero, s"must be nonpositive")

def positive[F: Numeric](getter: T => sourcecode.Text[F]): Validator[T] =
validatorImpl(getter, _ > summon[Numeric[F]].zero, s"must be positive")

def nonnegative[F: Numeric](getter: T => sourcecode.Text[F]): Validator[T] =
def nonNegative[F: Numeric](getter: T => sourcecode.Text[F]): Validator[T] =
validatorImpl(getter, _ >= summon[Numeric[F]].zero, s"must be nonnegative")

// strings
def notEmpty(getter: T => sourcecode.Text[String]): Validator[T] =
validatorImpl(getter, !_.isEmpty, "must not be empty")

def notBlank(getter: T => sourcecode.Text[String]): Validator[T] =
validatorImpl(getter, !_.isBlank, "must not be blank")

Expand All @@ -53,25 +50,11 @@ trait Validator[T] {
validatorImpl(getter, _.matches(value), s"must contain $value")

// seqs
def notEmpty(getter: T => sourcecode.Text[Seq[?]]): Validator[T] =
validatorImpl(getter, !_.isEmpty, "must not be empty")

def minItems(getter: T => sourcecode.Text[Seq[?]], value: String): Validator[T] =
validatorImpl(getter, _.size >= value, s"must be >= $value")

def maxItems(getter: T => sourcecode.Text[Seq[?]], value: String): Validator[T] =
validatorImpl(getter, _.size <= value, s"must be <= $value")

// sets
def notEmpty(getter: T => sourcecode.Text[Set[?]]): Validator[T] =
validatorImpl(getter, !_.isEmpty, "must not be empty")

def minItems(getter: T => sourcecode.Text[Set[?]], value: String): Validator[T] =
def minItems(getter: T => sourcecode.Text[Iterable[?]], value: Int): Validator[T] =
validatorImpl(getter, _.size >= value, s"must be >= $value")

def maxItems(getter: T => sourcecode.Text[Set[?]], value: String): Validator[T] =
def maxItems(getter: T => sourcecode.Text[Iterable[?]], value: Int): Validator[T] =
validatorImpl(getter, _.size <= value, s"must be <= $value")


private def validatorImpl[F](getter: T => sourcecode.Text[F], predicate: F => Boolean, msg: String): Validator[T] =
(value: T) => {
Expand Down
4 changes: 2 additions & 2 deletions validson/test/src/ba/sake/validson/ValidsonSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object SimpleData:
.derived[SimpleData]
.positive(_.num)
.notBlank(_.str)
.notEmptySeq(_.seq)
.minItems(_.seq, 1)
.and(_.seq, _.forall(_.size == 2), "must have elements of size 2")

case class ComplexData(password: String, datas: Seq[SimpleData], matrix: Seq[Seq[SimpleData]])
Expand All @@ -83,4 +83,4 @@ object ComplexData:
.derived[ComplexData]
.contains(_.password, "A")
.contains(_.password, "5")
.notEmptySeq(_.matrix)
.minItems(_.matrix, 1)

0 comments on commit 0e69d88

Please sign in to comment.