Skip to content

Commit a828f25

Browse files
committed
Allow procedures without validation blocks
Updated the `procedure` method to support registration without a validation block, defaulting to an empty contract. Simplified example usages in configuration files to reflect this change.
1 parent 34db240 commit a828f25

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

examples/procedures.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,5 @@
5252
end
5353

5454
# Used only to test internal server errors
55-
procedure(:explode) do
56-
params do
57-
# No params
58-
end
59-
end
55+
procedure(:explode)
6056
end

examples/rails-single-file-routing/config.ru

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ JSONRPC.configure do |config|
2222
end
2323
end
2424

25-
procedure(:ping) do
26-
params do
27-
# no params
28-
end
29-
end
25+
procedure(:ping)
3026
end
3127

3228
# Define the application

examples/rails/config/initializers/jsonrpc.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,5 @@
5454
# end
5555
#
5656
# # Used only to test internal server errors
57-
# procedure(:explode) do
58-
# params do
59-
# # No params
60-
# end
61-
# end
57+
# procedure(:explode)
6258
# end

lib/jsonrpc/configuration.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,24 @@ def self.instance
162162
# end
163163
# end
164164
#
165+
# @example Register a procedure without validation
166+
# config.procedure('ping')
167+
#
165168
# @param method_name [String, Symbol] the name of the procedure
166169
# @param allow_positional_arguments [Boolean] whether the procedure accepts positional arguments
167170
#
168-
# @yield A block that defines the validation contract using Dry::Validation DSL
171+
# @yield [optional] A block that defines the validation contract using Dry::Validation DSL
169172
#
170173
# @return [Procedure] the registered procedure
171174
#
172-
def procedure(method_name, allow_positional_arguments: false, &)
173-
contract_class = Class.new(Dry::Validation::Contract, &)
175+
def procedure(method_name, allow_positional_arguments: false, &block)
176+
contract_class = if block
177+
Class.new(Dry::Validation::Contract, &block)
178+
else
179+
Class.new(Dry::Validation::Contract) do
180+
params {} # rubocop:disable Lint/EmptyBlock
181+
end
182+
end
174183
contract_class.class_eval { import_predicates_as_macros }
175184
contract = contract_class.new
176185

0 commit comments

Comments
 (0)