Class: Rodolfo::JSONSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/rodolfo/json_schema.rb

Overview

Rodolfo Recipe JSON Schema

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JSONSchema

Returns a new instance of JSONSchema.



9
10
11
# File 'lib/rodolfo/json_schema.rb', line 9

def initialize(path)
  @path = path
end

Instance Method Details

#jsonObject



26
27
28
# File 'lib/rodolfo/json_schema.rb', line 26

def json
  @json ||= File.read @path
end

#to_hObject



22
23
24
# File 'lib/rodolfo/json_schema.rb', line 22

def to_h
  JSON.parse json
end

#to_sObject



30
31
32
# File 'lib/rodolfo/json_schema.rb', line 30

def to_s
  json
end

#validate(data) ⇒ Object

Validate the json schema May raise a SchemaValidationError



15
16
17
18
19
20
# File 'lib/rodolfo/json_schema.rb', line 15

def validate(data)
  opts = { insert_defaults: true, strict: true }
  errors = JSON::Validator.fully_validate json, data, opts
  raise SchemaValidationError, errors unless errors.empty?
  data
end