Sha256: e289a46bba6173250d22032a5e08228068780eb1dabb1d508616b7d5060ef39a

Contents?: true

Size: 1.59 KB

Versions: 19

Compression:

Stored size: 1.59 KB

Contents

module JSON
  class Schema
    class ValidationError < StandardError
      INDENT = "    "
      attr_accessor :fragments, :schema, :failed_attribute, :sub_errors, :message

      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

      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

      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

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

Version data entries

19 entries across 18 versions & 4 rubygems

Version Path
json-schema-ouidou-2.9.1 lib/json-schema/errors/validation_error.rb
json-schema-ouidou-2.9.0 lib/json-schema/errors/validation_error.rb
json-schema-3.0.0 lib/json-schema/errors/validation_error.rb
mountapi-0.11.1 vendor/json-schema/lib/json-schema/errors/validation_error.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/bundler/gems/json-schema-2253a5ee6679/lib/json-schema/errors/validation_error.rb
json-schema-2.8.1 lib/json-schema/errors/validation_error.rb
json-schema-2.8.0 lib/json-schema/errors/validation_error.rb
json-schema-2.7.0 lib/json-schema/errors/validation_error.rb
json-schema-2.6.2 lib/json-schema/errors/validation_error.rb
json-schema-2.6.1 lib/json-schema/errors/validation_error.rb
json-schema-2.6.0 lib/json-schema/errors/validation_error.rb
json-schema-2.5.2 lib/json-schema/errors/validation_error.rb
json-schema-openc-fork-0.0.2 lib/json-schema/errors/validation_error.rb
json-schema-2.5.1 lib/json-schema/errors/validation_error.rb
json-schema-openc-fork-0.0.1 lib/json-schema/errors/validation_error.rb
json-schema-2.5.0 lib/json-schema/errors/validation_error.rb
json-schema-2.4.1 lib/json-schema/errors/validation_error.rb
json-schema-2.4.0 lib/json-schema/errors/validation_error.rb
json-schema-2.3.0 lib/json-schema/errors/validation_error.rb