Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
206: Data Validation Pipelines with Scala
I've seen this play out in a dozen different code reviews: a developer builds a registration form or an API endpoint, and the validation logic looks like a giant nesting doll of if/else blocks or a sequence of for-comprehensions using Option or Either. On the surface, it looks clean. It's typesafe, it handles errors, and it compiles. But the moment a user interacts with it, the experience is miserable.
The Whack-a-Mole Experience
Let's say we're validating a UserRegistration request. We need to check if the email is valid, if the password is strong enough, and if the age is above 18. The "naive" functional approach usually looks something like this:
case class UserRegistration(email: String, password: String, age: Int)
def validateEmail(email: String): Either[String, String] =
if (email.contains("@")) Right(email) else Left("Invalid email format")
def validatePassword(pw: String): Either[String, String] =
if (pw.length >= 8) Right(pw) else Left("Password too short")
def validateAge(age: Int): Either[String, Int] =
if (age >= 18) Right(age) else Left("Must be 18 or older")
def validateUser(reg: UserRegistration) = for {
email <- validateEmail(reg.email)
pw <- validatePassword(reg.password)
age <- validateAge(reg.age)
} yield User(email, pw, age)
The problem here is that Either is designed to short-circuit. It's a "fail-fast" mechanism. If the email is invalid, the code stops right there and returns the email error. The user fixes their email, hits submit again, and then the system tells them their password is too short. They fix the password, submit again, and then it tells them they're too young. It's a game of whack-a-mole that drives users crazy.
Accumulating Failures instead of Short-Circuiting
In a real-world pipeline, you don't want the first error; you want all the errors. To do this, we have to move away from the monadic behavior of Either and toward an applicative style. In the Scala ecosystem, this is where Validated (from the Cats library) usually comes in, but the mental model is what matters most: we want to collect a list of failures rather than stopping at the first one.
Instead of a single String for an error, we use a NonEmptyList (Nel). This ensures that if we are in the "Failure" state, we definitely have at least one error to report. I prefer this because it removes the possibility of having an "Error" state that contains an empty list of reasons, which would be a logic bug.
import cats.data.ValidatedNel
import cats.implicits._
type ValidationResult[A] = ValidatedNel[String, A]
def validateEmail(email: String): ValidationResult[String] =
if (email.contains("@")) email.validNel else "Invalid email format".invalidNel
def validatePassword(pw: String): ValidationResult[String] =
if (pw.length >= 8) pw.validNel else "Password too short".invalidNel
def validateAge(age: Int): ValidationResult[Int] =
if (age >= 18) age.validNel else "Must be 18 or older".invalidNel
def validateUser(reg: UserRegistration): ValidationResult[User] = {
(
validateEmail(reg.email),
validatePassword(reg.password),
validateAge(reg.age)
).mapN(User.apply)
}
The Trade-off: Cognitive Load vs. User Experience
Now, let's be honest: mapN and ValidatedNel are more "noisy" than a simple for-comprehension. You're introducing a different set of type classes and a slightly steeper learning curve for anyone joining your project. If you're writing a small internal CLI tool where you're the only user, the short-circuiting Either is perfectly fine—it's faster to write and easier to read.
But for any public-facing API or UI, the cost of the extra complexity is worth it. By treating validation as a parallel accumulation of results rather than a sequential chain of dependencies, you transform the API from a frustrating bottleneck into a helpful guide. You're telling the user everything they need to fix in one go, which is the hallmark of professional software.
📋 Practical Task
Building a Robust Order Validation Pipeline
You are building the checkout logic for an e-commerce platform. An Order is only valid if it meets the following criteria:
- The
orderIdmust not be empty. - The
itemCountmust be greater than 0. - The
shippingAddressmust contain at least one comma (representing city/state separation). - The
totalAmountmust be positive.
- Define a
case class OrderRequestwith the fields mentioned above. - Implement four separate validation functions that return
ValidatedNel[String, A]. - Create a
validateOrderfunction that usesmapNto combine these validations into a finalOrdercase class. - Test your pipeline with an
OrderRequestthat has three invalid fields and print the resulting list of errors to the console to verify that no errors were dropped.
There are no comments for now.