Skip to content

Commit 8e3d14b

Browse files
committed
Add Params extension with schema definition capability
Adds the basic structure for the Params extension which allows operations to define dry-schema contracts for input validation. The extension provides a class-level params method to define schemas, with proper inheritance support for subclasses.
1 parent b288d55 commit 8e3d14b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
begin
4+
require "dry/schema"
5+
rescue LoadError
6+
raise Dry::Operation::MissingDependencyError.new(gem: "dry-schema", extension: "Params")
7+
end
8+
9+
module Dry
10+
class Operation
11+
module Extensions
12+
# Add params validation support to operations using dry-schema
13+
#
14+
# When this extension is included, you can use a class-level `params` method
15+
# to define a schema for validating the input to your operation's methods.
16+
#
17+
# @see https://dry-rb.org/gems/dry-schema/
18+
module Params
19+
def self.included(klass)
20+
klass.extend(ClassMethods)
21+
end
22+
23+
# Class methods added to the operation class
24+
module ClassMethods
25+
# Define a params schema for validating operation inputs
26+
#
27+
# The block is passed to `Dry::Schema.Params` to define the validation rules.
28+
#
29+
# @yield Block for defining the schema using dry-schema DSL
30+
# @return [Dry::Schema::Params] The compiled schema
31+
def params(&block)
32+
@_params_schema = Dry::Schema.Params(&block)
33+
end
34+
35+
# @api private
36+
def _params_schema
37+
@_params_schema
38+
end
39+
40+
# @api private
41+
def inherited(subclass)
42+
super
43+
if defined?(@_params_schema) && @_params_schema
44+
subclass.instance_variable_set(:@_params_schema, @_params_schema)
45+
end
46+
end
47+
end
48+
end
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)