Class: I18nJSON::Schema
- Inherits:
-
Object
- Object
- I18nJSON::Schema
- 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
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
- #expect_array_with_items(attribute, value, payload = value) ⇒ Object
- #expect_required_keys(required_keys, value) ⇒ Object
- #expect_type(attribute, value, expected_type, payload) ⇒ Object
-
#initialize(target) ⇒ Schema
constructor
A new instance of Schema.
- #reject(error_message, node = nil) ⇒ Object
- #reject_extraneous_keys(allowed_keys, value) ⇒ Object
- #validate! ⇒ Object
- #validate_translation(translation) ⇒ Object
- #validate_translations ⇒ Object
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
#target ⇒ Object (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 = [ "Expected #{attribute.inspect} to be #{expected_type};", "got #{actual_type} instead" ].join(" ") reject , payload end |
#reject(error_message, node = nil) ⇒ Object
49 50 51 52 |
# File 'lib/i18n-json/schema.rb', line 49 def reject(, node = nil) node_json = "\n#{JSON.pretty_generate(node)}" if node raise InvalidError, "#{}#{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_translations ⇒ Object
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 |