Class: JsonSchema::Schema
- Inherits:
-
Object
- Object
- JsonSchema::Schema
- Defined in:
- lib/json_schema/schema.rb
Defined Under Namespace
Constant Summary collapse
- @@copyable_attrs =
[]
- @@schema_attrs =
Attributes that are part of the JSON schema and hyper-schema specifications. These are allowed to be accessed with the [] operator.
Hash contains the access key mapped to the name of the method that should be invoked to retrieve a value. For example, ‘type` maps to `type` and `additionalItems` maps to `additional_items`.
{}
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Fragment of a JSON Pointer that can help us build a pointer back to this schema for debugging.
-
#reference ⇒ Object
Rather than a normal schema, the node may be a JSON Reference.
Class Method Summary collapse
-
.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.
- .attr_reader_default(attr, default) ⇒ Object
- .attr_schema(attr, options = {}) ⇒ Object
Instance Method Summary collapse
-
#[](name) ⇒ Object
Allows the values of schema attributes to be accessed with a symbol or a string.
- #copy_from(schema) ⇒ Object
- #expand_references(options = {}) ⇒ Object
- #expand_references!(options = {}) ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #inspect ⇒ Object
- #inspect_schema ⇒ Object
- #inspect_value(value) ⇒ Object
- #original? ⇒ Boolean
- #pointer ⇒ Object
- #validate(data) ⇒ Object
- #validate!(data) ⇒ Object
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
34 35 36 |
# File 'lib/json_schema/schema.rb', line 34 def initialize @clones = Set.new end |
Instance Attribute Details
#fragment ⇒ Object
Fragment of a JSON Pointer that can help us build a pointer back to this schema for debugging.
40 41 42 |
# File 'lib/json_schema/schema.rb', line 40 def fragment @fragment end |
#reference ⇒ Object
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.
44 45 46 |
# File 'lib/json_schema/schema.rb', line 44 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
17 18 19 20 |
# File 'lib/json_schema/schema.rb', line 17 def self.attr_copyable(attr) attr_accessor(attr) @@copyable_attrs << "@#{attr}".to_sym end |
.attr_reader_default(attr, default) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/json_schema/schema.rb', line 27 def self.attr_reader_default(attr, default) # remove the reader already created by attr_accessor remove_method(attr) class_eval("def #{attr} ; !@#{attr}.nil? ? @#{attr} : #{default} ; end") end |
.attr_schema(attr, options = {}) ⇒ Object
22 23 24 25 |
# File 'lib/json_schema/schema.rb', line 22 def self.attr_schema(attr, = {}) attr_copyable(attr) @@schema_attrs[[:schema_name] || attr] = attr end |
Instance Method Details
#[](name) ⇒ Object
Allows the values of schema attributes to be accessed with a symbol or a string. So for example, the value of ‘schema.additional_items` could be procured with `schema`. This only works for attributes that are part of the JSON schema specification; other methods on the class are not available (e.g. `expanded`.)
This is implemented so that ‘JsonPointer::Evaluator` can evaluate a reference on an sintance of this class (as well as plain JSON data).
221 222 223 224 225 226 227 228 |
# File 'lib/json_schema/schema.rb', line 221 def [](name) name = name.to_sym if @@schema_attrs.key?(name) send(@@schema_attrs[name]) else raise NoMethodError, "Schema does not respond to ##{name}" end end |
#copy_from(schema) ⇒ Object
230 231 232 233 234 |
# File 'lib/json_schema/schema.rb', line 230 def copy_from(schema) @@copyable_attrs.each do |copyable| instance_variable_set(copyable, schema.instance_variable_get(copyable)) end end |
#expand_references(options = {}) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/json_schema/schema.rb', line 236 def ( = {}) = ReferenceExpander.new if .(self, ) [true, nil] else [false, .errors] end end |
#expand_references!(options = {}) ⇒ Object
245 246 247 248 |
# File 'lib/json_schema/schema.rb', line 245 def ( = {}) ReferenceExpander.new.(self, ) true end |
#inspect ⇒ Object
250 251 252 |
# File 'lib/json_schema/schema.rb', line 250 def inspect "\#<JsonSchema::Schema pointer=#{pointer}>" end |
#inspect_schema ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/json_schema/schema.rb', line 254 def inspect_schema if reference str = reference.to_s str += ? " [EXPANDED]" : " [COLLAPSED]" str += original? ? " [ORIGINAL]" : " [CLONE]" str else hash = {} @@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
283 284 285 286 287 288 289 |
# File 'lib/json_schema/schema.rb', line 283 def inspect_value(value) if value.is_a?(Schema) value.inspect_schema else value.inspect end end |
#original? ⇒ Boolean
291 292 293 |
# File 'lib/json_schema/schema.rb', line 291 def original? !clones.include?(self) end |
#pointer ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/json_schema/schema.rb', line 295 def pointer if parent parent.pointer + "/" + fragment else fragment end end |
#validate(data) ⇒ Object
303 304 305 306 307 |
# File 'lib/json_schema/schema.rb', line 303 def validate(data) validator = Validator.new(self) valid = validator.validate(data) [valid, validator.errors] end |
#validate!(data) ⇒ Object
309 310 311 |
# File 'lib/json_schema/schema.rb', line 309 def validate!(data) Validator.new(self).validate!(data) end |