Skip to content

Instantly share code, notes, and snippets.

@gabro
Created September 12, 2014 10:17
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 gabro/b192a7258ec5072bd1e0 to your computer and use it in GitHub Desktop.
Save gabro/b192a7258ec5072bd1e0 to your computer and use it in GitHub Desktop.
Beautiful validation with scalaz. Fail fast in the for-comprehension and fail slow with the application builder
def editResult(testId: LabOnlineId[Test], worklistId: LabOnlineId[Worklist], result: TestResult): Future[ActionResult] =
for {
test <- ensureExists(testId)(testData.findOne(_))
sample <- ensureExists(test._sampleId)(sampleData.findOneWithWorklist(_, worklistId))
} yield {
(ensureNotClosed(sample, worklistId) |@|
ensureNotClosed(test, worklistId))((_,_)) match {
case Failure(errors) => ActionResult.UserError(errors.toList: _*)
case Success(_) => /* run a stored procedure */ ActionResult.Ok
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment