Skip to content

Conversation

@danielnottingham
Copy link
Owner

#6

This PR implements a structured ValidationError object to replace simple string errors, providing a richer and more programmatic validation experience, idiomatically adapted for Ruby.

Changes:

New ValidatorRb::ValidationError Class:

  • Created a value object to hold error details: message, code, path, and meta.
  • Implemented to_h for easy serialization (useful for APIs).

Updated ValidatorRb::Result:

  • errors now returns an array of ValidationError objects instead of strings.
  • error_message preserves backward compatibility by joining the message attributes of the errors.

Refactored Validators:

  • Updated BaseValidator, StringValidator, and IntegerValidator to populate specific error codes (e.g., :too_short, :not_positive) and metadata.

Breaking Changes:

  • Result#errors no longer returns an array of Strings. It returns an array of ValidationError objects. Consumers relying on result.errors.include?("some string") will need to update their code to check error.message or, preferably, error.code.

Example Usage:

result = ValidatorRb.string.min(5).validate("hi")
error = result.errors.first
# Old way (no longer works directly on errors array):
# result.errors.include?("must be at least 5 characters")
# New way:
puts error.code    # => :too_short
puts error.message # => "must be at least 5 characters"

@danielnottingham danielnottingham self-assigned this Dec 12, 2025
@danielnottingham danielnottingham merged commit 42e7238 into main Dec 13, 2025
3 checks passed
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.

2 participants