File: errors.rb

package info (click to toggle)
ruby-json-schemer 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 7,428; makefile: 4; sh: 4
file content (32 lines) | stat: -rw-r--r-- 1,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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