Class: JsonSchema::Schema

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

Defined Under Namespace

Classes: Link, Media

Constant Summary collapse

@@copyable =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



18
19
20
# File 'lib/json_schema/schema.rb', line 18

def initialize
  @clones = Set.new
end

Instance Attribute Details

#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.



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

def reference
  @reference
end

Class Method Details

.attr_copyable(attr) ⇒ Object

identical to attr_accessible, but allows us to copy in values from a target schema to help preserve our hierarchy during reference expansion



9
10
11
12
# File 'lib/json_schema/schema.rb', line 9

def self.attr_copyable(attr)
  attr_accessor(attr)
  @@copyable << "@#{attr}".to_sym
end

.attr_reader_default(attr, default) ⇒ Object



14
15
16
# File 'lib/json_schema/schema.rb', line 14

def self.attr_reader_default(attr, default)
  class_eval("def #{attr} ; !@#{attr}.nil? ? @#{attr} : #{default} ; end")
end

Instance Method Details

#copy_from(schema) ⇒ Object



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

def copy_from(schema)
  @@copyable.each do |copyable|
    instance_variable_set(copyable, schema.instance_variable_get(copyable))
  end
end

#expand_referencesObject



197
198
199
200
201
202
203
204
# File 'lib/json_schema/schema.rb', line 197

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

#expand_references!Object



206
207
208
209
# File 'lib/json_schema/schema.rb', line 206

def expand_references!
  ReferenceExpander.new.expand!(self)
  true
end

#inspectObject



211
212
213
214
215
# File 'lib/json_schema/schema.rb', line 211

def inspect
  str = inspect_schema
  str = JSON.pretty_generate(str).gsub(/\\?"/, '') if str.is_a?(Hash)
  "\#<JsonSchema::Schema #{str}>"
end

#inspect_schemaObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/json_schema/schema.rb', line 217

def inspect_schema
  if reference
    str = reference.to_s
    str += " [EXPANDED]" if expanded?
    str += " [CLONE]" if !original?
    str
  else
    hash = {}
    @@copyable.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



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

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

#original?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/json_schema/schema.rb', line 254

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

#validate(data) ⇒ Object



258
259
260
261
262
# File 'lib/json_schema/schema.rb', line 258

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

#validate!(data) ⇒ Object



264
265
266
# File 'lib/json_schema/schema.rb', line 264

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