class I18nJSON::Schema

Constants

InvalidError
REQUIRED_ROOT_KEYS
REQUIRED_TRANSLATION_KEYS
ROOT_KEYS
TRANSLATION_KEYS

Attributes

target[R]

Public Class Methods

new(target) click to toggle source
# File lib/i18n-json/schema.rb, line 18
def initialize(target)
  @target = target
end
validate!(target) click to toggle source
# File lib/i18n-json/schema.rb, line 12
def self.validate!(target)
  new(target).validate!
end

Public Instance Methods

expect_array_with_items(attribute, value, payload = value) click to toggle source
# File lib/i18n-json/schema.rb, line 67
def expect_array_with_items(attribute, value, payload = value)
  return unless value.empty?

  reject "Expected #{attribute.inspect} to have at least one item", payload
end
expect_required_keys(required_keys, value) click to toggle source
# File lib/i18n-json/schema.rb, line 73
def expect_required_keys(required_keys, value)
  keys = value.keys.map(&:to_sym)

  required_keys.each do |key|
    next if keys.include?(key)

    reject "Expected #{key.inspect} to be defined", value
  end
end
expect_type(attribute, value, expected_type, payload) click to toggle source
# File lib/i18n-json/schema.rb, line 54
def expect_type(attribute, value, expected_type, payload)
  return if value.is_a?(expected_type)

  actual_type = value.class

  message = [
    "Expected #{attribute.inspect} to be #{expected_type};",
    "got #{actual_type} instead"
  ].join(" ")

  reject message, payload
end
reject(error_message, node = nil) click to toggle source
# File lib/i18n-json/schema.rb, line 49
def reject(error_message, node = nil)
  node_json = "\n#{JSON.pretty_generate(node)}" if node
  raise InvalidError, "#{error_message}#{node_json}"
end
reject_extraneous_keys(allowed_keys, value) click to toggle source
# File lib/i18n-json/schema.rb, line 83
def reject_extraneous_keys(allowed_keys, value)
  keys = value.keys.map(&:to_sym)
  extraneous = keys - allowed_keys

  return if extraneous.empty?

  reject "Unexpected keys: #{extraneous.join(', ')}", value
end
validate!() click to toggle source
# File lib/i18n-json/schema.rb, line 22
def validate!
  expect_type(:root, target, Hash, target)

  expect_required_keys(REQUIRED_ROOT_KEYS, target)
  reject_extraneous_keys(ROOT_KEYS, target)
  validate_translations
end
validate_translation(translation) click to toggle source
# File lib/i18n-json/schema.rb, line 41
def validate_translation(translation)
  expect_required_keys(REQUIRED_TRANSLATION_KEYS, translation)
  reject_extraneous_keys(TRANSLATION_KEYS, translation)
  expect_type(:file, translation[:file], String, translation)
  expect_type(:patterns, translation[:patterns], Array, translation)
  expect_array_with_items(:patterns, translation[:patterns], translation)
end
validate_translations() click to toggle source
# File lib/i18n-json/schema.rb, line 30
def validate_translations
  translations = target[:translations]

  expect_type(:translations, translations, Array, target)
  expect_array_with_items(:translations, translations)

  translations.each do |translation|
    validate_translation(translation)
  end
end