Skip to content

Improve DynamicValue error handling with Validation#1061

Open
AfshaZabeen-5G2 wants to merge 3 commits intozio:mainfrom
AfshaZabeen-5G2:fix/dynamicvalue#480
Open

Improve DynamicValue error handling with Validation#1061
AfshaZabeen-5G2 wants to merge 3 commits intozio:mainfrom
AfshaZabeen-5G2:fix/dynamicvalue#480

Conversation

@AfshaZabeen-5G2
Copy link
Copy Markdown

/claim #480

🎯 Summary

This PR improves the DynamicValue.toTypedValue operation by switching from a "fail-fast" Either approach to an "error-accumulating" Validation approach using zio-prelude. This allows the library to collect all validation errors in a single pass instead of stopping at the first failure.

❓ Problem

The previous implementation used Either[DecodeError, A], which:

  • Stopped conversion at the first encountered error.
  • For complex nested structures, only reported the first issue found.
  • Forced users into an inefficient "fix one, re-run, find the next" workflow.

✅ Solution

Implemented Validation[DecodeError, A] from zio-prelude to enable:

  • Error Accumulation: Reports all validation issues across the entire data structure in one go.
  • Improved DX: Developers can now see the full state of their data's validity.
  • Zero Breaking Changes: Fully backward compatible with existing Either and Option APIs.

🛠 Changes

1. New API Method

def toTypedValueValidation[A](implicit schema: Schema[A]): Validation[DecodeError, A]

  • The primary entry point for users who want to access accumulated errors.

2. Updated Existing Methods (Backward Compatible)

  • toTypedValue[A]: Now projects Validation to Either, combining multiple errors into a single string.
  • toValue[A]: Now projects Validation to Either, combining multiple errors using DecodeError.And.
  • toTypedValueOption[A]: Internal logic now uses validation, but still returns None on any failure.

3. Error Accumulation Points

Errors are now collected across:

  • Tuples: Both left and right errors via Validation.validateWith.
  • Sequences/Sets: All element errors via Validation.validateAll.
  • Maps: Both key and value errors.
  • Records: All field errors via the new decodeStructureValidation.

4. Helper Methods

  • DecodeError.And.reduce: Helper to merge multiple DecodeError instances.
  • decodeStructureValidation: A validation-native version of the record decoding logic.

🧪 Testing & Verification

Added a new test suite: DynamicValueValidationSpec.scala.

  • Accumulation Tests: Verified that multiple errors in records, maps, and sequences are all caught.
  • Regression Tests: Ensured existing Either-based APIs still function as expected.
  • CI Check: Ran ./sbt "testsJVM/test" and all tests passed (100%).

📋 Checklist

  • Implementation follows error accumulation pattern.
  • All existing APIs remain backward compatible.
  • New toTypedValueValidation method added.
  • Comprehensive test suite added.
  • No breaking changes.
  • Demo video provided (will add)

- Replace Either (fail-fast) with Validation (error accumulation)
- Add new toTypedValueValidation method for full error collection
- Maintain backward compatibility for toTypedValue, toValue, toTypedValueOption
- Add DecodeError.And.reduce helper for combining errors
- Add comprehensive test suite for error accumulation
- All existing tests pass without modification
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 6, 2026

CLA assistant check
All committers have signed the CLA.

@AfshaZabeen-5G2 AfshaZabeen-5G2 requested a review from a team as a code owner April 6, 2026 11:40
- Replace Either[DecodeError, A] with Validation[DecodeError, A] for error accumulation
- Add toTypedValueValidation method for Validation-based decoding
- Maintain backward compatibility with existing toTypedValue, toValue, toTypedValueOption methods
- Add DecodeError.reduceErrors to combine multiple errors
- Add comprehensive test suite for error accumulation
- Add .gitattributes for consistent LF line endings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants