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, 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

#jsonObject



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

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

#to_hObject



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

def to_h
  JSON.parse json
end

#to_sObject



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