class JsonSchema::Schema

Constants

TYPE_MAP

Attributes

fragment[RW]

Fragment of a JSON Pointer that can help us build a pointer back to this schema for debugging.

reference[RW]

Rather than a normal schema, the node may be a JSON Reference. In this case, no other attributes will be filled in except for parent.

Public Class Methods

new() click to toggle source
# File lib/json_schema/schema.rb, line 17
def initialize
  # nil out all our fields so that it's possible to instantiate a schema
  # instance without going through the parser and validate against it
  # without Ruby throwing warnings about uninitialized instance variables.
  initialize_attrs

  # Don't put this in as an attribute default. We require that this precise
  # pointer gets copied between all clones of any given schema so that they
  # all share exactly the same set.
  @clones = Set.new
end

Public Instance Methods

expand_references(options = {}) click to toggle source
# File lib/json_schema/schema.rb, line 199
def expand_references(options = {})
  expander = ReferenceExpander.new
  if expander.expand(self, options)
    [true, nil]
  else
    [false, expander.errors]
  end
end
expand_references!(options = {}) click to toggle source
# File lib/json_schema/schema.rb, line 208
def expand_references!(options = {})
  ReferenceExpander.new.expand!(self, options)
  true
end
inspect() click to toggle source
# File lib/json_schema/schema.rb, line 221
def inspect
  "\#<JsonSchema::Schema pointer=#{pointer}>"
end
inspect_schema() click to toggle source
# File lib/json_schema/schema.rb, line 225
def inspect_schema
  if reference
    str = reference.to_s
    str += expanded? ? " [EXPANDED]" : " [COLLAPSED]"
    str += original? ? " [ORIGINAL]" : " [CLONE]"
    str
  else
    hash = {}
    self.class.copyable_attrs.each do |copyable, _|
      next if [:@clones, :@data, :@parent, :@uri].include?(copyable)
      if value = instance_variable_get(copyable)
        if value.is_a?(Array)
          if !value.empty?
            hash[copyable] = value.map { |v| inspect_value(v) }
          end
        elsif value.is_a?(Hash)
          if !value.empty?
            hash[copyable] =
              Hash[*value.map { |k, v| [k, inspect_value(v)] }.flatten]
          end
        else
          hash[copyable] = inspect_value(value)
        end
      end
    end
    hash
  end
end
inspect_value(value) click to toggle source
# File lib/json_schema/schema.rb, line 254
def inspect_value(value)
  if value.is_a?(Schema)
    value.inspect_schema
  else
    value.inspect
  end
end
original?() click to toggle source
# File lib/json_schema/schema.rb, line 262
def original?
  !clones.include?(self)
end
pointer() click to toggle source
# File lib/json_schema/schema.rb, line 266
def pointer
  if parent
    (parent.pointer + "/".freeze + fragment).freeze
  else
    fragment
  end
end
type_parsed() click to toggle source

An array of Ruby classes that are equivalent to the types defined in the schema.

Type: Array

# File lib/json_schema/schema.rb, line 217
def type_parsed
  @type_parsed ||= type.flat_map { |t| TYPE_MAP[t] }.compact
end
validate(data, fail_fast: false) click to toggle source
# File lib/json_schema/schema.rb, line 274
def validate(data, fail_fast: false)
  validator = Validator.new(self)
  valid = validator.validate(data, fail_fast: fail_fast)
  [valid, validator.errors]
end
validate!(data, fail_fast: false) click to toggle source
# File lib/json_schema/schema.rb, line 280
def validate!(data, fail_fast: false)
  Validator.new(self).validate!(data, fail_fast: fail_fast)
end