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 +4 -4
- data/CHANGELOG.md +3 -1
- data/Gemfile.lock +1 -1
- data/README.md +27 -0
- data/lib/rubocop/cop/iotventure/additional_properties.rb +106 -0
- data/lib/rubocop/cop/iotventure_cops.rb +1 -0
- data/lib/rubocop/iotventure/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: beea57247dc2b420f2cdff17108b0b0b4a7f542e242b4a01d3515ae3f16d5337
|
4
|
+
data.tar.gz: 4ce49ecad2def1b3879ea73cfa6c147af01c606e0a5ffc237812e3dcc3bb394c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d55967ddcd84b5c83e12c2e27efbc80d628c4de15cb187f9aebd954a042eff85f5260fe7967db59e4a077156395fd6ea5f6b82a81bf5167691a9225ea15257c
|
7
|
+
data.tar.gz: 543147561c54fc3bdbe7d6009773df9a65a8c951271125b6d907b7ad0a2753c0380fed021331c24d440780bf6652b89cfa653db1c018403a3d19e756650e6ae6
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|
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.
|
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-
|
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
|