Class: JSON::Validator

Inherits:
Object
  • Object
show all
Defined in:
common/json_schema_concurrency_fix.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_errorsObject



26
27
28
# File 'common/json_schema_concurrency_fix.rb', line 26

def clear_errors
  Thread.current[:jsonschema_errors] = []
end

.validation_error(error) ⇒ Object



30
31
32
# File 'common/json_schema_concurrency_fix.rb', line 30

def validation_error(error)
  Thread.current[:jsonschema_errors] << error
end

.validation_errorsObject



34
35
36
# File 'common/json_schema_concurrency_fix.rb', line 34

def validation_errors
  Thread.current[:jsonschema_errors] or []
end

Instance Method Details

#serialize(schema) ⇒ Object

Plus one bonus: don’t use MultiJson here.



41
42
43
44
45
46
47
48
49
# File 'common/json_schema_concurrency_fix.rb', line 41

def serialize schema
  # if defined?(MultiJson)
  #   MultiJson.respond_to?(:dump) ? MultiJson.dump(schema) : MultiJson.encode(schema)
  # else
  #   @@serializer.call(schema)
  # end

  ASUtils.to_json(schema)
end

#validateObject

Run a simple true/false validation of data against a schema



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'common/json_schema_concurrency_fix.rb', line 9

def validate()
  begin
    Validator.clear_errors
    @base_schema.validate(@data, [], @validation_options)
    Validator.clear_cache
    if @options[:errors_as_objects]
      self.class.validation_errors.map {|e| e.to_hash}
    else
      self.class.validation_errors.map {|e| e.to_string}
    end
  rescue JSON::Schema::ValidationError
    Validator.clear_cache
    raise $!
  end
end