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(name, index) ⇒ Validator

Returns a new instance of Validator.



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

def initialize(name, index)
  @name = name
  @index = index
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
# 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.', @name, @index)
  end
  if !key && name.respond_to?(:call) && !size
    raise Error.new('"size" must be defined, because column size cannot be determined.', @name, @index)
  end
  if size && size < DEFAULT_SIZE
    raise Error.new('"size" must be positive.', @name, @index)
  end
  if key && size && [key].flatten.size < size
    raise Error.new('"key" size must not be less than specified "size".', @name, @index)
  end

  true
end