Class: JSON::Validator
- Inherits:
-
Object
- Object
- JSON::Validator
- Defined in:
- common/json_schema_concurrency_fix.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#serialize(schema) ⇒ Object
Plus one bonus: don’t use MultiJson here.
-
#validate ⇒ Object
Run a simple true/false validation of data against a schema.
Class Method Details
.clear_errors ⇒ Object
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_errors ⇒ Object
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 |
#validate ⇒ Object
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, [], ) Validator.clear_cache if [: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 |