Skip to content

Commit

Permalink
feat: ValidationPolicy extends Function, add and and or to Valida…
Browse files Browse the repository at this point in the history
…tionPolicy (#37)
  • Loading branch information
0lejk4 authored Jul 12, 2022
1 parent cb44416 commit 49a7c99
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ ThisBuild / developers :=
)

lazy val V = new {
val Cats = "2.7.0"
val Zio = "1.0.13"
val Cats = "2.8.0"
val Zio = "1.0.15"
val Scala3 = "3.1.2"
val Scala213 = "2.13.8"
val Scala212 = "2.12.16"
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/scala/examples/RegisterExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ object RegisterRequest {
def asyncPolicy(implicit userService: UserService): Policy[RegisterRequest] =
Policy
.builder[RegisterRequest]
// Include Basic validation
.rule(RegisterRequest.basicPolicy.validate)
// Below will work to but keep in mind not to fall for cyclic implicit resolution
// .rule(RegisterRequest.basicPolicy)
// .rule(_.validate)
// Our Async validations
.subRule(_.username)(
Expand All @@ -99,6 +98,7 @@ object RegisterRequest {
)
.subRule(_.email)(_.ensureF(userService.emailIsAvailable, _.failMessage("Email is not available")))
.build
.and(RegisterRequest.basicPolicy)
}

trait UserService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class PostValidationService(postRepo: PostRepo, userRepo: UserRepo) {
implicit val policy: Policy[MediumPost] =
Policy
.builder[MediumPost]
.rule(MediumPost.policy.validate)
.rule(MediumPost.policy)
.subRule(_.authorId)(_.ensureF(userRepo.userExists, failCode(33)))
.subRule(_.postId)(_.ensureF(postRepo.postDoesNotExist, failCode(44)))
.subRule(_.title)(_.ensureF(postRepo.titleExists(_).negate, failCode(55)))
Expand Down
8 changes: 7 additions & 1 deletion modules/core/src/main/scala/fields/ValidationPolicy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package jap.fields
import typeclass._

/** Typeclass that defines how to validate given field */
trait ValidationPolicy[P, F[_], V[_], E] { self =>
trait ValidationPolicy[P, F[_], V[_], E] extends (Field[P] => Rule[F, V, E]) { self =>
def validate(field: Field[P]): Rule[F, V, E]

def apply(field: Field[P]): Rule[F, V, E] = validate(field)
Expand All @@ -30,6 +30,12 @@ trait ValidationPolicy[P, F[_], V[_], E] { self =>
if (V.isValid(v)) Right(field.value)
else Left(E.errors(v))
}

def and(other: ValidationPolicy[P, F, V, E])(implicit F: Effect[F], V: Validated[V]): ValidationPolicy[P, F, V, E] =
field => Rule.and(self(field), other(field))

def or(other: ValidationPolicy[P, F, V, E])(implicit F: Effect[F], V: Validated[V]): ValidationPolicy[P, F, V, E] =
field => Rule.or(self(field), other(field))
}

object ValidationPolicy {
Expand Down

0 comments on commit 49a7c99

Please sign in to comment.