The transform() Method
Every decoder has a.transform() method that accepts a function to modify the validated value.
String Transformations
Number Transformations
Type Conversions
Transformations can change the output type:Object Transformations
Array Transformations
Chaining Transformations
You can chain multiple transformations together:Combining with Pipe
Use.pipe() to validate the transformed result with another decoder:
Error Handling in Transformations
If a transformation function throws an error, the decoder will fail with that error message:Real-World Examples
Normalizing Phone Numbers
Converting API Response
Parsing Date Strings
URL Parsing
When to Use Transformations
Use transformations when you need to:- Normalize data: Convert to consistent format (trim, lowercase, etc.)
- Convert types: String to number, string to Date, etc.
- Compute values: Add derived fields, calculate totals
- Reshape data: Rename fields, flatten structures, extract subsets
- Format output: Apply formatting rules for display
Transform vs Refine vs Reject
.transform(fn): Modify the validated value (changes the value).refine(predicate, msg): Add validation constraint (keeps the value).reject(fn): Conditional rejection with dynamic error messages
Next Steps
- Learn about refinements for custom validation logic
- Explore custom decoders for advanced use cases
- Check out the API reference for all decoder methods
