module JsonTableSchema::Validate

Attributes

messages[R]

Public Instance Methods

load_validator!() click to toggle source
# File lib/jsontableschema/validate.rb, line 6
def load_validator!
  filepath = File.join(File.dirname(__FILE__), '..', '..', 'etc', 'schemas', 'json-table-schema.json')
  @validator ||= JSON.parse(File.read filepath)
end
valid?() click to toggle source
# File lib/jsontableschema/validate.rb, line 11
def valid?
  validate
  @messages.count == 0
end
validate() click to toggle source
# File lib/jsontableschema/validate.rb, line 16
def validate
  @messages = JSON::Validator.fully_validate(@validator, self)
  check_primary_keys
  check_foreign_keys
end

Private Instance Methods

add_error(error) click to toggle source
# File lib/jsontableschema/validate.rb, line 49
def add_error(error)
  @messages << error
end
check_field_value(key, type) click to toggle source
# File lib/jsontableschema/validate.rb, line 37
def check_field_value(key, type)
  add_error("The JSON Table Schema #{type} value `#{key}` is not found in any of the schema's field names") if headers.select { |f| key == f }.count == 0
end
check_foreign_keys() click to toggle source
# File lib/jsontableschema/validate.rb, line 29
def check_foreign_keys
  return if self['foreignKeys'].nil?
  self['foreignKeys'].each do |keys|
    foreign_key_fields(keys).each { |fk| check_field_value(fk, 'foreignKey.fields') }
    add_error("A JSON Table Schema foreignKey.fields must contain the same number entries as foreignKey.reference.fields.") if field_count_mismatch?(keys)
  end
end
check_primary_keys() click to toggle source
# File lib/jsontableschema/validate.rb, line 24
def check_primary_keys
  return if self['primaryKey'].nil?
  primary_keys.each { |pk| check_field_value(pk, 'primaryKey') }
end
field_count_mismatch?(keys) click to toggle source
# File lib/jsontableschema/validate.rb, line 45
def field_count_mismatch?(keys)
  keys['reference'] &&([keys['fields']].flatten.count != [keys['reference']['fields']].flatten.count)
end
foreign_key_fields(keys) click to toggle source
# File lib/jsontableschema/validate.rb, line 41
def foreign_key_fields(keys)
  [keys['fields']].flatten
end