Class: JSON::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/json-schema/schema.rb,
lib/json-schema/validator.rb,
lib/json-schema/attributes/ref.rb,
lib/json-schema/attributes/enum.rb,
lib/json-schema/attributes/type.rb,
lib/json-schema/attributes/items.rb,
lib/json-schema/attributes/format.rb,
lib/json-schema/validators/draft3.rb,
lib/json-schema/attributes/extends.rb,
lib/json-schema/attributes/maximum.rb,
lib/json-schema/attributes/minimum.rb,
lib/json-schema/attributes/pattern.rb,
lib/json-schema/attributes/disallow.rb,
lib/json-schema/attributes/maxitems.rb,
lib/json-schema/attributes/minitems.rb,
lib/json-schema/attributes/maxlength.rb,
lib/json-schema/attributes/minlength.rb,
lib/json-schema/attributes/properties.rb,
lib/json-schema/attributes/divisibleby.rb,
lib/json-schema/attributes/uniqueitems.rb,
lib/json-schema/attributes/dependencies.rb,
lib/json-schema/attributes/additionalitems.rb,
lib/json-schema/attributes/patternproperties.rb,
lib/json-schema/attributes/additionalproperties.rb

Defined Under Namespace

Classes: AdditionalItemsAttribute, AdditionalPropertiesAttribute, Attribute, DependenciesAttribute, DisallowAttribute, DivisibleByAttribute, Draft3, EnumAttribute, ExtendsAttribute, FormatAttribute, ItemsAttribute, MaxItemsAttribute, MaxLengthAttribute, MaximumAttribute, MinItemsAttribute, MinLengthAttribute, MinimumAttribute, PatternAttribute, PatternPropertiesAttribute, PropertiesAttribute, RefAttribute, SchemaError, TypeAttribute, UniqueItemsAttribute, ValidationError, Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, uri, parent_validator = nil) ⇒ Schema

Returns a new instance of Schema.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json-schema/schema.rb', line 10

def initialize(schema,uri,parent_validator=nil)
  @schema = schema
  @uri = uri
  
  # If there is an ID on this schema, use it to generate the URI
  if @schema['id']
    temp_uri = URI.parse(@schema['id'])
    if temp_uri.relative?
      uri.path = (Pathname.new(uri.path).parent + @schema['id']).cleanpath.to_s
      temp_uri = uri
    end
    @uri = temp_uri
  end
  @uri.fragment = nil
  
  # If there is a $schema on this schema, use it to determine which validator to use
  if @schema['$schema']
    u = URI.parse(@schema['$schema'])
    @validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"]
    if @validator.nil?
      raise SchemaError.new("This library does not have support for schemas defined by #{u.scheme}://#{u.host}#{u.path}")
    end
  elsif parent_validator
    @validator = parent_validator
  else
    @validator = JSON::Validator.default_validator
  end  
end

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



8
9
10
# File 'lib/json-schema/schema.rb', line 8

def schema
  @schema
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/json-schema/schema.rb', line 8

def uri
  @uri
end

#validatorObject

Returns the value of attribute validator.



8
9
10
# File 'lib/json-schema/schema.rb', line 8

def validator
  @validator
end

Instance Method Details

#base_uriObject



43
44
45
46
47
# File 'lib/json-schema/schema.rb', line 43

def base_uri
  parts = @uri.to_s.split('/')
  parts.pop
  parts.join('/') + '/'
end

#to_sObject



49
50
51
# File 'lib/json-schema/schema.rb', line 49

def to_s
  @schema.to_json
end

#validate(data, fragments) ⇒ Object



39
40
41
# File 'lib/json-schema/schema.rb', line 39

def validate(data, fragments)
  @validator.validate(self, data, fragments)
end