Sha256: 59b8c41969853a46d42ccdd7415411e7dd94df9b543ad9119ba5c53485d58c8d

Contents?: true

Size: 1.1 KB

Versions: 18

Compression:

Stored size: 1.1 KB

Contents

# 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

18 entries across 18 versions & 1 rubygems

Version Path
json_schemer-1.0.3 lib/json_schemer/errors.rb
json_schemer-1.0.2 lib/json_schemer/errors.rb
json_schemer-1.0.1 lib/json_schemer/errors.rb
json_schemer-1.0.0 lib/json_schemer/errors.rb
json_schemer-0.2.25 lib/json_schemer/errors.rb
json_schemer-0.2.24 lib/json_schemer/errors.rb
json_schemer-0.2.23 lib/json_schemer/errors.rb
json_schemer-0.2.22 lib/json_schemer/errors.rb
json_schemer-0.2.21 lib/json_schemer/errors.rb
json_schemer-0.2.20 lib/json_schemer/errors.rb
json_schemer-0.2.19 lib/json_schemer/errors.rb
json_schemer-0.2.18 lib/json_schemer/errors.rb
json_schemer-0.2.17 lib/json_schemer/errors.rb
json_schemer-0.2.16 lib/json_schemer/errors.rb
json_schemer-0.2.15 lib/json_schemer/errors.rb
json_schemer-0.2.14 lib/json_schemer/errors.rb
json_schemer-0.2.13 lib/json_schemer/errors.rb
json_schemer-0.2.12 lib/json_schemer/errors.rb