blumquist 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c073e8fec13fe44202f842b173958cf2540c0d8c
4
- data.tar.gz: 11b7507ac23839339a67acf7bfab064951fec0df
3
+ metadata.gz: d916de12dccf999a0a3a93ed8b1fb321230c7c29
4
+ data.tar.gz: bbb8e9662f65bd8ec435fc9c07632d79d7945ca4
5
5
  SHA512:
6
- metadata.gz: e1f3acfa2cd6aeb66a46650e785c3c02fb91775a224cea63b557b57c8d6d5e4b5fe4551597f625475b992d5675797a1162c37e604ff894977d4a51cf8a0dbf4d
7
- data.tar.gz: ebbe4c0316bb712999d5f83ddaa1bde3db6a5dd2c89c13b32e19be524ae20abf68218af7a113cc16b01da37607a5d523a89dd76dc5525a3f45c5cb2c7401b284
6
+ metadata.gz: d96097e7eb7bfc8915cf59d66e1ee8f5cdc1a6a917c9caf06661b9f8b9abcd877bdd880716d9e9a0234d1d8971a1167a59f18e1dfb84b21c8c5b124b32be9cc8
7
+ data.tar.gz: a029e3cbb688916ed4d18fe0c255a282829de0a7415f89afdcf5db6b21c9f2b095dd398b361b57092e7d44f9eb61946a02edc6bee3ffca823440629f3aabd42d
@@ -1,3 +1,11 @@
1
+ # [0.10.0] - 2017-09-05
2
+ ## Added
3
+ - References ($ref):
4
+ Support for more JSON pointers formats:
5
+ 1. #/key1/key2/.../keyN
6
+ 2. path-to-file.json
7
+ 3. path-to-file.json#/key1/key2/.../keyN
8
+
1
9
  # [0.9.1] - 2017-09-04 Alfonso Mancilla (#20)
2
10
  - Support primitive type: integer.
3
11
 
@@ -2,9 +2,12 @@ require 'blumquist/version'
2
2
  require 'active_support/core_ext/hash/indifferent_access'
3
3
  require 'json'
4
4
  require 'json-schema'
5
+ require 'blumquist/json_pointer'
5
6
  require 'blumquist/errors'
6
7
 
7
8
  class Blumquist
9
+ PRIMITIVE_TYPES = %w{null boolean number string enum integer}.freeze
10
+
8
11
  attr_reader :_type
9
12
 
10
13
  def initialize(options)
@@ -61,25 +64,20 @@ class Blumquist
61
64
  end
62
65
 
63
66
  def resolve_json_pointer(type_def)
64
- key = type_def[:$ref].split('/').last
65
- definition = @schema[:definitions][key]
66
- raise(Errors::InvalidPointer, pointer) unless definition
67
- type_def.merge(definition)
67
+ pointer = JSONPointer.new(type_def[:$ref], document: @schema)
68
+
69
+ type_def.merge(pointer.value)
68
70
  end
69
71
 
70
72
  def resolve_json_pointer!(type_def)
71
- # Should read up on how json pointers are really resolved
72
- pointer = type_def.delete(:$ref)
73
- key = pointer.split('/').last
74
- definition = @schema[:definitions][key]
75
-
76
- raise(Errors::InvalidPointer, pointer) unless definition
73
+ pointer_path = type_def.delete(:$ref)
74
+ pointer = JSONPointer.new(pointer_path, document: @schema)
77
75
 
78
- type_def.merge! definition
76
+ type_def.merge!(pointer.value)
79
77
  end
80
78
 
81
79
  def primitive_type?(type)
82
- %w{null boolean number string enum integer}.include?(type.to_s)
80
+ PRIMITIVE_TYPES.include?(type.to_s)
83
81
  end
84
82
 
85
83
  def define_getters
@@ -1,8 +1,9 @@
1
1
  require 'blumquist/error'
2
- require 'blumquist/errors/unsupported_schema'
3
- require 'blumquist/errors/unsupported_type'
4
2
  require 'blumquist/errors/invalid_pointer'
5
3
  require 'blumquist/errors/missing_array_items_type'
6
4
  require 'blumquist/errors/missing_properties'
7
5
  require 'blumquist/errors/no_compatible_one_of'
6
+ require 'blumquist/errors/unsupported_pointer'
7
+ require 'blumquist/errors/unsupported_schema'
8
+ require 'blumquist/errors/unsupported_type'
8
9
  require 'blumquist/errors/validation'
@@ -1,8 +1,13 @@
1
1
  class Blumquist
2
2
  module Errors
3
3
  class InvalidPointer < Blumquist::Error
4
- def initialize(pointer)
5
- super("Could not find pointer #{pointer.to_s.to_json}")
4
+ def initialize(pointer:, document:)
5
+ msg = %{
6
+ Could not find pointer '#{pointer}' in the given document:\n
7
+ #{JSON.pretty_generate(document)}
8
+ }
9
+
10
+ super(msg)
6
11
  end
7
12
  end
8
13
  end
@@ -0,0 +1,16 @@
1
+ class Blumquist
2
+ module Errors
3
+ class UnsupportedPointer < Blumquist::Error
4
+ def initialize(pointer:)
5
+ msg = %{
6
+ Pointer '#{pointer}' is not supported. Current supported formats are:\n
7
+ 1. #/key1/key2/.../keyN
8
+ 2. path_to_file.json
9
+ 3. path_to_file.json#/key1/key2/.../keyN
10
+ }
11
+
12
+ super(msg)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -2,7 +2,7 @@ class Blumquist
2
2
  module Errors
3
3
  class UnsupportedType < Blumquist::Error
4
4
  def initialize(type)
5
- super("Unsupported type '#{type.to_s}' (#{%w{null, boolean, number, string, array object}.to_json} are supported)")
5
+ super("Unsupported type '#{type.to_s}' (#{Blumquist::PRIMITIVE_TYPES.to_json}) are supported)")
6
6
  end
7
7
  end
8
8
  end
@@ -0,0 +1,75 @@
1
+ #
2
+ # JSON Pointer
3
+ # This is a VERY BASIC implementation
4
+ # to support pointers in the form of:
5
+ #
6
+ # 1. #/key1/key2/.../keyN
7
+ # 2. path_to_file.json
8
+ # 3. path_to_file.json#/key1/key2/.../keyN
9
+ #
10
+ # More about JSON Pointer & Reference Specification
11
+ # https://cswr.github.io/JsonSchema/spec/definitions_references/#json-pointers
12
+ #
13
+
14
+ class Blumquist
15
+ class JSONPointer
16
+ DOCUMENT_ROOT_IDENTIFIER = '#'.freeze
17
+ DOCUMENT_ADDRESS_IDENTIFIER = '.json'.freeze
18
+
19
+ attr_reader :uri
20
+
21
+ def initialize(uri, document: nil)
22
+ @uri = uri
23
+ validate_uri!
24
+ @document = document
25
+ end
26
+
27
+ def value
28
+ result = keys.any? ? document&.dig(*keys) : document
29
+
30
+ raise(Errors::InvalidPointer, pointer: uri, document: document) if result.nil?
31
+
32
+ result
33
+ end
34
+
35
+ private
36
+
37
+ def keys
38
+ return @keys if defined?(@keys)
39
+
40
+ @keys = @uri.split('#')[1]
41
+ @keys = @keys&.sub('/', '') if @keys&.start_with?('/')
42
+ @keys = @keys&.split('/') || []
43
+ end
44
+
45
+ def address
46
+ return @address if defined?(@address)
47
+
48
+ @address = uri.scan(/.*\.json/).first
49
+ end
50
+
51
+ def document
52
+ if points_to_document_address?
53
+ @external_document ||= JSON.parse( File.read(address) )
54
+ else
55
+ @document
56
+ end
57
+ end
58
+
59
+ def points_to_document_address?
60
+ !address.nil?
61
+ end
62
+
63
+ def validate_uri!
64
+ #
65
+ # Why only this uri formats are valid?
66
+ # Refer to the description on the top of this file
67
+ #
68
+
69
+ return if uri.start_with?(DOCUMENT_ROOT_IDENTIFIER)
70
+ return if uri.match(DOCUMENT_ADDRESS_IDENTIFIER)
71
+
72
+ raise(Errors::UnsupportedPointer, pointer: uri)
73
+ end
74
+ end
75
+ end
@@ -1,3 +1,3 @@
1
1
  class Blumquist
2
- VERSION = "0.9.1"
2
+ VERSION = '0.10.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blumquist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jannis Hermanns
@@ -189,10 +189,12 @@ files:
189
189
  - lib/blumquist/errors/missing_array_items_type.rb
190
190
  - lib/blumquist/errors/missing_properties.rb
191
191
  - lib/blumquist/errors/no_compatible_one_of.rb
192
+ - lib/blumquist/errors/unsupported_pointer.rb
192
193
  - lib/blumquist/errors/unsupported_schema.rb
193
194
  - lib/blumquist/errors/unsupported_type.rb
194
195
  - lib/blumquist/errors/validation.rb
195
196
  - lib/blumquist/errors/validation_error.rb
197
+ - lib/blumquist/json_pointer.rb
196
198
  - lib/blumquist/version.rb
197
199
  homepage: https://github.com/moviepilot/blumquist
198
200
  licenses: