Class: Schemacop::V3::HashNode

Inherits:
Node
  • Object
show all
Defined in:
lib/schemacop/v3/hash_node.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  type
  min_properties
  max_properties
  dependencies
  property_names
].freeze

Instance Attribute Summary collapse

Attributes inherited from Node

#as, #default, #description, #name, #options, #parent, #require_key, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

create, #create, #dsl_node, #dsl_scm, #initialize, #require_key?, #required?, resolve_class, #schemas, supports_children, supports_children_options, #used_external_schemas, #validate

Constructor Details

This class inherits a constructor from Schemacop::V3::Node

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



14
15
16
# File 'lib/schemacop/v3/hash_node.rb', line 14

def properties
  @properties
end

Class Method Details

.allowed_optionsObject



16
17
18
# File 'lib/schemacop/v3/hash_node.rb', line 16

def self.allowed_options
  super + ATTRIBUTES - %i[dependencies] + %i[additional_properties ignore_obsolete_properties]
end

.dsl_methodsObject



20
21
22
# File 'lib/schemacop/v3/hash_node.rb', line 20

def self.dsl_methods
  super + NodeRegistry.dsl_methods(true) + %i[dsl_dep dsl_add]
end

Instance Method Details

#_validate(data, result: Result.new) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/schemacop/v3/hash_node.rb', line 84

def _validate(data, result: Result.new)
  super_data = super
  return if super_data.nil?

  original_data_hash = super_data.dup
  data_hash = super_data.with_indifferent_access

  if original_data_hash.size != data_hash.size
    ambiguous_properties = original_data_hash.keys - data_hash.keys

    result.error "Has #{ambiguous_properties.size} ambiguous properties: #{ambiguous_properties}."
  end

  # Validate min_properties #
  if options[:min_properties] && data_hash.size < options[:min_properties]
    result.error "Has #{data_hash.size} properties but needs at least #{options[:min_properties]}."
  end

  # Validate max_properties #
  if options[:max_properties] && data_hash.size > options[:max_properties]
    result.error "Has #{data_hash.size} properties but needs at most #{options[:max_properties]}."
  end

  # Validate specified properties #
  @properties.each_value do |node|
    result.in_path(node.name) do
      next if node.name.is_a?(Regexp)

      result.error "Key #{node.name} must be given." if node.require_key? && !data_hash.include?(node.name)
      node._validate(data_hash[node.name], result: result)
    end
  end

  # Validate additional properties #
  specified_properties = @properties.keys.to_set
  additional_properties = data_hash.reject { |k, _v| specified_properties.include?(k.to_s) }

  property_patterns = {}

  @properties.each_value do |property|
    if property.name.is_a?(Regexp)
      property_patterns[property.name] = property
    end
  end

  property_names = options[:property_names]
  property_names = Regexp.compile(property_names) if property_names

  additional_properties.each do |name, additional_property|
    if property_names && !property_names.match?(name)
      result.error "Property name #{name.inspect} does not match #{options[:property_names].inspect}."
    end

    if options[:additional_properties].is_a?(TrueClass)
      next
    elsif options[:additional_properties].is_a?(FalseClass) || options[:additional_properties].blank?
      match = property_patterns.keys.find { |p| p.match?(name.to_s) }
      if match
        result.in_path(name) do
          property_patterns[match]._validate(additional_property, result: result)
        end
      elsif (options[:ignore_obsolete_properties].is_a?(Enumerable) &&
             options[:ignore_obsolete_properties].exclude?(name.to_sym)) ||
            !options[:ignore_obsolete_properties]
        result.error "Obsolete property #{name.to_s.inspect}."
      end
    elsif options[:additional_properties].is_a?(Node)
      result.in_path(name) do
        options[:additional_properties]._validate(additional_property, result: result)
      end
    end
  end

  # Validate dependencies #
  options[:dependencies]&.each do |source, targets|
    targets.each do |target|
      if data_hash[source].present? && data_hash[target].blank?
        result.error "Missing property #{target.to_s.inspect} because #{source.to_s.inspect} is given."
      end
    end
  end
end

#add_child(node) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/schemacop/v3/hash_node.rb', line 24

def add_child(node)
  unless node.name
    fail Exceptions::InvalidSchemaError, 'Child nodes must have a name.'
  end

  @properties[node.name] = node
end

#allowed_typesObject



80
81
82
# File 'lib/schemacop/v3/hash_node.rb', line 80

def allowed_types
  { Hash => :object }
end

#as_jsonObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/schemacop/v3/hash_node.rb', line 45

def as_json
  properties = {}
  pattern_properties = {}

  @properties.each do |name, property|
    if name.is_a?(Regexp)
      pattern_properties[name] = property
    else
      properties[name] = property
    end
  end

  json = {}
  json[:properties] = properties.values.map { |p| [p.name, p.as_json] }.to_h if properties.any?
  json[:patternProperties] = pattern_properties.values.map { |p| [V3.sanitize_exp(p.name), p.as_json] }.to_h if pattern_properties.any?

  # In schemacop, by default, additional properties are not allowed,
  # the users explicitly need to enable additional properties
  if options[:additional_properties].is_a?(TrueClass)
    json[:additionalProperties] = true
  elsif options[:additional_properties].is_a?(Node)
    json[:additionalProperties] = options[:additional_properties].as_json
  else
    json[:additionalProperties] = false
  end

  required_properties = @properties.values.select(&:required?).map(&:name)

  if required_properties.any?
    json[:required] = required_properties
  end

  return process_json(ATTRIBUTES, json)
end

#cast(data) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/schemacop/v3/hash_node.rb', line 171

def cast(data)
  result = {}.with_indifferent_access
  data ||= default
  return nil if data.nil?

  data_hash = data.dup.with_indifferent_access

  property_patterns = {}
  as_names = []

  @properties.each_value do |prop|
    if prop.name.is_a?(Regexp)
      property_patterns[prop.name] = prop
      next
    end

    as_names << prop.as&.to_s if prop.as.present?

    prop_name = prop.as&.to_s || prop.name

    casted_data = prop.cast(data_hash[prop.name])

    if !casted_data.nil? || data_hash.include?(prop.name)
      result[prop_name] = casted_data
    end

    if result[prop_name].nil? && !data_hash.include?(prop.name) && !as_names.include?(prop.name)
      result.delete(prop_name)
    end
  end

  # Handle regex properties
  specified_properties = @properties.keys.to_set
  additional_properties = data_hash.reject { |k, _v| specified_properties.include?(k.to_s.to_sym) }

  if additional_properties.any? && property_patterns.any?
    additional_properties.each do |name, additional_property|
      match_key = property_patterns.keys.find { |p| p.match?(name.to_s) }
      match = property_patterns[match_key]
      result[name] = match.cast(additional_property)
    end
  end

  # Handle additional properties
  if options[:additional_properties].is_a?(TrueClass)
    result = data_hash.merge(result)
  elsif options[:additional_properties].is_a?(Node)
    specified_properties = @properties.keys.to_set
    additional_properties = data_hash.reject { |k, _v| specified_properties.include?(k.to_s.to_sym) }
    if additional_properties.any?
      additional_properties_result = {}
      additional_properties.each do |key, value|
        additional_properties_result[key] = options[:additional_properties].cast(value)
      end
      result = additional_properties_result.merge(result)
    end
  end

  return result
end

#childrenObject



167
168
169
# File 'lib/schemacop/v3/hash_node.rb', line 167

def children
  @properties.values
end

#dsl_add(type, **options, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/schemacop/v3/hash_node.rb', line 32

def dsl_add(type, **options, &block)
  if @options[:additional_properties].is_a?(Node)
    fail Exceptions::InvalidSchemaError, 'You can only use "add" once to specify additional properties.'
  end

  @options[:additional_properties] = create(type, **options, &block)
end

#dsl_dep(source, *targets, **_kwargs) ⇒ Object



40
41
42
43
# File 'lib/schemacop/v3/hash_node.rb', line 40

def dsl_dep(source, *targets, **_kwargs)
  @options[:dependencies] ||= {}
  @options[:dependencies][source] = targets
end