File: schema.rb

package info (click to toggle)
ruby-json-schema 6.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 472 kB
  • sloc: ruby: 2,494; makefile: 4
file content (61 lines) | stat: -rw-r--r-- 1,748 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
57
58
59
60
61
require 'pathname'

module JSON
  class Schema
    attr_accessor :schema, :uri, :validator

    def initialize(schema, uri, parent_validator = nil)
      @schema = schema
      @uri = uri

      # If there is an ID on this schema, use it to generate the URI
      if @schema['id'].is_a?(String)
        temp_uri = JSON::Util::URI.parse(@schema['id'])
        if temp_uri.relative?
          temp_uri = uri.join(temp_uri)
        end
        @uri = temp_uri
      end
      @uri = JSON::Util::URI.strip_fragment(@uri)

      # If there is a $schema on this schema, use it to determine which validator to use
      @validator = if @schema['$schema']
                     JSON::Validator.validator_for_uri(@schema['$schema'])
                   elsif parent_validator
                     parent_validator
                   else
                     JSON::Validator.default_validator
                   end
    end

    def validate(data, fragments, processor, options = {})
      @validator.validate(self, data, fragments, processor, options)
    end

    def self.stringify(schema)
      case schema
      when Hash
        schema.map { |key, _value| [key.to_s, stringify(schema[key])] }.to_h
      when Array
        schema.map do |schema_item|
          stringify(schema_item)
        end
      when Symbol
        schema.to_s
      else
        schema
      end
    end

    # @return [JSON::Schema] a new schema matching an array whose items all match this schema.
    def to_array_schema
      array_schema = { 'type' => 'array', 'items' => schema }
      array_schema['$schema'] = schema['$schema'] unless schema['$schema'].nil?
      self.class.new(array_schema, uri, validator)
    end

    def to_s
      @schema.to_json
    end
  end
end