Class: JSON::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/json-schema/schema.rb,
lib/json-schema/attribute.rb,
lib/json-schema/attributes/not.rb,
lib/json-schema/attributes/ref.rb,
lib/json-schema/attributes/enum.rb,
lib/json-schema/attributes/type.rb,
lib/json-schema/attributes/allof.rb,
lib/json-schema/attributes/anyof.rb,
lib/json-schema/attributes/items.rb,
lib/json-schema/attributes/oneof.rb,
lib/json-schema/schema/validator.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/validators/draft4.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/type_v4.rb,
lib/json-schema/attributes/disallow.rb,
lib/json-schema/attributes/maxitems.rb,
lib/json-schema/attributes/minitems.rb,
lib/json-schema/attributes/required.rb,
lib/json-schema/errors/schema_error.rb,
lib/json-schema/attributes/maxlength.rb,
lib/json-schema/attributes/minlength.rb,
lib/json-schema/attributes/maxdecimal.rb,
lib/json-schema/attributes/multipleof.rb,
lib/json-schema/attributes/properties.rb,
lib/json-schema/attributes/divisibleby.rb,
lib/json-schema/attributes/formats/ip4.rb,
lib/json-schema/attributes/formats/ip6.rb,
lib/json-schema/attributes/formats/uri.rb,
lib/json-schema/attributes/uniqueitems.rb,
lib/json-schema/attributes/dependencies.rb,
lib/json-schema/attributes/formats/date.rb,
lib/json-schema/attributes/formats/time.rb,
lib/json-schema/errors/json_parse_error.rb,
lib/json-schema/errors/validation_error.rb,
lib/json-schema/validators/hyper-draft4.rb,
lib/json-schema/attributes/maxproperties.rb,
lib/json-schema/attributes/minproperties.rb,
lib/json-schema/attributes/properties_v4.rb,
lib/json-schema/attributes/formats/custom.rb,
lib/json-schema/attributes/additionalitems.rb,
lib/json-schema/attributes/dependencies_v4.rb,
lib/json-schema/errors/custom_format_error.rb,
lib/json-schema/attributes/formats/date_time.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,
lib/json-schema/attributes/formats/date_time_v4.rb

Defined Under Namespace

Classes: AdditionalItemsAttribute, AdditionalPropertiesAttribute, AllOfAttribute, AnyOfAttribute, Attribute, CustomFormat, CustomFormatError, DateFormat, DateTimeFormat, DateTimeV4Format, DependenciesAttribute, DependenciesV4Attribute, DisallowAttribute, DivisibleByAttribute, Draft1, Draft2, Draft3, Draft4, EnumAttribute, ExtendsAttribute, FormatAttribute, HyperDraft4, IP4Format, IP6Format, ItemsAttribute, JsonParseError, MaxDecimalAttribute, MaxItemsAttribute, MaxLengthAttribute, MaxPropertiesAttribute, MaximumAttribute, MaximumInclusiveAttribute, MinItemsAttribute, MinLengthAttribute, MinPropertiesAttribute, MinimumAttribute, MinimumInclusiveAttribute, MultipleOfAttribute, NotAttribute, OneOfAttribute, PatternAttribute, PatternPropertiesAttribute, PropertiesAttribute, PropertiesOptionalAttribute, PropertiesV4Attribute, RefAttribute, RequiredAttribute, SchemaError, TimeFormat, TypeAttribute, TypeV4Attribute, UniqueItemsAttribute, UriFormat, ValidationError, Validator

Instance Attribute Summary collapse

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

def initialize(schema,uri,parent_validator=nil)
  @schema = schema
  @uri = uri

  self.class.add_indifferent_access(@schema)

  # If there is an ID on this schema, use it to generate the URI
  if @schema['id'] && @schema['id'].kind_of?(String)
    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']
    @validator = JSON::Validator.validator_for(@schema['$schema'])
  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

Class Method Details

.add_indifferent_access(schema) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/json-schema/schema.rb', line 39

def self.add_indifferent_access(schema)
  if schema.is_a?(Hash)
    schema.default_proc = proc do |hash,key|
      if hash.has_key?(key)
        hash[key]
      else
        key = case key
        when Symbol then key.to_s
        when String then key.to_sym
        end
        hash.has_key?(key) ? hash[key] : nil
      end
    end
    schema.keys.each do |key|
      add_indifferent_access(schema[key])
    end
  elsif schema.is_a?(Array)
    schema.each do |schema_item|
      add_indifferent_access(schema_item)
    end
  end
end

Instance Method Details

#base_uriObject



62
63
64
65
66
# File 'lib/json-schema/schema.rb', line 62

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

#to_sObject



68
69
70
# File 'lib/json-schema/schema.rb', line 68

def to_s
  @schema.to_json
end

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



35
36
37
# File 'lib/json-schema/schema.rb', line 35

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