1

I have Product case class, which is returned by DAO layer (using Salat). User who is creating a product first time status of the product remains as "draft" where no field (of product) is mandatory.

What are the best functional ways to validate 10 of product's attributes, accumulate all validation errors into a single entity and then pass all errors at once in a JSON format to front end?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
user2066049
  • 1,371
  • 1
  • 12
  • 26

2 Answers2

3

I assume the core of the question is how to accumulate errors--JSON formatting is a separate issue and does not depend upon how you have collected your errors.

If it's really just a validation issue, you can have a series of methods

def problemWithX: Option[String] = ...

which return Some(errorMessage) if they are invalid or None if they're okay. Then it's as simple as

List(problemWithX, problemWithY, ...).flatten

to create a list of all of your errors. If your list is empty, you're good to go. If not, you have the errors listed. Creating some sensible error report is the job of the problemWithX method--and of course you need to decide whether merely a string or more complex information is necessary. (You may even need to define a Invalid trait and have classes extend it to handle different conditions.)

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
1

This is exactly what ScalaZ's Validation type is for.

Randall Schulz
  • 26,420
  • 4
  • 61
  • 81