Class: SimpleJSONSchema::Scope

Inherits:
Object
  • Object
show all
Extended by:
Concerns::HashAccessor
Defined in:
lib/simple_json_schema/scope.rb

Constant Summary collapse

CLASS_TYPE_TRANSLATE =
{
  Array => 'array',
  Float => 'number',
  Hash => 'object',
  Integer => 'integer',
  Numeric => 'number',
  String => 'string'
}.freeze

Instance Method Summary collapse

Methods included from Concerns::HashAccessor

hash_accessor

Constructor Details

#initialize(**args) ⇒ Scope

Returns a new instance of Scope.



18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#[](key) ⇒ Object



102
103
104
105
106
# File 'lib/simple_json_schema/scope.rb', line 102

def [](key)
  return unless segment.is_a?(Hash)

  segment[key]
end

#around_hooksObject



116
117
118
119
120
# 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

#cacheObject



112
113
114
# File 'lib/simple_json_schema/scope.rb', line 112

def cache
  options[:cache] ||= Cache.new
end

#error(type, details = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

#idObject



98
99
100
# File 'lib/simple_json_schema/scope.rb', line 98

def id
  self[:$id]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/simple_json_schema/scope.rb', line 108

def key?(key)
  segment.is_a?(Hash) && segment.key?(key)
end

#loaded_idsObject



122
123
124
125
126
127
128
# 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) ⇒ Object



38
39
40
41
42
43
# 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_hooksObject



66
67
68
69
# 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) ⇒ Object



29
30
31
32
33
34
35
36
# 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) ⇒ Object



45
46
47
48
49
50
# 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

#segmentObject



81
82
83
# File 'lib/simple_json_schema/scope.rb', line 81

def segment
  dig(schema, schema_paths)
end

#segment=(new_segment) ⇒ Object



85
86
87
# 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?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
# File 'lib/simple_json_schema/scope.rb', line 89

def segment?
  if segment == false
    error('schema')
    return false
  end

  !(segment == true || segment.nil?)
end

#valueObject



71
72
73
# File 'lib/simple_json_schema/scope.rb', line 71

def value
  dig(data, data_paths)
end

#value=(new_value) ⇒ Object



75
76
77
78
79
# 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