rubocop-iotventure 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b8a1f840812e93ae9174569844d2aa5d0f76aa7992bb3644d952c062e6182df
4
- data.tar.gz: 3991d6ec7f5d7ffce8ac15f6f774b1693ffaa793cee07c2fc6928acfbf78bf49
3
+ metadata.gz: beea57247dc2b420f2cdff17108b0b0b4a7f542e242b4a01d3515ae3f16d5337
4
+ data.tar.gz: 4ce49ecad2def1b3879ea73cfa6c147af01c606e0a5ffc237812e3dcc3bb394c
5
5
  SHA512:
6
- metadata.gz: 03cd8ba07eff16ae6291f202f3f9de70e8eead68bcfe15623edcdaf9a832ae370f4d3b4a316282162b2a5ee3db92adc636a68058a18c37318a11303548dbdbb3
7
- data.tar.gz: 77de6184ef30e9ccd0473c16e5e9a75ad618df38bf57eb3c7a779f853c8bd8989499fb3ce12daccd64477eb5f6f7e1208f354fe43f9049189acd96a759197de3
6
+ metadata.gz: 9d55967ddcd84b5c83e12c2e27efbc80d628c4de15cb187f9aebd954a042eff85f5260fe7967db59e4a077156395fd6ea5f6b82a81bf5167691a9225ea15257c
7
+ data.tar.gz: 543147561c54fc3bdbe7d6009773df9a65a8c951271125b6d907b7ad0a2753c0380fed021331c24d440780bf6652b89cfa653db1c018403a3d19e756650e6ae6
data/CHANGELOG.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased]
3
+ ## [0.3.0] - 2023-06-01
4
+
5
+ - BU-64 Add AdditionalProperties cop
4
6
 
5
7
  ## [0.2.1] - 2023-04-21
6
8
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-iotventure (0.2.1)
4
+ rubocop-iotventure (0.3.0)
5
5
  rubocop (~> 1.0)
6
6
  rubocop-rake (~> 0.6.0)
7
7
 
data/README.md CHANGED
@@ -24,6 +24,33 @@ gem install rubocop-iotventure
24
24
 
25
25
  ## Usage
26
26
 
27
+ ### IotVenture/AdditionalProperties
28
+
29
+ ```ruby
30
+ # bad
31
+ {
32
+ type: :object,
33
+ properties: {
34
+ prop1: { type: :string },
35
+ prop2: { type: :string }
36
+ }
37
+ }
38
+
39
+ # good
40
+ {
41
+ type: :object,
42
+ properties: {
43
+ prop1: { type: :string },
44
+ prop2: { type: :string }
45
+ },
46
+ additionalProperties: false
47
+ }
48
+ ```
49
+
50
+ This cop checks that all object schemas have the `additionalProperties` property set to `false` or a useful matcher (not `true`). This is to prevent unexpected properties from being added to the object.
51
+
52
+ Note: This only works for schemas defined as Ruby hashes.
53
+
27
54
  ### Iotventure/DuplicateResponseCode
28
55
 
29
56
  ```ruby
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Iotventure
6
+ # This cop checks that every array and object schema has additionalProperties (defined or set to false).
7
+ # Note: This only works for schemas defined as Ruby hashes.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ #
13
+ # {
14
+ # type: :object,
15
+ # properties: {
16
+ # foo: { type: :string },
17
+ # bar: { type: :boolean }
18
+ # }
19
+ # }
20
+ #
21
+ # @example
22
+ #
23
+ # # bad
24
+ #
25
+ # {
26
+ # type: :object,
27
+ # properties: {
28
+ # foo: { type: :string },
29
+ # bar: { type: :boolean }
30
+ # },
31
+ # additionalProperties: true
32
+ # }
33
+ #
34
+ #
35
+ # @example
36
+ #
37
+ # # good
38
+ #
39
+ # {
40
+ # type: :object,
41
+ # properties: {
42
+ # foo: { type: :string },
43
+ # bar: { type: :boolean }
44
+ # },
45
+ # additionalProperties: false
46
+ # }
47
+ #
48
+ #
49
+ #
50
+ class AdditionalProperties < Base
51
+ MISSING_MSG = 'Schema is missing additionalProperties. ' \
52
+ 'Please add it to the schema at %<current>s'
53
+ ALWAYS_TRUE_MSG = 'Schema has additionalProperties set to true. ' \
54
+ 'Please set it to false or define a rule at %<current>s'
55
+
56
+ # @!method schema_definition_with_properties?(node)
57
+ def_node_matcher :schema_object_definition_with_properties?, <<~PATTERN
58
+ (hash<
59
+ (pair
60
+ (sym :type)
61
+ {(sym :object) (str "object")}
62
+ )
63
+ (pair
64
+ <{(sym :properties) (sym :patternProperties)}>
65
+ ...
66
+ )
67
+ ...
68
+ >)
69
+ PATTERN
70
+
71
+ # @!method always_true_schema?(node)
72
+ def_node_matcher :always_true_schema?, <<~PATTERN
73
+ {
74
+ (true)
75
+ (hash) # Empty hash is also always true in JSON Schema
76
+ }
77
+ PATTERN
78
+
79
+ def on_hash(node)
80
+ return unless schema_object_definition_with_properties?(node)
81
+
82
+ additional_properties = node.pairs.find { |pair| pair.key.value == :additionalProperties }
83
+ return add_missing_offense(node) unless additional_properties
84
+
85
+ add_always_true_offense(node) if always_true_schema?(additional_properties.value)
86
+ end
87
+
88
+ private
89
+
90
+ def add_missing_offense(node)
91
+ add_offense(node, message: format(MISSING_MSG, current: source_location(node)))
92
+ end
93
+
94
+ def add_always_true_offense(node)
95
+ add_offense(node, message: format(ALWAYS_TRUE_MSG, current: source_location(node)))
96
+ end
97
+
98
+ def source_location(node)
99
+ range = node.location.expression
100
+ path = smart_path(range.source_buffer.name)
101
+ "#{path}:#{range.line}"
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -3,3 +3,4 @@
3
3
  require_relative 'iotventure/duplicate_response_code'
4
4
  require_relative 'iotventure/save_request_example'
5
5
  require_relative 'iotventure/schema_definition_per_response'
6
+ require_relative 'iotventure/additional_properties'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Iotventure
5
- VERSION = '0.2.1'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-iotventure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fynn Starke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-21 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -128,6 +128,7 @@ files:
128
128
  - bin/setup
129
129
  - config/default.yml
130
130
  - lib/rubocop-iotventure.rb
131
+ - lib/rubocop/cop/iotventure/additional_properties.rb
131
132
  - lib/rubocop/cop/iotventure/duplicate_response_code.rb
132
133
  - lib/rubocop/cop/iotventure/save_request_example.rb
133
134
  - lib/rubocop/cop/iotventure/schema_definition_per_response.rb