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/draft1.rb,
lib/json-schema/validators/draft2.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/maxdecimal.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/maximum_inclusive.rb,
lib/json-schema/attributes/minimum_inclusive.rb,
lib/json-schema/attributes/patternproperties.rb,
lib/json-schema/attributes/properties_optional.rb,
lib/json-schema/attributes/additionalproperties.rb

Defined Under Namespace

Classes: AdditionalItemsAttribute, AdditionalPropertiesAttribute, Attribute, DependenciesAttribute, DisallowAttribute, DivisibleByAttribute, Draft1, Draft2, Draft3, EnumAttribute, ExtendsAttribute, FormatAttribute, ItemsAttribute, JsonParseError, MaxDecimalAttribute, MaxItemsAttribute, MaxLengthAttribute, MaximumAttribute, MaximumInclusiveAttribute, MinItemsAttribute, MinLengthAttribute, MinimumAttribute, MinimumInclusiveAttribute, PatternAttribute, PatternPropertiesAttribute, PropertiesAttribute, PropertiesOptionalAttribute, 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.



8
9
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
# File 'lib/json-schema/schema.rb', line 8

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 = uri.merge(@schema['id'])
      temp_uri = uri
    end
    @uri = temp_uri
  end
  @uri.fragment = ''
  
  # 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.



6
7
8
# File 'lib/json-schema/schema.rb', line 6

def schema
  @schema
end

#uriObject

Returns the value of attribute uri.



6
7
8
# File 'lib/json-schema/schema.rb', line 6

def uri
  @uri
end

#validatorObject

Returns the value of attribute validator.



6
7
8
# File 'lib/json-schema/schema.rb', line 6

def validator
  @validator
end

Instance Method Details

#base_uriObject



41
42
43
44
45
# File 'lib/json-schema/schema.rb', line 41

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

#validate(data, fragments, options = {}) ⇒ Object



37
38
39
# File 'lib/json-schema/schema.rb', line 37

def validate(data, fragments, options = {})
  @validator.validate(self, data, fragments, options)
end