Class: Halidator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hal, engine = :pure_ruby) ⇒ Halidator

Returns a new instance of Halidator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/halidator.rb', line 7

def initialize(hal, engine = :pure_ruby)
  case hal
  when String
    @json_string = hal
    parse_json
  else
    @json = hal
  end
  @errors = []
  if engine == :json_schema
    extend Halidate::JsonSchema
  else
    extend Halidate::PureRuby
  end
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/halidator.rb', line 6

def errors
  @errors
end

Instance Method Details

#debug(*str) ⇒ Object



33
34
35
36
37
# File 'lib/halidator.rb', line 33

def debug(*str)
  if $DEBUG
    $stderr.puts str
  end
end

#parse_jsonObject



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

def parse_json
  @json = JSON.parse(@json_string)
end

#show_errorsObject



39
40
41
42
43
# File 'lib/halidator.rb', line 39

def show_errors
  debug ["\nERRORS-VVVVVVVVVVVVVVVVVV",
         *errors,
         "ERRORS-^^^^^^^^^^^^^^^^^^"]
end

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/halidator.rb', line 27

def valid?
  result = validate_json_as_hal
  show_errors
  result
end