Sha256: e6e30a97814d4ce4d96127a2ded7a319f9db91b921a2717e35ef9112e78b4fde

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
# Based on code from @robacarp found in issue 48:
# https://github.com/davishmcclurg/json_schemer/issues/48
#
module JSONSchemer
  module Errors
    class << self
      def pretty(error)
        data_pointer, type, schema = error.values_at('data_pointer', 'type', 'schema')
        location = data_pointer.empty? ? 'root' : "property '#{data_pointer}'"

        case type
        when 'required'
          keys = error.fetch('details').fetch('missing_keys').join(', ')
          "#{location} is missing required keys: #{keys}"
        when 'null', 'string', 'boolean', 'integer', 'number', 'array', 'object'
          "#{location} is not of type: #{type}"
        when 'pattern'
          "#{location} does not match pattern: #{schema.fetch('pattern')}"
        when 'format'
          "#{location} does not match format: #{schema.fetch('format')}"
        when 'const'
          "#{location} is not: #{schema.fetch('const').inspect}"
        when 'enum'
          "#{location} is not one of: #{schema.fetch('enum')}"
        else
          "#{location} is invalid: error_type=#{type}"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
json_schemer-2.3.0 lib/json_schemer/errors.rb
json_schemer-2.2.1 lib/json_schemer/errors.rb
json_schemer-2.2.0 lib/json_schemer/errors.rb
json_schemer-2.1.1 lib/json_schemer/errors.rb
json_schemer-2.1.0 lib/json_schemer/errors.rb
json_schemer-2.0.0 lib/json_schemer/errors.rb