Skip to content

Instantly share code, notes, and snippets.

@holograph
Created March 4, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holograph/00acf3680295b3358f28ac812ce4ade8 to your computer and use it in GitHub Desktop.
Save holograph/00acf3680295b3358f28ac812ce4ade8 to your computer and use it in GitHub Desktop.
Accord example showing stateful validators
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