Class: ForestAdminDatasourceToolkit::Validations::RecordValidator

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/forest_admin_datasource_toolkit/validations/record_validator.rb

Class Method Summary collapse

Class Method Details

.validate(collection, record_data) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/forest_admin_datasource_toolkit/validations/record_validator.rb', line 6

def self.validate(collection, record_data)
  raise ForestException, 'The record data is empty' if !record_data || record_data.empty?

  record_data.each_key do |key|
    schema = collection.schema[:fields][key]

    if !schema
      raise ForestException, "Unknown field #{key}"
    elsif schema.type == 'Column'
      FieldValidator.validate(collection, key, record_data[key])
    elsif ['OneToOne', 'ManyToOne'].include?(schema.type)
      sub_record = record_data[key]
      association = collection.datasource.get_collection(schema.foreign_collection)
      RecordValidator.validate(association, sub_record)
    else
      raise ForestException, "Unexpected schema type '#{schema.type}' while traversing record"
    end
  end
end