Class: Rodolfo::JSONSchema
- Inherits:
-
Object
- Object
- Rodolfo::JSONSchema
- Defined in:
- lib/rodolfo/json_schema.rb
Overview
Rodolfo Recipe JSON Schema
Instance Method Summary collapse
-
#initialize(path, strict = false) ⇒ JSONSchema
constructor
A new instance of JSONSchema.
- #json ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
-
#validate(data) ⇒ Object
Validate the json schema May raise a SchemaValidationError.
Constructor Details
#initialize(path, strict = false) ⇒ JSONSchema
Returns a new instance of JSONSchema.
9 10 11 12 |
# File 'lib/rodolfo/json_schema.rb', line 9 def initialize(path, strict = false) @path = path @strict = strict end |
Instance Method Details
#json ⇒ Object
27 28 29 |
# File 'lib/rodolfo/json_schema.rb', line 27 def json @json ||= File.read @path end |
#to_h ⇒ Object
23 24 25 |
# File 'lib/rodolfo/json_schema.rb', line 23 def to_h JSON.parse json end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/rodolfo/json_schema.rb', line 31 def to_s json end |
#validate(data) ⇒ Object
Validate the json schema May raise a SchemaValidationError
16 17 18 19 20 21 |
# File 'lib/rodolfo/json_schema.rb', line 16 def validate(data) opts = { insert_defaults: true, strict: @strict } errors = JSON::Validator.fully_validate json, data, opts raise SchemaValidationError, errors unless errors.empty? data end |