Class: TableStructure::Schema::Definition::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/schema/definition/validator.rb

Constant Summary collapse

DEFAULT_SIZE =
1

Instance Method Summary collapse

Constructor Details

#initialize(index, options) ⇒ Validator

Returns a new instance of Validator.



9
10
11
12
# File 'lib/table_structure/schema/definition/validator.rb', line 9

def initialize(index, options)
  @index = index
  @options = options
end

Instance Method Details

#validate(name:, key:, size:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/table_structure/schema/definition/validator.rb', line 14

def validate(name:, key:, size:, **)
  if key.respond_to?(:call)
    raise Error.new('"key" must not be lambda.', @index)
  end
  if !key && name.respond_to?(:call) && !size
    raise Error.new('"size" must be defined, because column size cannot be determined.', @index)
  end
  if size && size < DEFAULT_SIZE
    raise Error.new('"size" must be positive.', @index)
  end
  if @options[:result_type] == :hash && !key
    raise Error.new('"key" must be defined when "result_type: :hash" is specified.', @index)
  end
  if key && size && [key].flatten.size < size
    raise Error.new('"key" size must be greater than or equal to specified "size".', @index)
  end

  true
end