Class: JsonSchema::Schema

Inherits:
Object
  • Object
show all
Includes:
Attributes
Defined in:
lib/json_schema/schema.rb

Direct Known Subclasses

Link

Defined Under Namespace

Classes: Link, Media

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes

#[], #copy_from, included, #initialize_attrs

Constructor Details

#initializeSchema

Returns a new instance of Schema.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/json_schema/schema.rb', line 7

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

Instance Attribute Details

#fragmentObject

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



21
22
23
# File 'lib/json_schema/schema.rb', line 21

def fragment
  @fragment
end

#referenceObject

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.



25
26
27
# File 'lib/json_schema/schema.rb', line 25

def reference
  @reference
end

Instance Method Details

#expand_references(options = {}) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/json_schema/schema.rb', line 188

def expand_references(options = {})
  expander = ReferenceExpander.new
  if expander.expand(self, options)
    [true, nil]
  else
    [false, expander.errors]
  end
end

#expand_references!(options = {}) ⇒ Object



197
198
199
200
# File 'lib/json_schema/schema.rb', line 197

def expand_references!(options = {})
  ReferenceExpander.new.expand!(self, options)
  true
end

#inspectObject



202
203
204
# File 'lib/json_schema/schema.rb', line 202

def inspect
  "\#<JsonSchema::Schema pointer=#{pointer}>"
end

#inspect_schemaObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/json_schema/schema.rb', line 206

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) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/json_schema/schema.rb', line 235

def inspect_value(value)
  if value.is_a?(Schema)
    value.inspect_schema
  else
    value.inspect
  end
end

#original?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/json_schema/schema.rb', line 243

def original?
  !clones.include?(self)
end

#pointerObject



247
248
249
250
251
252
253
# File 'lib/json_schema/schema.rb', line 247

def pointer
  if parent
    parent.pointer + "/" + fragment
  else
    fragment
  end
end

#validate(data) ⇒ Object



255
256
257
258
259
# File 'lib/json_schema/schema.rb', line 255

def validate(data)
  validator = Validator.new(self)
  valid = validator.validate(data)
  [valid, validator.errors]
end

#validate!(data) ⇒ Object



261
262
263
# File 'lib/json_schema/schema.rb', line 261

def validate!(data)
  Validator.new(self).validate!(data)
end