Class: JSON::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/json-schema/schema.rb,
lib/json-schema/attribute.rb,
lib/json-schema/schema/reader.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/limit.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/pattern.rb,
lib/json-schema/attributes/type_v4.rb,
lib/json-schema/attributes/disallow.rb,
lib/json-schema/attributes/required.rb,
lib/json-schema/errors/schema_error.rb,
lib/json-schema/attributes/formats/ip.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/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-draft1.rb,
lib/json-schema/validators/hyper-draft2.rb,
lib/json-schema/validators/hyper-draft4.rb,
lib/json-schema/attributes/formats/custom.rb,
lib/json-schema/attributes/additionalitems.rb,
lib/json-schema/errors/custom_format_error.rb,
lib/json-schema/attributes/formats/date_time.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, HyperDraft1, HyperDraft2, HyperDraft4, IP4Format, IP6Format, IPFormat, ItemsAttribute, JsonParseError, LimitAttribute, MaxDecimalAttribute, MaxItemsAttribute, MaxLengthAttribute, MaxPropertiesAttribute, MaximumAttribute, MaximumInclusiveAttribute, MinItemsAttribute, MinLengthAttribute, MinPropertiesAttribute, MinimumAttribute, MinimumInclusiveAttribute, MultipleOfAttribute, NotAttribute, NumericLimitAttribute, OneOfAttribute, PatternAttribute, PatternPropertiesAttribute, PropertiesAttribute, PropertiesOptionalAttribute, PropertiesV4Attribute, ReadRefused, Reader, 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
# 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'] && @schema['id'].kind_of?(String)
    temp_uri = Addressable::URI.parse(@schema['id'])
    if temp_uri.relative?
      temp_uri = uri.join(temp_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

.stringify(schema) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/json-schema/schema.rb', line 36

def self.stringify(schema)
  case schema
  when Hash then
    Hash[schema.map { |key, value| [key.to_s, stringify(schema[key])] }]
  when Array then
    schema.map do |schema_item|
      stringify(schema_item)
    end
  when Symbol then
    schema.to_s
  else
    schema
  end
end

Instance Method Details

#to_array_schemaJSON::Schema

Returns a new schema matching an array whose items all match this schema.

Returns:

  • (JSON::Schema)

    a new schema matching an array whose items all match this schema.



52
53
54
55
56
# File 'lib/json-schema/schema.rb', line 52

def to_array_schema
  array_schema = { 'type' => 'array', 'items' => schema }
  array_schema['$schema'] = schema['$schema'] unless schema['$schema'].nil?
  JSON::Schema.new(array_schema, uri, validator)
end

#to_sObject



58
59
60
# File 'lib/json-schema/schema.rb', line 58

def to_s
  @schema.to_json
end

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



32
33
34
# File 'lib/json-schema/schema.rb', line 32

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