strum 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 3f31df72127e2e0b1641f04a6653c6dd73c9104a0755a1b3b69536a9da88478a
4
- data.tar.gz: 61e6b4f3e9751b1399feaa24c036d0f1296cf71fc0e6009043bf4b70fbb329c2
3
+ metadata.gz: 461d85a4cb7a32b26a404517717ee5b677ff41488f37be951023a2b25a517086
4
+ data.tar.gz: 699a002f15c6f4aab8d2fabe5d99846f622a269cf655577cda138cde0e49f418
5
5
  SHA512:
6
- metadata.gz: 6f728e080d5ac456c2d45abbaf7e615526c03097a911676e7cc1ac5198b5998a26d7cff4bf8ab1ced7b422ff385a4ca58bca8950ac87f02dd81d2caba079ba16
7
- data.tar.gz: d7cd68e74b374f946a85e4425de7cf112c83c6b56c93a1da0801e6541fced786eb1990e82342a0c555a10b126e5a0729a2263c452eb5cba454f6e24908217f10
6
+ metadata.gz: 169582be09f5b176d0cc27fb77d18fb77c315a68d7b9c71ce7d02448b7848a2f3449040ffe613bc3199272d8b65b559d1f19a2987201be5514974fe2888b214b
7
+ data.tar.gz: b9f33038c7f6cd2cdc1ea297d3860b67c19115f0212bad1947c81f6eb3f0e06bf5c64e21f731fd0a9a6723b17c8e3090e1b86c544dd82045bb98a0433041e8bb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strum (0.1.0)
4
+ strum (0.1.2)
5
5
  dry-inflector (~> 0.2.0)
6
6
  dry-struct (~> 1.2)
7
7
  json-schema (~> 2.8.1)
@@ -53,7 +53,7 @@ GEM
53
53
  parallel (1.19.1)
54
54
  parser (2.7.0.5)
55
55
  ast (~> 2.4.0)
56
- public_suffix (4.0.3)
56
+ public_suffix (4.0.4)
57
57
  rainbow (3.0.0)
58
58
  rake (10.5.0)
59
59
  reline (0.1.3)
@@ -1,102 +1,10 @@
1
- require "strum/service"
2
- require "json-schema"
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/json_schema/validate"
4
+ require "strum/json_schema/cast"
3
5
 
4
6
  module Strum
5
7
  module JsonSchema
6
- class Validate
7
- include Strum::Service
8
-
9
- def call
10
- array_errors = JSON::Validator.fully_validate(args[:schema], input, errors_as_objects: true)
11
- array_errors.each { |error| add_error(*self.parse_json_schema_error(error)) }
12
- output(input)
13
- end
14
-
15
- def audit
16
- add_error(:schema, :not_found) if !args[:schema].is_a?(Hash)
17
- end
18
-
19
- protected
20
-
21
- def parse_json_schema_error(error)
22
- id = error[:fragment].sub(/#/, "input")
23
- keys = id.split("/")
24
- last_key_idx = keys.rindex { |key| key !~ /[0-9]+/ }
25
- last_key = keys.map { |key| key =~ /[0-9]+/ ? "[#{key}]" : ".#{key}" }.join[1..-1]
26
- value = error[:message][0, error[:message].index(/ in schema/)].sub(error[:fragment], last_key)
27
- [last_key.to_sym, value]
28
- end
29
- end
30
-
31
- class Cast
32
- include Strum::Service
33
-
34
- def call
35
- output(casted(input, args[:schema]))
36
- end
37
-
38
- def audit
39
- add_error(:schema, :not_found) if !args[:schema].is_a?(Hash)
40
- end
41
-
42
- protected
43
-
44
- def casted(input_data, schema)
45
- @input_data = input_data
46
-
47
- def rec_casted(schema, path = [])
48
- item = self.custom_dig(@input_data, path)
49
-
50
- if item.is_a?(NoValue)
51
- return item
52
- end
53
-
54
- case schema[:type]
55
- when Array
56
- schema[:type].reduce(nil) do |type, x|
57
- new_schema = schema.clone
58
- new_schema[:type] = x
59
- type ||= rec_casted(new_schema, path)
60
- end
61
- when "object"
62
- if item.is_a?(Hash) && schema[:properties].is_a?(Hash)
63
- schema[:properties].map { |key, val| [key, rec_casted(val, path + [key])] }.filter { |pair| !pair[1].is_a?(NoValue) }.to_h
64
- end
65
- when "array"
66
- if item.is_a?(Array) && schema[:items].is_a?(Hash)
67
- (0..(item.length - 1)).reduce([]) { |res, idx| res << rec_casted(schema[:items], path + [idx]) }
68
- end
69
- when "string"
70
- item.to_s
71
- when "integer"
72
- if item.is_a?(Numeric)
73
- item
74
- elsif item.is_a?(String) || item.is_a?(Symbol)
75
- item.to_s.to_i
76
- end
77
- when "boolean"
78
- item.to_s.downcase == "true"
79
- when "jsonb"
80
- if (Module.constants.include?(:Sequel)) && (Sequel.methods.include?(:pg_jsonb_wrap))
81
- Sequel.pg_jsonb_wrap(item)
82
- else
83
- add_error(:schema, "jsonb type is not supported")
84
- end
85
- when nil, ""
86
- item
87
- else
88
- add_error(:schema, :invalid_type)
89
- end
90
- end
91
-
92
- rec_casted(schema)
93
- end
94
-
95
- def custom_dig(obj, path)
96
- path.reduce(obj) { |item, x| item.fetch(x) { |it| return NoValue.new } }
97
- end
98
- end
99
-
100
8
  class NoValue; end
101
9
  end
102
10
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/service"
4
+
5
+ module Strum
6
+ module JsonSchema
7
+ class Cast
8
+ include Strum::Service
9
+
10
+ def call
11
+ output(casted(input, args[:schema]))
12
+ end
13
+
14
+ def audit
15
+ add_error(:schema, :not_found) if !args[:schema].is_a?(Hash)
16
+ end
17
+
18
+ protected
19
+
20
+ def casted(input_data, schema, path = [])
21
+ item = custom_dig(input_data, path)
22
+
23
+ return item if item.is_a?(NoValue)
24
+
25
+ case schema[:type]
26
+ when Array
27
+ schema[:type].reduce(nil) do |type, x|
28
+ new_schema = schema.clone
29
+ new_schema[:type] = x
30
+ type || casted(input_data, new_schema, path)
31
+ end
32
+ when "object"
33
+ if item.is_a?(Hash) && schema[:properties].is_a?(Hash)
34
+ item.map { |key, val| (prop = schema[:properties][key]) ? [key, casted(input_data, prop, path + [key])] : [key, val]}.filter { |pair| !pair[1].is_a?(NoValue) }.to_h
35
+ end
36
+ when "array"
37
+ if item.is_a?(Array) && schema[:items].is_a?(Hash)
38
+ (0..(item.length - 1)).reduce([]) { |res, idx| res << casted(input_data, schema[:items], path + [idx]) }
39
+ end
40
+ when "string"
41
+ item.to_s
42
+ when "integer"
43
+ if item.is_a?(Numeric)
44
+ item
45
+ elsif item.is_a?(String) || item.is_a?(Symbol)
46
+ item.to_s.to_i
47
+ end
48
+ when "boolean"
49
+ item.to_s.downcase == "true"
50
+ when "jsonb"
51
+ if Module.constants.include?(:Sequel) && Sequel.methods.include?(:pg_jsonb_wrap)
52
+ Sequel.pg_jsonb_wrap(item)
53
+ else
54
+ add_error(:schema, "jsonb type is not supported")
55
+ end
56
+ when nil, ""
57
+ item
58
+ else
59
+ add_error(:schema, :invalid_type)
60
+ end
61
+ end
62
+
63
+ def custom_dig(obj, path)
64
+ path.reduce(obj) { |item, x| item.fetch(x) { |_it| return NoValue.new } }
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strum/service"
4
+ require "json-schema"
5
+
6
+ module Strum
7
+ module JsonSchema
8
+ class Validate
9
+ include Strum::Service
10
+
11
+ def call
12
+ array_errors = JSON::Validator.fully_validate(args[:schema], input, errors_as_objects: true)
13
+ array_errors.each { |error| add_error(*parse_json_schema_error(error)) }
14
+ output(input)
15
+ end
16
+
17
+ def audit
18
+ add_error(:schema, :not_found) if !args[:schema].is_a?(Hash)
19
+ end
20
+
21
+ protected
22
+
23
+ def parse_json_schema_error(error)
24
+ id = error[:fragment].sub(/#/, "input")
25
+ keys = id.split("/")
26
+ last_key = keys.map { |key| key =~ /[0-9]+/ ? "[#{key}]" : ".#{key}" }.join[1..-1]
27
+ value = error[:message][0, error[:message].index(/ in schema/)].sub(error[:fragment], last_key)
28
+ [last_key.to_sym, value]
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/strum/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strum
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov
@@ -203,6 +203,8 @@ files:
203
203
  - lib/strum/json.rb
204
204
  - lib/strum/json_deserializer.rb
205
205
  - lib/strum/json_schema.rb
206
+ - lib/strum/json_schema/cast.rb
207
+ - lib/strum/json_schema/validate.rb
206
208
  - lib/strum/json_serializer.rb
207
209
  - lib/strum/object_to_hash.rb
208
210
  - lib/strum/pipe.rb