File: keyword.rb

package info (click to toggle)
ruby-json-schemer 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 7,428; makefile: 4; sh: 4
file content (56 lines) | stat: -rw-r--r-- 1,330 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
module JSONSchemer
  class Keyword
    include Output

    attr_reader :value, :parent, :root, :parsed

    def initialize(value, parent, keyword, schema = parent)
      @value = value
      @parent = parent
      @root = parent.root
      @keyword = keyword
      @schema = schema
      @parsed = parse
    end

    def validate(_instance, _instance_location, _keyword_location, _context)
      nil
    end

    def absolute_keyword_location
      @absolute_keyword_location ||= "#{parent.absolute_keyword_location}/#{fragment_encode(escaped_keyword)}"
    end

    def schema_pointer
      @schema_pointer ||= "#{parent.schema_pointer}/#{escaped_keyword}"
    end

    def error_key
      keyword
    end

    def fetch(key)
      parsed.fetch(parsed.is_a?(Array) ? key.to_i : key)
    end

    def parsed_schema
      parsed.is_a?(Schema) ? parsed : nil
    end

  private

    def parse
      value
    end

    def subschema(value, keyword = nil, **options)
      options[:configuration] ||= schema.configuration
      options[:base_uri] ||= schema.base_uri
      options[:meta_schema] ||= schema.meta_schema
      options[:ref_resolver] ||= schema.ref_resolver
      options[:regexp_resolver] ||= schema.regexp_resolver
      Schema.new(value, self, root, keyword, **options)
    end
  end
end