Class: JSchema::Schema

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

Constant Summary collapse

VERSION_ID =
'http://json-schema.org/draft-04/schema#'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, uri, parent) ⇒ Schema

Returns a new instance of Schema.



45
46
47
48
49
50
# File 'lib/jschema/schema.rb', line 45

def initialize(schema, uri, parent)
  @uri = uri
  @parent = parent
  @schema = schema
  @validators = Validator.build(schema, self)
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



43
44
45
# File 'lib/jschema/schema.rb', line 43

def parent
  @parent
end

#uriObject (readonly)

Returns the value of attribute uri.



43
44
45
# File 'lib/jschema/schema.rb', line 43

def uri
  @uri
end

Class Method Details

.build(sch = {}, parent = nil, id = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jschema/schema.rb', line 6

def build(sch = {}, parent = nil, id = nil)
  schema = sch || {}

  check_schema_version schema

  if (json_reference = schema['$ref'])
    unescaped_ref = json_reference.gsub(/~1|~0/, '~1' => '/', '~0' => '~')
    SchemaRef.new(URI(unescaped_ref), parent)
  else
    uri = SchemaURI.build(schema['id'], parent, id)
    parent && JSONReference.dereference(uri, parent) || begin
      jschema = new(schema, uri, parent)
      register_definitions schema, jschema
      JSONReference.register_schema jschema
    end
  end
end

Instance Method Details

#fragment(path) ⇒ Object



62
63
64
# File 'lib/jschema/schema.rb', line 62

def fragment(path)
  JSchema::JSONReference.dereference(URI.parse(path), self)
end

#to_sObject



66
67
68
# File 'lib/jschema/schema.rb', line 66

def to_s
  uri.to_s
end

#valid?(instance) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/jschema/schema.rb', line 52

def valid?(instance)
  validate(instance).empty?
end

#validate(instance) ⇒ Object



56
57
58
59
60
# File 'lib/jschema/schema.rb', line 56

def validate(instance)
  @validators.map do |validator|
    validator.validate(instance)
  end.compact
end