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

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

#to_sObject



55
56
57
# File 'lib/jschema/schema.rb', line 55

def to_s
  uri.to_s
end

#valid?(instance) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#validate(instance) ⇒ Object



49
50
51
52
53
# File 'lib/jschema/schema.rb', line 49

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