Class: I18nJSON::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n-json/schema.rb

Constant Summary collapse

InvalidError =
Class.new(StandardError)
ROOT_KEYS =
%i[translations].freeze
REQUIRED_ROOT_KEYS =
%i[translations].freeze
REQUIRED_TRANSLATION_KEYS =
%i[file patterns].freeze
TRANSLATION_KEYS =
%i[file patterns].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Schema

Returns a new instance of Schema.



18
19
20
# File 'lib/i18n-json/schema.rb', line 18

def initialize(target)
  @target = target
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



16
17
18
# File 'lib/i18n-json/schema.rb', line 16

def target
  @target
end

Class Method Details

.validate!(target) ⇒ Object



12
13
14
# File 'lib/i18n-json/schema.rb', line 12

def self.validate!(target)
  new(target).validate!
end

Instance Method Details

#expect_array_with_items(attribute, value, payload = value) ⇒ Object



67
68
69
70
71
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Raises:



49
50
51
52
# 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) ⇒ Object



83
84
85
86
87
88
89
90
# 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!Object



22
23
24
25
26
27
28
# 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) ⇒ Object



41
42
43
44
45
46
47
# 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_translationsObject



30
31
32
33
34
35
36
37
38
39
# 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