Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
User.parse(id: "123") # ✓ valid (name is optional)
```

### Changed

- Standardize error messages to use lowercase for consistency with Ruby conventions
- Improve type error messages to include attribute name for easier debugging

## [4.1.0] - 2025-10-09

### Added
Expand Down
9 changes: 7 additions & 2 deletions lib/structure/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def attribute(name, type = nil, from: nil, default: nil, null: true, &block)
@non_nullable.add(name) unless null

if type && block
raise ArgumentError, "Cannot specify both type and block for :#{name}"
raise ArgumentError, "cannot specify both type and block for :#{name}"
else
types[name] = type || block
end
Expand Down Expand Up @@ -102,7 +102,12 @@ def non_nullable = @non_nullable.to_a

# @api private
def coercions(context = nil)
@types.transform_values { |type| Types.coerce(type, context) }
@types.to_h do |attr, type|
coercion = Types.coerce(type, context)
[attr, coercion]
rescue ArgumentError => e
raise ArgumentError, "#{e.message} for :#{attr}"
end
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion lib/structure/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def coerce(type, context = nil)
when String
lazy_class(type, context)
else
raise ArgumentError, "Cannot specify #{type.inspect} as type"
raise ArgumentError, "cannot specify #{type.inspect} as type"
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_core_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_attribute_with_both_type_and_block_raises_error
end
end

assert_match(/Cannot specify both type and block/, error.message)
assert_match(/cannot specify both type and block/, error.message)
end

def test_attribute_with_default_value
Expand Down
4 changes: 3 additions & 1 deletion test/test_type_coercions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,12 @@ def test_empty_hash_type_raises_error
def test_invalid_object_type_raises_error
invalid_object = Object.new

assert_raises(ArgumentError) do
error = assert_raises(ArgumentError) do
Structure.new do
attribute(:value, invalid_object)
end
end

assert_match(/cannot specify.*as type for :value/, error.message)
end
end