class SimpleJSONSchema::Scope

Constants

CLASS_TYPE_TRANSLATE

Public Class Methods

new(**args) click to toggle source
# File lib/simple_json_schema/scope.rb, line 18
def initialize(**args)
  scope.merge!(args)

  self.data_paths ||= []
  self.schema_paths ||= []
  self.errors ||= []
  self.ids ||= {}
  self.options ||= {}
  self.type ||= evaluate_type
end

Public Instance Methods

[](key) click to toggle source
# File lib/simple_json_schema/scope.rb, line 102
def [](key)
  return unless segment.is_a?(Hash)

  segment[key]
end
around_hooks() { || ... } click to toggle source
# File lib/simple_json_schema/scope.rb, line 116
def around_hooks
  options[:before_property_validation]&.call(self)
  yield
  options[:after_property_validation]&.call(self)
end
cache() click to toggle source
# File lib/simple_json_schema/scope.rb, line 112
def cache
  options[:cache] ||= Cache.new
end
error(type, details = nil) click to toggle source
# File lib/simple_json_schema/scope.rb, line 52
def error(type, details = nil)
  error = {
    type: type,
    segment: segment,
    value: value,
    data_pointer: "/#{data_paths.join('/')}",
    schema_pointer: "/#{schema_paths.join('/')}"
  }

  error[:details] = details if details

  errors.push(error)
end
id() click to toggle source
# File lib/simple_json_schema/scope.rb, line 98
def id
  self[:$id]
end
key?(key) click to toggle source
# File lib/simple_json_schema/scope.rb, line 108
def key?(key)
  segment.is_a?(Hash) && segment.key?(key)
end
loaded_ids() click to toggle source
# File lib/simple_json_schema/scope.rb, line 122
def loaded_ids
  if ids.empty?
    resolve_ids(ids, schema)
    ids[:$loaded] = true
  end
  ids
end
merge(data_paths: self.data_paths, schema_paths: self.schema_paths, type: nil, parent_uri: nil) click to toggle source
# File lib/simple_json_schema/scope.rb, line 38
def merge(data_paths: self.data_paths, schema_paths: self.schema_paths, type: nil, parent_uri: nil)
  self.class.new(**scope.merge(data_paths: data_paths,
                               schema_paths: schema_paths,
                               type: type,
                               parent_uri: parent_uri || URIExtender.join_uri(self.parent_uri, id)))
end
no_hooks() click to toggle source
# File lib/simple_json_schema/scope.rb, line 66
def no_hooks
  self.options = options.except(:before_property_validation, :after_property_validation)
  self
end
path_to(data_path: nil, schema_path: nil, type: nil) click to toggle source
# File lib/simple_json_schema/scope.rb, line 29
def path_to(data_path: nil, schema_path: nil, type: nil)
  return self if data_path.nil? && schema_path.nil? && type.nil?

  new_data_paths = data_paths + [data_path].flatten.compact
  new_schema_paths = schema_paths + [schema_path].flatten.compact

  merge(data_paths: new_data_paths, schema_paths: new_schema_paths, type: type)
end
replace_data(new_data) click to toggle source
# File lib/simple_json_schema/scope.rb, line 45
def replace_data(new_data)
  self.data = new_data
  self.data_paths = []
  self.type = evaluate_type
  self
end
segment() click to toggle source
# File lib/simple_json_schema/scope.rb, line 81
def segment
  dig(schema, schema_paths)
end
segment=(new_segment) click to toggle source
# File lib/simple_json_schema/scope.rb, line 85
def segment=(new_segment)
  replace(schema, schema_paths, new_segment) { self.schema = new_segment }
end
segment?() click to toggle source
# File lib/simple_json_schema/scope.rb, line 89
def segment?
  if segment == false
    error('schema')
    return false
  end

  !(segment == true || segment.nil?)
end
value() click to toggle source
# File lib/simple_json_schema/scope.rb, line 71
def value
  dig(data, data_paths)
end
value=(new_value) click to toggle source
# File lib/simple_json_schema/scope.rb, line 75
def value=(new_value)
  return if errors.any? # only convert value until be invalid.

  replace(data, data_paths, new_value) { self.data = new_value }
end

Private Instance Methods

dig(hash, paths) click to toggle source
# File lib/simple_json_schema/scope.rb, line 140
def dig(hash, paths)
  return hash if paths.empty?

  hash.dig(*paths) if hash.respond_to?(:dig)
rescue TypeError
  nil
end
evaluate_type() click to toggle source
# File lib/simple_json_schema/scope.rb, line 132
def evaluate_type
  if key?(:type)
    self[:type]
  else
    CLASS_TYPE_TRANSLATE[value.class]
  end
end
replace(hash, paths, value) { || ... } click to toggle source
# File lib/simple_json_schema/scope.rb, line 148
def replace(hash, paths, value)
  *steps, leaf = paths

  if steps.empty?
    if leaf.nil?
      yield if block_given?
    else
      hash[leaf] = value
    end
  else
    hash.dig(*steps)[leaf] = value
  end
end
resolve_ids(ids, schema, parent_uri = nil, schema_paths = []) click to toggle source
# File lib/simple_json_schema/scope.rb, line 162
def resolve_ids(ids, schema, parent_uri = nil, schema_paths = [])
  case schema
  when Array
    schema.each_with_index { |subschema, index| resolve_ids(ids, subschema, parent_uri, schema_paths + [index]) }
  when Hash
    uri = URIExtender.join_uri(parent_uri, schema[:$id])

    schema.each do |key, value|
      ids[uri.to_s] = { schema: schema, schema_paths: schema_paths } if key == '$id' && uri != parent_uri

      resolve_ids(ids, value, uri, schema_paths + [key])
    end
  end
end