Exception: JSON::Schema::ValidationError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/json-schema/errors/validation_error.rb

Constant Summary collapse

INDENT =
"    "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, fragments, failed_attribute, schema) ⇒ ValidationError

Returns a new instance of ValidationError



7
8
9
10
11
12
13
14
# File 'lib/json-schema/errors/validation_error.rb', line 7

def initialize(message, fragments, failed_attribute, schema)
  @fragments = fragments.clone
  @schema = schema
  @sub_errors = {}
  @failed_attribute = failed_attribute
  @message = message
  super(message_with_schema)
end

Instance Attribute Details

#failed_attributeObject

Returns the value of attribute failed_attribute



5
6
7
# File 'lib/json-schema/errors/validation_error.rb', line 5

def failed_attribute
  @failed_attribute
end

#fragmentsObject

Returns the value of attribute fragments



5
6
7
# File 'lib/json-schema/errors/validation_error.rb', line 5

def fragments
  @fragments
end

#messageObject

Returns the value of attribute message



5
6
7
# File 'lib/json-schema/errors/validation_error.rb', line 5

def message
  @message
end

#schemaObject

Returns the value of attribute schema



5
6
7
# File 'lib/json-schema/errors/validation_error.rb', line 5

def schema
  @schema
end

#sub_errorsObject

Returns the value of attribute sub_errors



5
6
7
# File 'lib/json-schema/errors/validation_error.rb', line 5

def sub_errors
  @sub_errors
end

Instance Method Details

#message_with_schemaObject



41
42
43
# File 'lib/json-schema/errors/validation_error.rb', line 41

def message_with_schema
  "#{message} in schema #{schema.uri}"
end

#to_hashObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/json-schema/errors/validation_error.rb', line 29

def to_hash
  base = {:schema => @schema.uri, :fragment => ::JSON::Schema::Attribute.build_fragment(fragments), :message => message_with_schema, :failed_attribute => @failed_attribute.to_s.split(":").last.split("Attribute").first}
  if !@sub_errors.empty?
    base[:errors] = @sub_errors.inject({}) do |hsh, (subschema, errors)|
      subschema_sym = subschema.downcase.gsub(/\W+/, '_').to_sym
      hsh[subschema_sym] = Array(errors).map{|e| e.to_hash}
      hsh
    end
  end
  base
end

#to_string(subschema_level = 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json-schema/errors/validation_error.rb', line 16

def to_string(subschema_level = 0)
  if @sub_errors.empty?
    subschema_level == 0 ? message_with_schema : message
  else
    messages = ["#{message}. The schema specific errors were:\n"]
    @sub_errors.each do |subschema, errors|
      messages.push "- #{subschema}:"
      messages.concat Array(errors).map { |e| "#{INDENT}- #{e.to_string(subschema_level + 1)}" }
    end
    messages.map { |m| (INDENT * subschema_level) + m }.join("\n")
  end
end