Skip to content

Commit a13fb87

Browse files
committed
no-jira: raise ArgumentError if enum limit: is not an array of 1 or more Symbols
1 parent 43459d4 commit a13fb87

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/declare_schema/model/field_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ def initialize(model, name, type, position: 0, **options)
7676
when :bigint
7777
@type = :integer
7878
@options[:limit] = 8
79+
when :enum
80+
@options[:limit].is_a?(Array) && @options[:limit].size >= 1 && @options[:limit].all? { |value| value.is_a?(Symbol) } or
81+
raise ArgumentError, "enum limit: must be an array of 1 or more Symbols; got #{@options[:limit].inspect}"
7982
end
8083

8184
Column.native_type?(@type) or raise UnknownTypeError, "#{@type.inspect} not found in #{Column.native_types.inspect} for adapter #{ActiveRecord::Base.connection.class.name}"
8285

83-
if @type.in?([:string, :text, :binary, :varbinary, :integer, :enum])
86+
if @type.in?([:string, :text, :binary, :varbinary, :integer])
8487
@options[:limit] ||= Column.native_types.dig(@type, :limit)
88+
elsif @type.in?([:enum])
89+
# nothing to do
8590
else
8691
@type != :decimal && @options.has_key?(:limit) and warn("unsupported limit: for SQL type #{@type} in field #{model}##{@name}")
8792
@options.delete(:limit)

spec/lib/declare_schema/field_spec_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@
115115
end
116116
end
117117

118+
describe 'enum' do
119+
before do
120+
allow(::DeclareSchema::Model::Column).to receive(:native_types).and_wrap_original do |m, *_args|
121+
result = m.call
122+
if result.has_key?(:enum)
123+
result
124+
else
125+
result.merge(enum: { name: "enum" })
126+
end
127+
end
128+
end
129+
130+
it 'raises ArgumentError if any of the limit values are not Symbols' do
131+
[['first', 'second', 'third'], [1, 2, 3], nil, []].each do |limit|
132+
expect do
133+
described_class.new(model, :status, :enum, limit: limit, null: false, position: 2)
134+
end.to raise_exception(ArgumentError, /enum limit: must be an array of 1 or more Symbols; got #{Regexp.escape(limit.inspect)}/)
135+
end
136+
end
137+
end
138+
118139
if defined?(Mysql2)
119140
describe 'varbinary' do # TODO: :varbinary is an Invoca addition to Rails; make it a configurable option
120141
it 'is supported' do

0 commit comments

Comments
 (0)