Gatling’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/gatling/gatling.io-doc/llms.txt
Use this file to discover all available pages before exploring further.
Validation[T] type is a composable abstraction for operations that may either produce a value (Success[T]) or fail with an error message (Failure). It is used internally throughout the Gatling Scala SDK—most notably in Expression[T], which is defined as Session => Validation[T]—to propagate errors cleanly without throwing exceptions. Understanding Validation lets you write safe, composable Scala functions that chain multiple Session lookups and transformations, short-circuiting gracefully when any step fails.
This section is Scala-only. Java and Kotlin users work with plain functions and do not need to interact with
Validation directly. JavaScript/TypeScript users can also skip this page.The Two Cases
Validation[T] has exactly two implementations:
| Type | Meaning |
|---|---|
Success[T](value: T) | The operation succeeded and produced a value of type T |
Failure(message: String) | The operation failed with a descriptive error message |
Either[String, T] or Try[T], but is intentionally simpler and more lightweight than Scalaz’s Validation.
Creating Instances
First, import thevalidation package:
Pattern Matching
The most direct way to handle aValidation result:
Chaining with map and flatMap
The power of Validation comes from its monadic interface. Both methods propagate Failure automatically: if the original Validation is a Failure, the chained function is never called.
map — chain an operation that cannot fail
Use map when the next step always produces a value:
flatMap — chain an operation that can also fail
Use flatMap when the next step itself returns a Validation:
Scala for-Comprehension
For chains of multiple flatMap/map calls, Scala’s for-comprehension syntax is cleaner. Think of it as a loop over a sequence of Validation steps that stops early on the first Failure:
Failure, the entire for-block yields that Failure immediately.
Session Accessors and Validation
The GatlingSession API provides three levels of access for Scala users:
| Method | Return type | Behaviour on missing/wrong type |
|---|---|---|
session("key").as[T] | T | Throws an exception |
session("key").asOption[T] | Option[T] | Returns None |
session("key").validate[T] | Validation[T] | Returns Failure(message) |
validate[T]:
Usage in Expression[T]
In Gatling Scala, Expression[T] is Session => Validation[T]. All SDK methods that accept a dynamic parameter will accept an Expression[T]. Scalar values are implicitly lifted to Success[T]:
Failure, Gatling marks the request as failed with the failure message and does not execute the request.
Example: Composing Multiple Lookups
"age" is missing from the Session, the request is skipped and Gatling logs a failure with the message produced by validate[Int]—no exception is thrown and no other virtual users are affected.