j2119 0.1.0 → 0.2.1

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
- SHA1:
3
- metadata.gz: 77e37161a3dd37e54c5bd312adf906ba4acb3f31
4
- data.tar.gz: 28ec7bfb523432d79a1be3b52119aca775dfea08
2
+ SHA256:
3
+ metadata.gz: 202e9050184c4caffde950c3247ff1186481b540b8dc8f9b8b3d22255f63beff
4
+ data.tar.gz: 70d6bde16dcbe291f7c1cb82305e0487a029b51abf3684dd6b5489abfed064be
5
5
  SHA512:
6
- metadata.gz: 0ec99127e2a2db8e9708d3736952c5a0a7e61974756576ed3dc9df83f7c95dab55daad4f701dcd87356415d180dcacbe88d49c6e2ac582f0492990e93779d300
7
- data.tar.gz: 317119a518e9e0d6802edfa478acb0275bd0bb0b4cd139bc4e1e9caa43c0601f319951319dc18f75c6cb9e8de1874b4cad38a4d3e6b4ceedd8c115646ac83e88
6
+ metadata.gz: c3e99db9fc578439cd14de9608f35ec5f524f521b351c8e180ceca6f7e2c154c2ad0a8c0f169e53eac0d3d1acbf8471c4aa75a62c9c3daaf3ead5af4694dc96a
7
+ data.tar.gz: 006c59bc14e255f57f368386b8283c91fd67987ec3b4874882c21105f59dc45f8391be2efbb00dd7adf292a4b8085a18a0d1636fc9424dc4b237a7328c20176d
data/j2119.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'j2119'
3
- s.version = '0.1.0'
3
+ s.version = '0.2.1'
4
4
  s.date = '2016-09-28'
5
5
  s.summary = "JSON DSL Validator"
6
6
  s.description = "Validates JSON objects based on constraints in RFC2119-like language"
@@ -19,6 +19,7 @@ module J2119
19
19
 
20
20
  def initialize
21
21
  @allowed = {}
22
+ @any = []
22
23
  end
23
24
 
24
25
  def set_allowed(role, child)
@@ -26,10 +27,20 @@ module J2119
26
27
  @allowed[role] << child
27
28
  end
28
29
 
30
+ def set_any(role)
31
+ @any << role
32
+ end
33
+
29
34
  def allowed?(roles, child)
30
- roles.any? do |role|
35
+ any?(roles) || roles.any? do |role|
31
36
  @allowed[role] && @allowed[role].include?(child)
32
37
  end
33
38
  end
39
+
40
+ def any?(roles)
41
+ roles.any? do |role|
42
+ @any.include?(role)
43
+ end
44
+ end
34
45
  end
35
46
  end
@@ -58,6 +58,7 @@ module J2119
58
58
  relation = assertion['relation']
59
59
  target = assertion['target']
60
60
  strings = assertion['strings']
61
+ child_type = assertion['child_type']
61
62
  vals = assertion['vals']
62
63
 
63
64
  # watch out for conditionals
@@ -110,14 +111,20 @@ module J2119
110
111
  end
111
112
 
112
113
  # there can be role defs there too
113
- if assertion['child_type']
114
+ if child_type
114
115
  @matcher.add_role assertion['child_role']
115
- child_type = assertion['child_type']
116
116
  if child_type == 'value'
117
117
  @roles.add_child_role(role, field_name, assertion['child_role'])
118
118
  elsif child_type == 'element' || child_type == 'field'
119
119
  @roles.add_grandchild_role(role, field_name, assertion['child_role'])
120
120
  end
121
+ else
122
+ anyOrObjectOrArray = (!type) || (type == 'object') || (type == 'array')
123
+ # untyped field without a defined child role
124
+ if field_name && anyOrObjectOrArray && (modal != 'MUST NOT')
125
+ @roles.add_grandchild_role(role, field_name, field_name)
126
+ @allowed_fields.set_any(field_name)
127
+ end
121
128
  end
122
129
  end
123
130
 
@@ -30,17 +30,17 @@ module J2119
30
30
  "[#{re_classes.join('')}]"
31
31
  end
32
32
 
33
- @@name_re = classes_to_re(INITIAL_NAME_CLASSES) +
33
+ name_re = classes_to_re(INITIAL_NAME_CLASSES) +
34
34
  classes_to_re(FOLLOWING_NAME_CLASSES) + '*'
35
- dot_step = DOT_SEPARATOR + '((' + @@name_re + ')|(\*))'
36
- rp_dot_step = DOT_SEPARATOR + @@name_re
37
- bracket_step = '\[' + "'" + @@name_re + "'" + '\]'
35
+ dot_step = DOT_SEPARATOR + '((' + name_re + ')|(\*))'
36
+ rp_dot_step = DOT_SEPARATOR + name_re
37
+ bracket_step = '\[' + "'" + name_re + "'" + '\]'
38
38
  rp_num_index = '\[\d+\]'
39
39
  num_index = '\[\d+(, *\d+)?\]'
40
40
  star_index = '\[\*\]'
41
41
  colon_index = '\[(-?\d+)?:(-?\d+)?\]'
42
42
  index = '((' + num_index + ')|(' + star_index + ')|(' + colon_index + '))'
43
- step = '((' + dot_step + ')|(' + bracket_step + '))' + '(' + index + ')?'
43
+ step = '((' + dot_step + ')|(' + bracket_step + ')|(' + index + '))' + '(' + index + ')?'
44
44
  rp_step = '((' + rp_dot_step + ')|(' + bracket_step + '))' + '(' + rp_num_index + ')?'
45
45
  path = '^\$' + '(' + step + ')+$'
46
46
  reference_path = '^\$' + '(' + rp_step + ')+$'
data/lib/j2119/matcher.rb CHANGED
@@ -205,6 +205,9 @@ module J2119
205
205
  def build(re, line)
206
206
  data = {}
207
207
  match = re.match(line)
208
+ unless match
209
+ puts "No names for: #{line}"
210
+ end
208
211
  match.names.each do |name|
209
212
  data[name] = match[name]
210
213
  end
@@ -56,21 +56,21 @@ module J2119
56
56
 
57
57
  # find inheritance-based roles for that field
58
58
  grandchild_roles = @parser.find_grandchild_roles(roles, name)
59
-
60
- # recurse into grandkids
61
- if val.is_a? Hash
62
- val.each do |child_name, child_val|
63
- validate_node(child_val, "#{path}.#{name}.#{child_name}",
64
- grandchild_roles.clone, problems)
65
- end
66
- elsif val.is_a? Array
67
- i = 0
68
- val.each do |member|
69
- validate_node(member, "#{path}.#{name}[#{i}]",
70
- grandchild_roles.clone, problems)
71
- i += 1
59
+ if !@parser.allows_any?(grandchild_roles)
60
+ # recurse into grandkids
61
+ if val.is_a? Hash
62
+ val.each do |child_name, child_val|
63
+ validate_node(child_val, "#{path}.#{name}.#{child_name}",
64
+ grandchild_roles.clone, problems)
65
+ end
66
+ elsif val.is_a? Array
67
+ i = 0
68
+ val.each do |member|
69
+ validate_node(member, "#{path}.#{name}[#{i}]",
70
+ grandchild_roles.clone, problems)
71
+ i += 1
72
+ end
72
73
  end
73
-
74
74
  end
75
75
  end
76
76
  end
data/lib/j2119/parser.rb CHANGED
@@ -97,5 +97,9 @@ module J2119
97
97
  def field_allowed?(roles, child)
98
98
  @allowed_fields.allowed?(roles, child)
99
99
  end
100
+
101
+ def allows_any?(roles)
102
+ @allowed_fields.any?(roles)
103
+ end
100
104
  end
101
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: j2119
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Bray
@@ -14,14 +14,14 @@ dependencies:
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: Validates JSON objects based on constraints in RFC2119-like language
@@ -30,7 +30,7 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
- - .gitignore
33
+ - ".gitignore"
34
34
  - Gemfile
35
35
  - LICENSE
36
36
  - NOTICE.txt
@@ -60,17 +60,17 @@ require_paths:
60
60
  - lib
61
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
62
  requirements:
63
- - - '>='
63
+ - - ">="
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - '>='
68
+ - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 2.0.14.1
73
+ rubygems_version: 2.7.7
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: JSON DSL Validator