Class: JSONSchemer::Draft201909::Vocab::Applicator::Items

Inherits:
Keyword
  • Object
show all
Defined in:
lib/json_schemer/draft201909/vocab/applicator.rb

Constant Summary

Constants included from Output

Output::FRAGMENT_ENCODE_REGEX

Instance Attribute Summary

Attributes inherited from Keyword

#parent, #parsed, #root, #value

Attributes included from Output

#keyword, #schema

Instance Method Summary collapse

Methods inherited from Keyword

#absolute_keyword_location, #error_key, #fetch, #initialize, #parsed_schema, #schema_pointer

Methods included from Output

#x_error

Constructor Details

This class inherits a constructor from JSONSchemer::Keyword

Instance Method Details

#error(formatted_instance_location:) ⇒ Object



7
8
9
# File 'lib/json_schemer/draft201909/vocab/applicator.rb', line 7

def error(formatted_instance_location:, **)
  "array items at #{formatted_instance_location} do not match `items` schema(s)"
end

#parseObject



11
12
13
14
15
16
17
18
19
# File 'lib/json_schemer/draft201909/vocab/applicator.rb', line 11

def parse
  if value.is_a?(Array)
    value.map.with_index do |subschema, index|
      subschema(subschema, index.to_s)
    end
  else
    subschema(value)
  end
end

#validate(instance, instance_location, keyword_location, context) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/json_schemer/draft201909/vocab/applicator.rb', line 21

def validate(instance, instance_location, keyword_location, context)
  return result(instance, instance_location, keyword_location, true) unless instance.is_a?(Array)

  nested = if parsed.is_a?(Array)
    instance.take(parsed.size).map.with_index do |item, index|
      parsed.fetch(index).validate_instance(item, join_location(instance_location, index.to_s), join_location(keyword_location, index.to_s), context)
    end
  else
    instance.map.with_index do |item, index|
      parsed.validate_instance(item, join_location(instance_location, index.to_s), keyword_location, context)
    end
  end

  result(instance, instance_location, keyword_location, nested.all?(&:valid), nested, :annotation => (nested.size - 1))
end