Class: MagicQuery::Schema::Validator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Validator

Returns a new instance of Validator.



10
11
12
# File 'lib/magic_query/schema/validator.rb', line 10

def initialize(schema)
  @schema = schema
end

Class Method Details

.validate(schema) ⇒ Object



6
7
8
# File 'lib/magic_query/schema/validator.rb', line 6

def self.validate(schema)
  new(schema).validate
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/magic_query/schema/validator.rb', line 40

def valid?
  validate.empty?
end

#validateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/magic_query/schema/validator.rb', line 14

def validate
  errors = []

  unless @schema.is_a?(Hash)
    errors << 'Schema must be a Hash'
    return errors
  end

  @schema.each do |table_name, table_info|
    errors.concat(validate_table(table_name, table_info))
  end

  errors
end

#validate_table(table_name, table_info) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/magic_query/schema/validator.rb', line 29

def validate_table(table_name, table_info)
  errors = []
  unless table_info.is_a?(Hash)
    errors << "Table #{table_name}: must be a Hash"
    return errors
  end

  errors << "Table #{table_name}: columns must be an Array" unless table_info[:columns].is_a?(Array)
  errors
end