idcf-json_hyper_schema 0.1.0 → 0.2.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: ffd49fb93bcdb6b81071e58dd5ebebb6587941db
4
- data.tar.gz: b118944ec52abfc512b2485cd09cc0dc6e555d92
3
+ metadata.gz: cf6fd0e7a80ec0ef552e19bccec568431e293887
4
+ data.tar.gz: 0881a485385f89b213f7491e6485f6f1a7bae38a
5
5
  SHA512:
6
- metadata.gz: 65a81b483dd7ba5850af10409de7e642b41b0c46218fe30caffe171cb9fb7b645c0ea3d04e723250bcfa0b87ef2027be6d2b53407ccc1cec636dd3d70730e937
7
- data.tar.gz: 509d639623c599763209b9690b5dd8436f8aa935841e286c05d050d3ba9f522142ad118d71f5056eed4465477c54a0fe70d9fd137142ee13cf11c0972981a824
6
+ metadata.gz: a82bc9a88edb0460e2bb77b3a017dea8628db49497caf0fe6e5a96cb40b585951f440eca62b76d5e8e82bf191aef97ceb2563739c9ff58a10e75aad2437c69fd
7
+ data.tar.gz: 0374d508d3728696cb5464b3ed7a49135b43f328c04dc92e4de51111518215e47a05b36b695598549b81b8e57533678adbcdc5688f1c7c4dd9fb0005f3a4bf8d
@@ -14,6 +14,10 @@ engines:
14
14
  ratings:
15
15
  paths:
16
16
  - "**.rb"
17
+ checks:
18
+ method-complexity:
19
+ config:
20
+ threshold: 10
17
21
  exclude_paths:
18
22
  - vendor/bundle
19
23
  - .idea
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -26,3 +26,5 @@ Style/ClassLength:
26
26
  Max: 150
27
27
  Style/MethodLength:
28
28
  Max: 15
29
+ Style/FormatStringToken:
30
+ Enabled: false
data/README.md CHANGED
@@ -63,7 +63,7 @@ require 'idcf/json_hyper_schema'
63
63
 
64
64
  path = File.expand_path('./sample.json')
65
65
  analyst = Idcf::JsonHyperSchema::Analyst.new.load(path)
66
- p analyst.links(j)
66
+ p analyst.links
67
67
  ```
68
68
 
69
69
  or
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'idcf/json_hyper_schema/version'
@@ -1,9 +1,19 @@
1
+ require 'json_schema'
2
+ require 'idcf/json_hyper_schema/validation'
1
3
  module Idcf
2
4
  module JsonHyperSchema
3
5
  # json-hyper-schema analyst
4
6
  class Analyst
5
7
  attr_reader :process_version, :schema, :load_schema
6
8
 
9
+ def json_schema_init
10
+ configuration = JsonSchema.configuration
11
+ Idcf::JsonHyperSchema::Validation.validations.each do |key, val|
12
+ configuration.register_format(key, val)
13
+ end
14
+ self
15
+ end
16
+
7
17
  # json file laod
8
18
  #
9
19
  # @param path [String]
@@ -40,6 +50,7 @@ module Idcf
40
50
  #
41
51
  # @return Expands::LinkInfoBase
42
52
  def links
53
+ json_schema_init
43
54
  p_schema = JsonSchema.parse!(@load_schema)
44
55
  @process_version = schema_version(p_schema)
45
56
  p_schema.expand_references!
@@ -53,8 +64,9 @@ module Idcf
53
64
  # @return Expands::LinkInfoBase
54
65
  def schema_links(schema, options = {})
55
66
  ex_schema = expand(schema, options)
56
- p_schema = JsonSchema.parse!(ex_schema.schema)
57
- t_v = @process_version
67
+ json_schema_init
68
+ p_schema = JsonSchema.parse!(ex_schema.schema)
69
+ t_v = @process_version
58
70
  p_schema.expand_references!
59
71
  result = find_links(p_schema)
60
72
  @process_version = t_v
@@ -95,7 +107,7 @@ module Idcf
95
107
  result << link_obj
96
108
  end
97
109
 
98
- schema.properties.each do |_, sub|
110
+ schema.properties.each_value do |sub|
99
111
  result.concat(find_links(sub))
100
112
  end
101
113
  end
@@ -55,7 +55,7 @@ module Idcf
55
55
  def make_ids(schema)
56
56
  {}.tap do |result|
57
57
  next if schema['definitions'].nil?
58
- schema['definitions'].each do |_k, v|
58
+ schema['definitions'].each_value do |v|
59
59
  result[v['id']] = v.deep_dup unless v['id'].nil?
60
60
  result.merge!(search_child_ids(v))
61
61
  end
@@ -63,14 +63,15 @@ module Idcf
63
63
  end
64
64
 
65
65
  def search_child_ids(child)
66
- {}.tap do |result|
67
- next unless child.class == Hash
68
- child.each do |_k, v|
69
- next unless v.class == Hash
70
- result[v['id']] = v.deep_dup unless v['id'].nil?
71
- result.merge!(search_child_ids(v))
72
- end
66
+ result = {}
67
+ return result unless child.class == Hash
68
+ child.each_value do |v|
69
+ next unless v.class == Hash
70
+ id = v['id']
71
+ result[id] = v.deep_dup unless id.nil?
72
+ result.merge!(search_child_ids(v))
73
73
  end
74
+ result
74
75
  end
75
76
 
76
77
  def delete_id(data)
@@ -95,7 +96,8 @@ module Idcf
95
96
  end
96
97
 
97
98
  def exp_hash(schema)
98
- schema.keys.each do |k|
99
+ list = schema.keys
100
+ list.each do |k|
99
101
  exp = expansion(k, schema[k])
100
102
  if k == '$ref' && exp.class == Hash
101
103
  schema.delete(k)
@@ -132,10 +132,10 @@ module Idcf
132
132
  param = args.deep_dup
133
133
  next unless param.class == Hash
134
134
  param = param.stringify_keys
135
- make_query_params(args).each do |qk, _qv|
135
+ make_query_params(args).each_key do |qk|
136
136
  param.delete(qk)
137
137
  end
138
- properties.each do |pk, _pv|
138
+ properties.each_key do |pk|
139
139
  result[pk] = param[pk] if param.key?(pk)
140
140
  end
141
141
  end
@@ -0,0 +1,50 @@
1
+ require 'json_schema'
2
+ module Idcf
3
+ module JsonHyperSchema
4
+ # json-hyper-schema validation
5
+ class Validation
6
+ class << self
7
+ attr_reader :add_validation
8
+
9
+ def reset_format
10
+ @add_validation = {}
11
+ self
12
+ end
13
+
14
+ def register_format(name, validator_proc)
15
+ @add_validation ||= {}
16
+ @add_validation[name] = validator_proc
17
+ self
18
+ end
19
+
20
+ def validations
21
+ @add_validation ||= {}
22
+ custom_validations.merge(@add_validation)
23
+ end
24
+
25
+ protected
26
+
27
+ def custom_validations
28
+ {
29
+ 'ipv4_cidr' => lambda do |data|
30
+ ip_check(data, JsonSchema::Validator::IPV4_PATTERN)
31
+ end,
32
+ 'ipv6_cidr' => lambda do |data|
33
+ ip_check(data, JsonSchema::Validator::IPV6_PATTERN)
34
+ end,
35
+ 'integer' => lambda do |data|
36
+ data =~ /^[0-9]+$/
37
+ end
38
+ }
39
+ end
40
+
41
+ def ip_check(data, pattern)
42
+ list = data.split('/')
43
+ return false unless list.size == 2
44
+ return false unless list[0] =~ pattern
45
+ list[1] =~ /^[0-9]*$/
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,5 +1,5 @@
1
1
  module Idcf
2
2
  module JsonHyperSchema
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idcf-json_hyper_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - IDC Frontier Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-05 00:00:00.000000000 Z
11
+ date: 2018-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,7 @@ extra_rdoc_files: []
114
114
  files:
115
115
  - ".codeclimate.yml"
116
116
  - ".gitignore"
117
+ - ".rspec"
117
118
  - ".rubocop.yml"
118
119
  - Gemfile
119
120
  - Gemfile.lock
@@ -128,6 +129,7 @@ files:
128
129
  - lib/idcf/json_hyper_schema/expands/link_info_base.rb
129
130
  - lib/idcf/json_hyper_schema/expands/link_info_v4.rb
130
131
  - lib/idcf/json_hyper_schema/expands/v4.rb
132
+ - lib/idcf/json_hyper_schema/validation.rb
131
133
  - lib/idcf/json_hyper_schema/version.rb
132
134
  homepage: https://www.idcfcloud.jp
133
135
  licenses:
@@ -149,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
151
  version: '0'
150
152
  requirements: []
151
153
  rubyforge_project:
152
- rubygems_version: 2.6.8
154
+ rubygems_version: 2.6.13
153
155
  signing_key:
154
156
  specification_version: 4
155
157
  summary: IDCF Json-Hyper-Schema Expand tools