Created
March 4, 2020 17:56
-
-
Save holograph/00acf3680295b3358f28ac812ce4ade8 to your computer and use it in GitHub Desktop.
Accord example showing stateful validators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wix.accord.examples | |
class Stateful(allowedNames: Set[String]) { | |
import com.wix.accord.dsl._ | |
import com.wix.accord._ | |
private implicit val classroomValidator = validator[Classroom] { c => | |
c.teacher.name is in(allowedNames) | |
c.students.map(_.name).each is in(allowedNames) | |
} | |
def process(classroom: Classroom): Unit = { | |
val result = validate(classroom) | |
if (result.isFailure) | |
throw new Exception("Uncool: " + result) | |
} | |
} | |
object Stateful { | |
def main(args: Array[String]): Unit = { | |
val allowed = Set("moshe", "haim", "boris", "iris") | |
val classroom = Classroom( | |
grade = 10, | |
teacher = Adult("iris", "abutbul", 27, "somewhere over the rainbow"), | |
students = Set( | |
Minor("moshe", "abutbul", 15, Set.empty), | |
Minor("boris", "tchaikovsky", 15, Set.empty), | |
Minor("drama", "queen", 15, Set.empty), | |
) | |
) | |
val processor = new Stateful(allowed) | |
processor.process(classroom) // Fails on "drama" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment