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

Constant Summary collapse

TYPE_MAP =
{
  "array"   => Array,
  "boolean" => [FalseClass, TrueClass],
  "integer" => Integer,
  "number"  => [Integer, Float],
  "null"    => NilClass,
  "object"  => Hash,
  "string"  => String,
}

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.



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

Instance Attribute Details

#fragmentObject

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



31
32
33
# File 'lib/json_schema/schema.rb', line 31

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.



35
36
37
# File 'lib/json_schema/schema.rb', line 35

def reference
  @reference
end

Instance Method Details

#expand_references(options = {}) ⇒ Object



199
200
201
202
203
204
205
206
# 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 = {}) ⇒ Object



208
209
210
211
# File 'lib/json_schema/schema.rb', line 208

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

#inspectObject



221
222
223
# File 'lib/json_schema/schema.rb', line 221

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

#inspect_schemaObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# 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) ⇒ Object



254
255
256
257
258
259
260
# 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?Boolean

Returns:

  • (Boolean)


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

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

#pointerObject



266
267
268
269
270
271
272
# File 'lib/json_schema/schema.rb', line 266

def pointer
  if parent
    (parent.pointer + "/".freeze + fragment).freeze
  else
    fragment
  end
end

#type_parsedObject

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

Type: Array



217
218
219
# 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) ⇒ Object



274
275
276
277
278
# 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) ⇒ Object



280
281
282
# File 'lib/json_schema/schema.rb', line 280

def validate!(data, fail_fast: false)
  Validator.new(self).validate!(data, fail_fast: fail_fast)
end