33module Grape
44 module DSL
55 module Validations
6+ # Opens a root-level ParamsScope, defining parameter coercions and
7+ # validations for the endpoint.
8+ # @yield instance context of the new scope
9+ def params ( &block )
10+ Grape ::Validations ::ParamsScope . new ( api : self , type : Hash , &block )
11+ end
12+
13+ # Declare the contract to be used for the endpoint's parameters.
14+ # @param contract [Class<Dry::Validation::Contract> | Dry::Schema::Processor]
15+ # The contract or schema to be used for validation. Optional.
16+ # @yield a block yielding a new instance of Dry::Schema::Params
17+ # subclass, allowing to define the schema inline. When the
18+ # +contract+ parameter is a schema, it will be used as a parent. Optional.
19+ def contract ( contract = nil , &block )
20+ raise ArgumentError , 'Either contract or block must be provided' unless contract || block
21+ raise ArgumentError , 'Cannot inherit from contract, only schema' if block && contract . respond_to? ( :schema )
22+
23+ Grape ::Validations ::ContractScope . new ( self , contract , &block )
24+ end
25+
26+ private
27+
628 # Clears all defined parameters and validations. The main purpose of it is to clean up
729 # settings, so next endpoint won't interfere with previous one.
830 #
@@ -24,26 +46,6 @@ module Validations
2446 def reset_validations!
2547 inheritable_setting . namespace_stackable . delete ( :declared_params , :params , :validations )
2648 end
27-
28- # Opens a root-level ParamsScope, defining parameter coercions and
29- # validations for the endpoint.
30- # @yield instance context of the new scope
31- def params ( &block )
32- Grape ::Validations ::ParamsScope . new ( api : self , type : Hash , &block )
33- end
34-
35- # Declare the contract to be used for the endpoint's parameters.
36- # @param contract [Class<Dry::Validation::Contract> | Dry::Schema::Processor]
37- # The contract or schema to be used for validation. Optional.
38- # @yield a block yielding a new instance of Dry::Schema::Params
39- # subclass, allowing to define the schema inline. When the
40- # +contract+ parameter is a schema, it will be used as a parent. Optional.
41- def contract ( contract = nil , &block )
42- raise ArgumentError , 'Either contract or block must be provided' unless contract || block
43- raise ArgumentError , 'Cannot inherit from contract, only schema' if block && contract . respond_to? ( :schema )
44-
45- Grape ::Validations ::ContractScope . new ( self , contract , &block )
46- end
4749 end
4850 end
4951end
0 commit comments