Class: JSONSkooma::Keywords::Applicator::PatternProperties

Inherits:
Base
  • Object
show all
Defined in:
lib/json_skooma/keywords/applicator/pattern_properties.rb

Instance Attribute Summary

Attributes inherited from Base

#json, #parent_schema

Instance Method Summary collapse

Methods inherited from Base

#each_schema, inherited, #instance_types, #key, #resolve, set_defaults, #static, value_schema=

Constructor Details

#initialize(parent_schema, value) ⇒ PatternProperties

Returns a new instance of PatternProperties.



11
12
13
14
15
16
# File 'lib/json_skooma/keywords/applicator/pattern_properties.rb', line 11

def initialize(parent_schema, value)
  super
  @patterned = json.map do |pattern, subschema|
    [Regexp.new(pattern), pattern, subschema]
  end
end

Instance Method Details

#evaluate(instance, result) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/json_skooma/keywords/applicator/pattern_properties.rb', line 18

def evaluate(instance, result)
  matched_names = Set.new
  err_names = []

  instance.each do |name, item|
    @patterned.each do |regexp, pattern, subschema|
      if regexp.match?(name)
        result.call(item, pattern) do |subresult|
          subschema.evaluate(item, subresult)
          if subresult.passed?
            matched_names << name
          else
            err_names << name
          end
        end
      end
    end
  end

  if err_names.any?
    result.failure("Properties #{err_names.join(", ")} are invalid")
  else
    result.annotate(matched_names.to_a)
  end
end