Class: JSON::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/scorpio/json-schema-fragments.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_data, data, opts = {}) ⇒ Validator

Returns a new instance of Validator.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/scorpio/json-schema-fragments.rb', line 141

def initialize(schema_data, data, opts={})
  @options = @@default_opts.clone.merge(opts)
  @errors = []

  validator = self.class.validator_for_name(@options[:version])
  @options[:version] = validator
  @options[:schema_reader] ||= self.class.schema_reader

  @validation_options = @options[:record_errors] ? {:record_errors => true} : {}
  @validation_options[:insert_defaults] = true if @options[:insert_defaults]
  @validation_options[:strict] = true if @options[:strict] == true
  @validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]

  @@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
  @original_data = data
  @data = initialize_data(data)
  @@mutex.synchronize { build_schemas(@base_schema) }

  # If the :fragment option is set, try and validate against the fragment
  if opts[:fragment]
    @base_schema = schema_from_fragment(@base_schema, opts[:fragment])
  end

  # validate the schema, if requested
  if @options[:validate_schema]
    if @base_schema.schema["$schema"]
      base_validator = self.class.validator_for_name(@base_schema.schema["$schema"])
    end
    metaschema = base_validator ? base_validator.metaschema : validator.metaschema
    # Don't clear the cache during metaschema validation!
    self.class.validate!(metaschema, @base_schema.schema, {:clear_cache => false})
  end
end

Instance Method Details

#schema_from_fragment(base_schema, fragment) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/scorpio/json-schema-fragments.rb', line 175

def schema_from_fragment(base_schema, fragment)
  schema_uri = base_schema.uri

  pointer = JSON::Schema::Pointer.new(:fragment, fragment)

  base_schema = JSON::Schema.new(pointer.evaluate(base_schema.schema), schema_uri, @options[:version])

  if @options[:list]
    base_schema.to_array_schema
  elsif base_schema.is_a?(Hash)
    JSON::Schema.new(base_schema, schema_uri, @options[:version])
  else
    base_schema
  end
end