statelint 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c171b698ac8e7b834abfd2a1106aa543f3c4e8c
4
+ data.tar.gz: e2250f8cc52a03abfb5b823d5dc85ae340f73cf8
5
+ SHA512:
6
+ metadata.gz: 50c4bfde953ba79fac5e7df834f8c6c624a27999b3708b58554cffdc7d460560dd0199615d95e9aabee8c8d8c7b3615af2794c96260d60daf0ae475429072711
7
+ data.tar.gz: 30c22b4e9e4c61388b50b4fbbffeac283a02e1a880b0c9b8a2ae8777f345e279cebf2be6afdf4277b2c1df33187ac62545a53bd67f1a5b2819d61ae56d06ae30
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *~
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'j2119', '>=0.1.0'
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/NOTICE.txt ADDED
@@ -0,0 +1,3 @@
1
+ statelint
2
+
3
+ Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # statelint
2
+ A Ruby gem that provides a command-line validator for Amazon States Language JSON files.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'statelint'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install statelint
19
+
20
+ ## Usage
21
+
22
+ statelint state-machine-spec, state-machinespec...
23
+
24
+ There are no options. If you see no output, your state machine is fine.
25
+
26
+ ## To do
27
+
28
+ Currently covers most of the grammatical constraints for state-machine
29
+ definitions. The checking of JsonPath syntax is hand-built and probably
30
+ imperfect.
31
+
32
+ The Ruby JSON parser unfortunately does not detect duplicate keys
33
+ in objects, so neither does statelint.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub
38
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib"))
2
+ $:.unshift(lib_dir)
3
+ $:.uniq!
4
+
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :default => :spec
8
+
9
+ RSpec::Core::RakeTask.new
10
+
data/bin/statelint ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"). You may
5
+ # not use this file except in compliance with the License. A copy of the
6
+ # License is located at
7
+ #
8
+ # http://aws.amazon.com/apache2.0/
9
+ #
10
+ # or in the LICENSE.txt file accompanying this file. This file is distributed
11
+ # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ # express or implied. See the License for the specific language governing
13
+ # permissions and limitations under the License.
14
+
15
+ require 'statelint'
16
+
17
+ linter = StateMachineLint::Linter.new
18
+
19
+ # arguments are JSON filenames
20
+ ARGV.each do |file|
21
+ problems = linter.validate(ARGV[0])
22
+
23
+ if !problems.empty?
24
+ header = (problems.size == 1) ? 'One error:' : "#{problems.size} errors:"
25
+ puts header
26
+ problems.each do |problem|
27
+ puts " #{problem}"
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,112 @@
1
+ This document specifies a JSON object called a "State Machine".
2
+ A State Machine MUST have an object field named "States"; each field is a "State".
3
+ A State Machine MUST have a string field named "StartAt".
4
+ A State Machine MAY have a string field named "Comment".
5
+ A State Machine MAY have a string field named "Version".
6
+ A State Machine MAY have a positive-integer field named "TimeoutSeconds" whose value MUST be less than 99999999.
7
+ A State MUST have a string field named "Type" whose value MUST be one of "Pass", "Succeed", "Fail", "Task", "Choice", "Wait", or "Parallel".
8
+ A State whose "Type" field's value is "Pass" is a "Pass State".
9
+ A State whose "Type" field's value is "Succeed" is a "Succeed State".
10
+ A State whose "Type" field's value is "Fail" is a "Fail State".
11
+ A State whose "Type" field's value is "Task" is a "Task State".
12
+ A State whose "Type" field's value is "Choice" is a "Choice State".
13
+ A State whose "Type" field's value is "Wait" is a "Wait State".
14
+ A State whose "Type" field's value is "Parallel" is a "Parallel State".
15
+ A State MAY have a string field named "Comment".
16
+ Each of a Pass State, a Task State, a Wait State, and a Parallel State MAY have a boolean field named "End".
17
+ A State whose "End" field's value is true is a "Terminal State".
18
+ Each of a Succeed State and a Fail State is a "Terminal State".
19
+ A State which is not a Terminal State or a Choice State MUST have a string field named "Next".
20
+ A Terminal State MUST NOT have a field named "Next".
21
+ A State MAY have a nullable-JSONPath field named "InputPath".
22
+ A State MAY have a nullable-referencePath field named "ResultPath".
23
+ A State MAY have a nullable-JSONPath field named "OutputPath".
24
+ A Pass State MAY have a field named "Result".
25
+ A Fail State MUST NOT have a field named "InputPath".
26
+ A Fail State MUST NOT have a field named "ResultPath".
27
+ A Fail State MUST NOT have a field named "OutputPath".
28
+ A Fail State MUST have a string field named "Cause".
29
+ A Fail State MUST have a string field named "Error".
30
+ Each of a Task State and a Parallel State MAY have an object-array field named "Retry"; each element is a "Retrier".
31
+ A Task State MUST have a URI field named "Resource".
32
+ A Task State MAY have a positive-integer field named "TimeoutSeconds" whose value MUST be less than 99999999.
33
+ A Task State MAY have a positive-integer field named "HeartbeatSeconds" whose value MUST be less than 99999999.
34
+ A Retrier MUST have a string-array field named "ErrorEquals".
35
+ A Retrier MAY have an positive-integer field named "IntervalSeconds".
36
+ A Retrier MAY have a nonnegative-integer field named "MaxAttempts" whose value MUST be less than 99999999.
37
+ A Retrier MAY have a float field named "BackoffRate" whose value MUST be greater than or equal to 1.0.
38
+ Each of a Task State and a Parallel State MAY have an object-array field named "Catch"; each element is a "Catcher".
39
+ A Catcher MUST have an string-array field named "ErrorEquals".
40
+ A Catcher MUST have a string field named "Next".
41
+ A Catcher MAY have a nullable-referencePath field named "ResultPath".
42
+ A Choice State MUST have a nonempty-object-array field named "Choices"; each element is a "Choice Rule".
43
+ A Choice State MUST NOT have a field named "Next".
44
+ A Choice State MUST NOT have a field named "End".
45
+ A Choice State MAY have a string field named "Default".
46
+ A Choice Rule MAY have a nonempty-object-array field named "And"; each element is a "Nested Rule".
47
+ A Choice Rule MAY have a nonempty-object-array field named "Or"; each element is a "Nested Rule".
48
+ A Choice Rule MAY have an object field named "Not"; its value is a "Nested Rule".
49
+ A Choice Rule MUST have a string field named "Next".
50
+ A Choice Rule with an "And" field is a "Boolean".
51
+ A Choice Rule with an "Or" field is a "Boolean".
52
+ A Choice Rule with a "Not" field is a "Boolean".
53
+ A Choice Rule MAY have a referencePath field named "Variable".
54
+ A Choice Rule with a "Variable" field is a "Comparison".
55
+ A Comparison MUST have a field named one of "StringEquals", "StringLessThan", "StringGreaterThan", "StringLessThanEquals", "StringGreaterThanEquals", "NumericEquals", "NumericLessThan", "NumericGreaterThan", "NumericLessThanEquals", "NumericGreaterThanEquals", "BooleanEquals", "TimestampEquals", "TimestampLessThan", "TimestampGreaterThan", "TimestampLessThanEquals", or "TimestampGreaterThanEquals".
56
+ A Comparison MAY have a string field named "StringEquals".
57
+ A Comparison MAY have a string field named "StringLessThan".
58
+ A Comparison MAY have a string field named "StringGreaterThan".
59
+ A Comparison MAY have a string field named "StringLessThanEquals".
60
+ A Comparison MAY have a string field named "StringGreaterThanEquals".
61
+ A Comparison MAY have a numeric field named "NumericEquals".
62
+ A Comparison MAY have a numeric field named "NumericLessThan".
63
+ A Comparison MAY have a numeric field named "NumericGreaterThan".
64
+ A Comparison MAY have a numeric field named "NumericLessThanEquals".
65
+ A Comparison MAY have a numeric field named "NumericGreaterThanEquals".
66
+ A Comparison MAY have a boolean field named "BooleanEquals".
67
+ A Comparison MAY have a timestamp field named "TimestampEquals".
68
+ A Comparison MAY have a timestamp field named "TimestampLessThan".
69
+ A Comparison MAY have a timestamp field named "TimestampGreaterThan".
70
+ A Comparison MAY have a timestamp field named "TimestampLessThanEquals".
71
+ A Comparison MAY have a timestamp field named "TimestampGreaterThanEquals".
72
+ A Comparison MUST NOT have a field named "And".
73
+ A Comparison MUST NOT have a field named "Or".
74
+ A Comparison MUST NOT have a field named "Not".
75
+ A Nested Rule MAY have a nonempty-object-array field named "And"; each element is a "Nested Rule".
76
+ A Nested Rule MAY have a nonempty-object-array field named "Or"; each element is a "Nested Rule".
77
+ A Nested Rule MAY have an object field named "Not"; its value is a "Nested Rule".
78
+ A Nested Rule with an "And" field is a "Nested Boolean".
79
+ A Nested Rule with an "Or" field is a "Nested Boolean".
80
+ A Nested Rule with a "Not" field is a "Nested Boolean".
81
+ A Nested Rule MUST NOT have a field named "Next".
82
+ A Nested Rule MAY have a referencePath field named "Variable".
83
+ A Nested Rule with a "Variable" field is a "Nested Comparison".
84
+ A Nested Comparison MAY have a string field named "StringEquals".
85
+ A Nested Comparison MAY have a string field named "StringLessThan".
86
+ A Nested Comparison MAY have a string field named "StringGreaterThan".
87
+ A Nested Comparison MAY have a string field named "StringLessThanEquals".
88
+ A Nested Comparison MAY have a string field named "StringGreaterThanEquals".
89
+ A Nested Comparison MAY have a numeric field named "NumericEquals".
90
+ A Nested Comparison MAY have a numeric field named "NumericLessThan".
91
+ A Nested Comparison MAY have a numeric field named "NumericGreaterThan".
92
+ A Nested Comparison MAY have a numeric field named "NumericLessThanEquals".
93
+ A Nested Comparison MAY have a numeric field named "NumericGreaterThanEquals".
94
+ A Nested Comparison MAY have a boolean field named "BooleanEquals".
95
+ A Nested Comparison MAY have a timestamp field named "TimestampEquals".
96
+ A Nested Comparison MAY have a timestamp field named "TimestampLessThan".
97
+ A Nested Comparison MAY have a timestamp field named "TimestampGreaterThan".
98
+ A Nested Comparison MAY have a timestamp field named "TimestampLessThanEquals".
99
+ A Nested Comparison MAY have a timestamp field named "TimestampGreaterThanEquals".
100
+ A Nested Comparison MUST NOT have a field named "And".
101
+ A Nested Comparison MUST NOT have a field named "Or".
102
+ A Nested Comparison MUST NOT have a field named "Not".
103
+ A Wait State MAY have a positive-integer field named "Seconds".
104
+ A Wait State MAY have a referencePath field named "SecondsPath".
105
+ A Wait State MAY have a timestamp field named "Timestamp".
106
+ A Wait State MAY have a referencePath field named "TimestampPath".
107
+ A Wait State MUST have only one of "Seconds", "SecondsPath", "Timestamp", and "TimestampPath".
108
+ A Wait State MUST have a field named one of "Seconds", "SecondsPath", "Timestamp", or "TimestampPath".
109
+ A Parallel State MUST have an object-array field named "Branches"; each element is a "Branch".
110
+ A Branch MUST have an object field named "States"; each element is a "State".
111
+ A Branch MUST have a string field named "StartAt".
112
+ A Branch MAY have a string field named "Comment".
data/lib/statelint.rb ADDED
@@ -0,0 +1,41 @@
1
+ # Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You may
4
+ # not use this file except in compliance with the License. A copy of the
5
+ # License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the LICENSE.txt file accompanying this file. This file is distributed
10
+ # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11
+ # express or implied. See the License for the specific language governing
12
+ # permissions and limitations under the License.
13
+ #!/usr/bin/env ruby
14
+
15
+ $:.unshift("#{File.expand_path(File.dirname(__FILE__))}/../lib")
16
+ require 'j2119'
17
+ require 'statelint/state_node'
18
+
19
+ # This module calls the J2119 validator on the supplised input, and
20
+ # then performs some States-Language specific semantic checks that
21
+ # aren't expressible in a J2119 schema
22
+
23
+ module StateMachineLint
24
+
25
+ # TODO: Semantic validations:
26
+ # - Path and Reference Path validations
27
+ class Linter
28
+
29
+ def initialize
30
+ schema = File.dirname(__FILE__) + '/../data/StateMachine.j2119'
31
+ @validator = J2119::Validator.new schema
32
+ end
33
+
34
+ def validate json
35
+ problems = @validator.validate json
36
+ checker = StateNode.new
37
+ checker.check(@validator.parsed, @validator.root, problems)
38
+ problems
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,159 @@
1
+ # Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You may
4
+ # not use this file except in compliance with the License. A copy of the
5
+ # License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the LICENSE.txt file accompanying this file. This file is distributed
10
+ # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11
+ # express or implied. See the License for the specific language governing
12
+ # permissions and limitations under the License.
13
+ #!/usr/bin/env ruby
14
+
15
+ module StateMachineLint
16
+
17
+ # Semantic validation that can't be expressed in a J2119 schema
18
+ #
19
+ class StateNode
20
+
21
+ def initialize
22
+ # We push States nodes on here when we traverse them.
23
+ # Then, whenever we find a "Next" or "Default" or "StartAt" node,
24
+ # we validate that the target is there, and record that that
25
+ # target has an incoming pointer
26
+ #
27
+ @current_states_node = []
28
+ @current_states_incoming = []
29
+
30
+ # We keep track of all the state names and complain about
31
+ # dupes
32
+ @all_state_names = {}
33
+ end
34
+
35
+ def check(node, path, problems)
36
+ if !node.is_a? Hash
37
+ return
38
+ end
39
+
40
+ is_machine_top = node.key?("States") && node['States'].is_a?(Hash)
41
+ if is_machine_top
42
+ @current_states_node << node['States']
43
+ start_at = node['StartAt']
44
+ if start_at && start_at.is_a?(String)
45
+ @current_states_incoming << [ start_at ]
46
+ if !node['States'].key?(start_at)
47
+ problems <<
48
+ "StartAt value #{start_at} not found in " +
49
+ "States field at #{path}"
50
+ end
51
+ else
52
+ @current_states_incoming << []
53
+ end
54
+
55
+ node['States'].keys.each do |name|
56
+ if @all_state_names[name]
57
+ problems <<
58
+ "State \"#{name}\", defined at #{path}.States, " +
59
+ "is also defined at #{@all_state_names[name]}"
60
+ else
61
+ @all_state_names[name] = "#{path}.States"
62
+ end
63
+ end
64
+ end
65
+
66
+ check_for_terminal(node, path, problems)
67
+
68
+ check_next(node, path, problems)
69
+
70
+ check_States_ALL(node['Retry'], path + '.Retry', problems)
71
+ check_States_ALL(node['Catch'], path + '.Catch', problems)
72
+
73
+ node.each do |name, val|
74
+ if val.is_a?(Array)
75
+ i = 0
76
+ val.each do |element|
77
+ check(element, "#{path}.#{name}[#{i}]", problems)
78
+ i += 1
79
+ end
80
+ else
81
+ check(val, "#{path}.#{name}", problems)
82
+ end
83
+ end
84
+
85
+ if is_machine_top
86
+ states = @current_states_node.pop
87
+ incoming = @current_states_incoming.pop
88
+ missing = states.keys - incoming
89
+ missing.each do |state|
90
+ problems << "No transition found to state #{path}.#{state}"
91
+ end
92
+ end
93
+ end
94
+
95
+ def check_next(node, path, problems)
96
+ add_next(node, path, 'Next', problems)
97
+ add_next(node, path, 'Default', problems)
98
+ end
99
+
100
+ def add_next(node, path, field, problems)
101
+ if node[field] && node[field].is_a?(String)
102
+ transition_to = node[field]
103
+
104
+ if !@current_states_node.empty?
105
+ if @current_states_node[-1].key?(transition_to)
106
+ @current_states_incoming[-1] << transition_to
107
+ else
108
+ problems <<
109
+ "No state found named \"#{transition_to}\", referenced at " +
110
+ "#{path}.#{field}"
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def check_for_terminal(node, path, problems)
117
+ if node['States'] && node['States'].is_a?(Hash)
118
+ terminal_found = false
119
+ node['States'].each_value do |state_node|
120
+ if state_node.is_a?(Hash)
121
+ if [ 'Succeed', 'Fail' ].include?(state_node['Type'])
122
+ terminal_found = true
123
+ elsif state_node['End'] == true
124
+ terminal_found = true
125
+ end
126
+ end
127
+ end
128
+
129
+ if !terminal_found
130
+ problems << "No terminal state found in machine at #{path}.States"
131
+ end
132
+ end
133
+ end
134
+
135
+ def check_States_ALL(node, path, problems)
136
+ if !node.is_a?(Array)
137
+ return
138
+ end
139
+
140
+ i = 0
141
+ node.each do |element|
142
+ if element.is_a?(Hash)
143
+ if element['ErrorEquals'].is_a?(Array)
144
+ ee = element['ErrorEquals']
145
+ if ee.include? 'States.ALL'
146
+ if i != (node.size - 1) || ee.size != 1
147
+ problems <<
148
+ "#{path}[#{i}]: States.ALL can only appear in the last " +
149
+ "element, and by itself."
150
+ end
151
+ end
152
+ end
153
+ end
154
+ i += 1
155
+ end
156
+ end
157
+ end
158
+ end
159
+
data/statelint.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'statelint'
3
+ s.version = '0.1.0'
4
+ s.date = '2016-09-28'
5
+ s.summary = "State Machine JSON validator"
6
+ s.description = "Validates a JSON object representing a State Machine"
7
+ s.authors = ["Tim Bray"]
8
+ s.email = 'timbray@amazon.com'
9
+ s.executables << 'statelint'
10
+ s.files = `git ls-files`.split("\n").reject do |f|
11
+ f.match(%r{^(spec|test)/})
12
+ end
13
+
14
+ s.homepage = 'http://rubygems.org/gems/statelint'
15
+ s.license = 'Apache 2.0'
16
+ s.add_runtime_dependency "j2119"
17
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statelint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Bray
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: j2119
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Validates a JSON object representing a State Machine
28
+ email: timbray@amazon.com
29
+ executables:
30
+ - statelint
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE
37
+ - NOTICE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/statelint
41
+ - data/StateMachine.j2119
42
+ - lib/statelint.rb
43
+ - lib/statelint/state_node.rb
44
+ - statelint.gemspec
45
+ homepage: http://rubygems.org/gems/statelint
46
+ licenses:
47
+ - Apache 2.0
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.14.1
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: State Machine JSON validator
69
+ test_files: []