Skip to content

Add path property to ValidationBuilder for path override#270

Draft
dhoepelman wants to merge 2 commits into
mainfrom
dhoepelman/issue-198
Draft

Add path property to ValidationBuilder for path override#270
dhoepelman wants to merge 2 commits into
mainfrom
dhoepelman/issue-198

Conversation

@dhoepelman
Copy link
Copy Markdown
Collaborator

Fixes #198

Summary

Adds support for overriding validation paths to suppress redundant path segments, particularly useful for inline/value classes serialized with kotlinx.serialization.

Implementation

  • Added var path: ValidationPath? property to ValidationBuilder
  • Modified validate(), ifPresent(), required(), and onEach*() methods to respect the path override
  • When path is set (e.g., to ValidationPath.EMPTY), it replaces the automatic path segment from the property

Usage Example

@JvmInline
value class ValueClass(val integer: Int)

data class WrapperClass(val valueClass: ValueClass)

val validation = Validation<WrapperClass> {
  WrapperClass::valueClass {
    ValueClass::integer {
      path = ValidationPath.EMPTY  // Suppresses .integer from the path
      minimum(1)
    }
  }
}

// Error path will be: .valueClass (not .valueClass.integer)

This is particularly useful when value classes are transparently serialized by kotlinx.serialization, so the JSON path doesn't include the inner property.

Testing

  • Added comprehensive test suite in PathOverrideTest.kt
  • All existing tests pass
  • API compatibility maintained (new optional property)

Notes

  • Follows the same pattern as RequiredValidationBuilder.hint
  • The path property is nullable to distinguish "not set" from "set to EMPTY"
  • Works with nested validations and doesn't affect sibling validations

dhoepelman and others added 2 commits February 7, 2026 21:56
Implements #198: Add support for overriding validation paths to
suppress redundant path segments, particularly useful for inline/value
classes serialized with kotlinx.serialization.

Users can now set `path = ValidationPath.EMPTY` in a validation block
to suppress the automatic path segment, or set a custom path to
replace it entirely.

Example usage:
```kotlin
Validation<WrapperClass> {
  WrapperClass::valueClass {
    ValueClass::integer {
      path = ValidationPath.EMPTY  // Suppresses .integer
      minimum(1)
    }
  }
}
// Error path: .valueClass (not .valueClass.integer)
```

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redundant path segment

1 participant