Skip to content

Commit

Permalink
Fix signature for validate_prop_name (#2548)
Browse files Browse the repository at this point in the history
* Fix signature for validate_prop_name

* Add test

* Use match?
  • Loading branch information
djudd-stripe authored and jez committed Jan 27, 2020
1 parent 36c7cd9 commit 68c732e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions gems/sorbet-runtime/lib/types/props/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ def prop_validate_definition!(name, cls, rules, type)
nil
end

sig {params(name: Symbol).void}
# Used to validate both prop names and serialized forms
sig {params(name: T.any(Symbol, String)).void}
private def validate_prop_name(name)
if name !~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/
if !name.match?(/\A[A-Za-z_][A-Za-z0-9_-]*\z/)
raise ArgumentError.new("Invalid prop name in #{@class.name}: #{name}")
end
end
Expand Down
12 changes: 12 additions & 0 deletions gems/sorbet-runtime/test/types/props/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,16 @@ class NoPropsStruct < T::Struct
assert_equal({}, NoPropsStruct.from_hash({}).serialize)
end
end

class CustomSerializedForm < T::Struct
prop :prop, String, name: 'something_custom'
end

describe 'custom serialized form' do
it 'round trips' do
hash = CustomSerializedForm.new(prop: 'foo').serialize
assert_equal({'something_custom' => 'foo'}, hash)
assert_equal('foo', CustomSerializedForm.from_hash(hash).prop)
end
end
end

0 comments on commit 68c732e

Please sign in to comment.