dry-operation 1.0.1 → 1.2.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 +68 -6
- data/LICENSE +1 -2
- data/README.md +6 -1
- data/dry-operation.gemspec +4 -5
- data/lib/dry/operation/class_context/prepend_manager.rb +4 -0
- data/lib/dry/operation/errors.rb +1 -2
- data/lib/dry/operation/extensions/rom.rb +30 -20
- data/lib/dry/operation/extensions/validation.rb +291 -0
- data/lib/dry/operation/version.rb +1 -1
- data/lib/dry/operation.rb +7 -6
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 631f9fa95ca8e0d6c5b045747a3367ab717ed748b364d85b72fd538c2191e9dd
|
|
4
|
+
data.tar.gz: '08619ae70bf36118fda2d005793a68859bc4e476d8e3c1617ea6464bfe6f5b5c'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 619bd04e70bed1121e5e23a006b5ce5bf6de936352252aedae8b283f079741d787f19688159ad6186de7076d6d71be86c387d1f5eb08f60784adc1ce4082c6f9
|
|
7
|
+
data.tar.gz: 4cc801afa0a7ce522af51de2c7ad448fd707fa429e2daa49ccf7de2c946b655409c4da750faec2d33ddcf62c1bc5d1dce46210d71910d21a30d9f78f514c0ec1
|
data/CHANGELOG.md
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
-
and this project adheres to [Break Versioning](https://www.taoensso.com/break-versioning).
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Break Versioning](https://www.taoensso.com/break-versioning).
|
|
7
6
|
|
|
8
7
|
## [Unreleased]
|
|
9
8
|
|
|
@@ -19,17 +18,80 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
|
|
|
19
18
|
|
|
20
19
|
### Security
|
|
21
20
|
|
|
21
|
+
[Unreleased]: https://github.com/dry-rb/dry-operation/compare/v1.2.0...main
|
|
22
|
+
|
|
23
|
+
## [1.2.0] - 2026-11-20
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
|
|
27
|
+
- Add a Validation extension, for validating operation input with Dry Validation. (@aaronmallen and @timriley in #34, #47)
|
|
28
|
+
|
|
29
|
+
Define a contract with `params`, `schema`, or `contract`. The operation's keyword arguments are validated before the method runs, and the method receives the contract's output. If validation fails, the operation returns `Failure[:invalid, result]`.
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
class CreateUser < Dry::Operation
|
|
33
|
+
include Dry::Operation::Extensions::Validation
|
|
34
|
+
|
|
35
|
+
params do
|
|
36
|
+
required(:name).filled(:string)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def call(name:)
|
|
40
|
+
# ...
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
By default, all keyword arguments are validated. Use `for:` to validate only one keyword argument, or `keys:` to validate a subset of them. All other keyword arguments are passed through untouched. Positional arguments are never validated.
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
params(for: :attrs) do
|
|
49
|
+
required(:name).filled(:string)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
params(keys: %i[account_id scope]) do
|
|
53
|
+
required(:account_id).filled(:string)
|
|
54
|
+
optional(:scope).array(:string)
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- `#step` now accepts any object that responds to `#to_result`, such as `Try`, `Maybe`, or `Validated` from Dry Monads. The object is converted to a result before it is processed. (@timriley in #38)
|
|
59
|
+
|
|
60
|
+
### Changed
|
|
61
|
+
|
|
62
|
+
- Require dry-monads 1.10 or later. This makes sure that dry-monads' RSpec extension is available for testing your operations. (@timriley in #45)
|
|
63
|
+
- Require Ruby 3.3 or later. (@timriley)
|
|
64
|
+
|
|
65
|
+
[1.2.0]: https://github.com/dry-rb/dry-operation/compare/v1.1.0...v1.2.0
|
|
66
|
+
|
|
67
|
+
## [1.1.0] - 2026-02-06
|
|
68
|
+
|
|
69
|
+
### Changed
|
|
70
|
+
|
|
71
|
+
- In Rom extension, allow transaction options to be passed via `#transaction`. (@wuarmin in #37)
|
|
72
|
+
```ruby
|
|
73
|
+
# Set options at extension time (used for all transactions within the class):
|
|
74
|
+
include Dry::Operation::Extensions::ROM[isolation: :serializable]
|
|
75
|
+
|
|
76
|
+
# Or per-transaction, in instance methods. These will be merged with the extension-level
|
|
77
|
+
# options.
|
|
78
|
+
transaction(savepoint: true) do
|
|
79
|
+
# This transaction will have options `isolation: :serializable, savepoint: true`
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
[1.1.0]: https://github.com/dry-rb/dry-operation/compare/v1.0.1...v1.1.0
|
|
84
|
+
|
|
22
85
|
## [1.0.1] - 2025-10-24
|
|
23
86
|
|
|
24
87
|
### Changed
|
|
25
88
|
|
|
26
89
|
- Define `#transaction` method in extension modules themselves (which are included into the operation class), rather than directly on the operation class itself. This adheres to typical Ruby inheritance-based method lookup, and allows for overloads of `#transaction` to be defined directly in the operation class or mixed in via other modules. (@timriley in #33)
|
|
27
90
|
|
|
28
|
-
|
|
91
|
+
[1.0.1]: https://github.com/dry-rb/dry-operation/compare/v1.0.0...v1.0.1
|
|
92
|
+
|
|
93
|
+
## 1.0.0 - 2024-11-02
|
|
29
94
|
|
|
30
95
|
### Added
|
|
31
96
|
|
|
32
97
|
- Initial release. (@waiting-for-dev)
|
|
33
|
-
|
|
34
|
-
[1.0.1]: https://github.com/dry-rb/dry-operation/compare/v1.0.0...v1.0.1
|
|
35
|
-
[1.0.0]: https://github.com/dry-rb/dry-operation/releases/tag/v1.0.0
|
data/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2015-
|
|
3
|
+
Copyright (c) 2015-2026 Hanakai team
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
6
|
this software and associated documentation files (the "Software"), to deal in
|
|
@@ -18,4 +18,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
18
18
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
19
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
20
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
-
|
data/README.md
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
<!--- This file is synced from hanakai-rb/repo-sync -->
|
|
2
2
|
|
|
3
|
-
[rubygem]: https://rubygems.org/gems/dry-operation
|
|
4
3
|
[actions]: https://github.com/dry-rb/dry-operation/actions
|
|
4
|
+
[chat]: https://discord.gg/naQApPAsZB
|
|
5
|
+
[forum]: https://discourse.hanakai.org
|
|
6
|
+
[rubygem]: https://rubygems.org/gems/dry-operation
|
|
5
7
|
|
|
6
8
|
# dry-operation [][rubygem] [][actions]
|
|
7
9
|
|
|
10
|
+
[][forum]
|
|
11
|
+
[][chat]
|
|
12
|
+
|
|
8
13
|
## Links
|
|
9
14
|
|
|
10
15
|
- [User documentation](https://dry-rb.org/gems/dry-operation)
|
data/dry-operation.gemspec
CHANGED
|
@@ -17,21 +17,20 @@ Gem::Specification.new do |spec|
|
|
|
17
17
|
spec.description = spec.summary
|
|
18
18
|
spec.homepage = "https://dry-rb.org/gems/dry-operation"
|
|
19
19
|
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-operation.gemspec", "lib/**/*"]
|
|
20
|
-
spec.bindir = "
|
|
21
|
-
spec.executables = []
|
|
20
|
+
spec.bindir = "exe"
|
|
21
|
+
spec.executables = Dir["exe/*"].map { |f| File.basename(f) }
|
|
22
22
|
spec.require_paths = ["lib"]
|
|
23
23
|
|
|
24
24
|
spec.extra_rdoc_files = ["README.md", "CHANGELOG.md", "LICENSE"]
|
|
25
25
|
|
|
26
|
-
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
27
26
|
spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-operation/blob/main/CHANGELOG.md"
|
|
28
27
|
spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-operation"
|
|
29
28
|
spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-operation/issues"
|
|
30
29
|
spec.metadata["funding_uri"] = "https://github.com/sponsors/hanami"
|
|
31
30
|
|
|
32
|
-
spec.required_ruby_version = ">= 3.
|
|
31
|
+
spec.required_ruby_version = ">= 3.3"
|
|
33
32
|
|
|
34
|
-
spec.add_runtime_dependency "dry-monads", "~> 1.
|
|
33
|
+
spec.add_runtime_dependency "dry-monads", "~> 1.10"
|
|
35
34
|
spec.add_runtime_dependency "zeitwerk", "~> 2.6"
|
|
36
35
|
spec.add_development_dependency "bundler"
|
|
37
36
|
spec.add_development_dependency "rspec"
|
data/lib/dry/operation/errors.rb
CHANGED
|
@@ -38,8 +38,7 @@ module Dry
|
|
|
38
38
|
class InvalidStepResultError < Error
|
|
39
39
|
def initialize(result:)
|
|
40
40
|
super <<~MSG
|
|
41
|
-
Your step must return `Success(..)
|
|
42
|
-
from `Dry::Monads::Result`. Instead, it was `#{result.inspect}`.
|
|
41
|
+
Your step must return `Success(..)`, `Failure(..)`, or an object with `.to_result`. Instead, it was `#{result.inspect}`.
|
|
43
42
|
MSG
|
|
44
43
|
end
|
|
45
44
|
end
|
|
@@ -9,15 +9,14 @@ end
|
|
|
9
9
|
module Dry
|
|
10
10
|
class Operation
|
|
11
11
|
module Extensions
|
|
12
|
-
# Add
|
|
12
|
+
# Add Rom transaction support to operations.
|
|
13
13
|
#
|
|
14
|
-
# When this extension is included, you can use a `#transaction` method
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
# back and, as usual, the rest of the flow will be skipped.
|
|
14
|
+
# When this extension is included, you can use a `#transaction` method to wrap the desired
|
|
15
|
+
# steps in a Rom transaction. If any of the steps returns a `Dry::Monads::Result::Failure`,
|
|
16
|
+
# the transaction will be rolled back and, as usual, the rest of the flow will be skipped.
|
|
18
17
|
#
|
|
19
|
-
# The extension expects the including class to give access to the rom
|
|
20
|
-
#
|
|
18
|
+
# The extension expects the including class to give access to the Rom container via a `#rom`
|
|
19
|
+
# method.
|
|
21
20
|
#
|
|
22
21
|
# ```ruby
|
|
23
22
|
# require "dry/operation/extensions/rom"
|
|
@@ -46,18 +45,27 @@ module Dry
|
|
|
46
45
|
# end
|
|
47
46
|
# ```
|
|
48
47
|
#
|
|
49
|
-
# By default, the `:default` gateway will be used
|
|
50
|
-
#
|
|
48
|
+
# By default, the `:default` gateway will be used and no additional
|
|
49
|
+
# options will be passed to the Rom transaction.
|
|
50
|
+
#
|
|
51
|
+
# You can change the default gateway and transaction options when
|
|
52
|
+
# including the extension:
|
|
51
53
|
#
|
|
52
54
|
# ```ruby
|
|
53
|
-
# include Dry::Operation::Extensions::ROM[
|
|
55
|
+
# include Dry::Operation::Extensions::ROM[
|
|
56
|
+
# gateway: :my_gateway,
|
|
57
|
+
# isolation: :serializable
|
|
58
|
+
# ]
|
|
54
59
|
# ```
|
|
55
60
|
#
|
|
56
|
-
#
|
|
61
|
+
# You can also override the gateway and/or transaction options at runtime:
|
|
57
62
|
#
|
|
58
63
|
# ```ruby
|
|
59
|
-
# user = transaction(
|
|
60
|
-
#
|
|
64
|
+
# user = transaction(
|
|
65
|
+
# gateway: :my_gateway,
|
|
66
|
+
# isolation: :serializable
|
|
67
|
+
# ) do
|
|
68
|
+
# # ...
|
|
61
69
|
# end
|
|
62
70
|
# ```
|
|
63
71
|
#
|
|
@@ -84,29 +92,31 @@ module Dry
|
|
|
84
92
|
# Include the extension providing a custom gateway
|
|
85
93
|
#
|
|
86
94
|
# @param gateway [Symbol] the rom gateway to use
|
|
87
|
-
def self.[](gateway: DEFAULT_GATEWAY)
|
|
88
|
-
Builder.new(gateway: gateway)
|
|
95
|
+
def self.[](gateway: DEFAULT_GATEWAY, **options)
|
|
96
|
+
Builder.new(gateway: gateway, **options)
|
|
89
97
|
end
|
|
90
98
|
|
|
91
99
|
# @api private
|
|
92
100
|
class Builder < Module
|
|
93
|
-
def initialize(gateway
|
|
101
|
+
def initialize(gateway:, **options)
|
|
94
102
|
super()
|
|
95
103
|
@gateway = gateway
|
|
104
|
+
@options = options
|
|
96
105
|
end
|
|
97
106
|
|
|
98
107
|
def included(_klass)
|
|
99
108
|
default_gateway = @gateway
|
|
109
|
+
default_options = @options
|
|
100
110
|
|
|
101
|
-
define_method(:transaction) do |gateway: default_gateway, &steps|
|
|
111
|
+
define_method(:transaction) do |gateway: default_gateway, **opts, &steps|
|
|
102
112
|
raise Dry::Operation::ExtensionError, <<~MSG unless respond_to?(:rom)
|
|
103
|
-
When using the
|
|
104
|
-
that returns the
|
|
113
|
+
When using the Rom extension, you need to define a #rom method \
|
|
114
|
+
that returns the Rom container
|
|
105
115
|
MSG
|
|
106
116
|
|
|
107
117
|
intercepting_failure do
|
|
108
118
|
result = nil
|
|
109
|
-
rom.gateways[gateway].transaction do |t|
|
|
119
|
+
rom.gateways[gateway].transaction(**default_options.merge(opts)) do |t|
|
|
110
120
|
intercepting_failure(->(failure) {
|
|
111
121
|
result = failure
|
|
112
122
|
t.rollback!
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
begin
|
|
4
|
+
require "dry/validation"
|
|
5
|
+
rescue LoadError
|
|
6
|
+
raise Dry::Operation::MissingDependencyError.new(gem: "dry-validation", extension: "Validation")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module Dry
|
|
10
|
+
class Operation
|
|
11
|
+
module Extensions
|
|
12
|
+
# Adds validation support to operations using Dry Validation.
|
|
13
|
+
#
|
|
14
|
+
# When this extension is included, define your contract on your operation class using
|
|
15
|
+
# `params`, `schema`, or `contract`, or make a `#contract` dependency available from your
|
|
16
|
+
# operation instance. The operation's keyword arguments are validated before its method body
|
|
17
|
+
# runs, and the method receives the contract's output in place of the input it was called with.
|
|
18
|
+
# Positional arguments are never validated, and are always passed through untouched.
|
|
19
|
+
#
|
|
20
|
+
# By default, the keyword arguments are validated as a whole:
|
|
21
|
+
#
|
|
22
|
+
# ```
|
|
23
|
+
# class CreateUser < Dry::Operation
|
|
24
|
+
# include Dry::Operation::Extensions::Validation
|
|
25
|
+
#
|
|
26
|
+
# params do
|
|
27
|
+
# required(:name).filled(:string)
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# def call(name:)
|
|
31
|
+
# # ...
|
|
32
|
+
# end
|
|
33
|
+
# end
|
|
34
|
+
#
|
|
35
|
+
# CreateUser.new.call(name: "Alice")
|
|
36
|
+
# ```
|
|
37
|
+
#
|
|
38
|
+
# The method receives only the keyword arguments in the contract's output. Any keyword
|
|
39
|
+
# arguments not declared by the contract are dropped. To pass other keyword arguments through
|
|
40
|
+
# to the method, use `for:` or `keys:`.
|
|
41
|
+
#
|
|
42
|
+
# Use `for:` to validate the value of a single keyword argument, leaving the others untouched:
|
|
43
|
+
#
|
|
44
|
+
# ```
|
|
45
|
+
# class UpdateUser < Dry::Operation
|
|
46
|
+
# include Dry::Operation::Extensions::Validation
|
|
47
|
+
#
|
|
48
|
+
# params(for: :attrs) do
|
|
49
|
+
# required(:name).filled(:string)
|
|
50
|
+
# end
|
|
51
|
+
#
|
|
52
|
+
# def call(user, attrs:, notify: false)
|
|
53
|
+
# # ...
|
|
54
|
+
# end
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# UpdateUser.new.call(user, attrs: {name: "Alice", admin: true}, notify: true)
|
|
58
|
+
# # user is passed through untouched
|
|
59
|
+
# # attrs is {name: "Alice"}, with admin filtered out by the contract
|
|
60
|
+
# # notify is passed through untouched
|
|
61
|
+
# ```
|
|
62
|
+
#
|
|
63
|
+
# Use `keys:` to validate a subset of the keyword arguments, leaving the others untouched:
|
|
64
|
+
#
|
|
65
|
+
# ```
|
|
66
|
+
# class AuthorizeCode < Dry::Operation
|
|
67
|
+
# include Dry::Operation::Extensions::Validation
|
|
68
|
+
#
|
|
69
|
+
# params(keys: %i[account_id scope]) do
|
|
70
|
+
# required(:account_id).filled(:string)
|
|
71
|
+
# optional(:scope).array(:string)
|
|
72
|
+
# end
|
|
73
|
+
#
|
|
74
|
+
# def call(code, account_id:, scope: [], notify: false)
|
|
75
|
+
# # ...
|
|
76
|
+
# end
|
|
77
|
+
# end
|
|
78
|
+
# ```
|
|
79
|
+
#
|
|
80
|
+
# When a `for:` keyword argument is not given, there is nothing to validate, so the method is
|
|
81
|
+
# called without it, and its own signature decides whether that is allowed.
|
|
82
|
+
#
|
|
83
|
+
# When there is no contract defined, all arguments are forwarded untouched.
|
|
84
|
+
#
|
|
85
|
+
# @see https://hanakai.org/learn/dry/dry-validation
|
|
86
|
+
#
|
|
87
|
+
# @api public
|
|
88
|
+
# @since 1.2.0
|
|
89
|
+
module Validation
|
|
90
|
+
CONTRACT_CLASS_NAME = "Contract"
|
|
91
|
+
|
|
92
|
+
# The config for how an operation validates its keyword arguments: the contract class, and
|
|
93
|
+
# which keyword arguments are its input.
|
|
94
|
+
#
|
|
95
|
+
# @api private
|
|
96
|
+
Config = Data.define(:contract_class, :for_key, :keys) do
|
|
97
|
+
def initialize(contract_class: nil, for_key: nil, keys: nil)
|
|
98
|
+
raise ArgumentError, "pass either for: or keys:, not both" if for_key && keys
|
|
99
|
+
|
|
100
|
+
super
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
DEFAULT_CONFIG = Config.new
|
|
105
|
+
|
|
106
|
+
def self.included(klass)
|
|
107
|
+
klass.extend(ClassMethods)
|
|
108
|
+
klass.prepend(InstanceMethods)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# @api public
|
|
112
|
+
# @since 1.2.0
|
|
113
|
+
module InstanceMethods
|
|
114
|
+
include Dry::Monads::Result::Mixin
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# Returns the contract to use for validation.
|
|
119
|
+
#
|
|
120
|
+
# Uses an existing `@contract`, if present, otherwise initializes an instance of the
|
|
121
|
+
# contract defined on the class, and stores it as `@contract`.
|
|
122
|
+
#
|
|
123
|
+
# @return [Dry::Validation::Contract, nil]
|
|
124
|
+
#
|
|
125
|
+
# @api private
|
|
126
|
+
def contract
|
|
127
|
+
return @contract if defined?(@contract)
|
|
128
|
+
|
|
129
|
+
@contract = self.class.contract_class&.new
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Validates the input using the operation's {#contract}.
|
|
133
|
+
#
|
|
134
|
+
# @param input [Hash] The input to validate
|
|
135
|
+
# @return [Dry::Monads::Result] Success with validated input, or Failure with result
|
|
136
|
+
#
|
|
137
|
+
# @api private
|
|
138
|
+
def validate(input)
|
|
139
|
+
return Success(input) unless contract
|
|
140
|
+
|
|
141
|
+
result = contract.call(input)
|
|
142
|
+
|
|
143
|
+
if result.success?
|
|
144
|
+
Success(result.to_h)
|
|
145
|
+
else
|
|
146
|
+
Failure[:invalid, result]
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @api public
|
|
152
|
+
# @since 1.2.0
|
|
153
|
+
module ClassMethods
|
|
154
|
+
# @return [Config]
|
|
155
|
+
#
|
|
156
|
+
# @api private
|
|
157
|
+
def validation_config
|
|
158
|
+
@validation_config || DEFAULT_CONFIG
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# @return [Class, nil]
|
|
162
|
+
#
|
|
163
|
+
# @api private
|
|
164
|
+
def contract_class
|
|
165
|
+
validation_config.contract_class
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Defines a validation contract using the full contract DSL.
|
|
169
|
+
#
|
|
170
|
+
# @param klass [Class, nil] A Dry::Validation::Contract subclass to use
|
|
171
|
+
# @param for [Symbol, nil] The keyword argument whose value to validate
|
|
172
|
+
# @param keys [Array<Symbol>, nil] The keyword arguments to validate together
|
|
173
|
+
# @yield Block for defining contract rules
|
|
174
|
+
# @return [void]
|
|
175
|
+
#
|
|
176
|
+
# @api public
|
|
177
|
+
# @since 1.2.0
|
|
178
|
+
def contract(klass = nil, for: nil, keys: nil, &block)
|
|
179
|
+
# Build the config first, to validate options
|
|
180
|
+
config = Config.new(for_key: binding.local_variable_get(:for), keys:)
|
|
181
|
+
|
|
182
|
+
if klass.nil?
|
|
183
|
+
klass = Class.new(Dry::Validation::Contract, &block)
|
|
184
|
+
const_set(CONTRACT_CLASS_NAME, klass)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
@validation_config = config.with(contract_class: klass)
|
|
188
|
+
_apply_validation
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Defines a validation contract using the schema DSL only.
|
|
192
|
+
#
|
|
193
|
+
# @param for [Symbol, nil] The keyword argument whose value to validate
|
|
194
|
+
# @param keys [Array<Symbol>, nil] The keyword arguments to validate together
|
|
195
|
+
# @yield Block for defining schema validation rules
|
|
196
|
+
# @return [void]
|
|
197
|
+
#
|
|
198
|
+
# @api public
|
|
199
|
+
# @since 1.2.0
|
|
200
|
+
def schema(for: nil, keys: nil, &block)
|
|
201
|
+
raise ArgumentError, "schema requires a block" unless block_given?
|
|
202
|
+
|
|
203
|
+
contract(for: binding.local_variable_get(:for), keys:) { schema(&block) }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Defines a validation contract using the params schema DSL only.
|
|
207
|
+
#
|
|
208
|
+
# @param for [Symbol, nil] The keyword argument whose value to validate
|
|
209
|
+
# @param keys [Array<Symbol>, nil] The keyword arguments to validate together
|
|
210
|
+
# @yield Block for defining params validation rules
|
|
211
|
+
# @return [void]
|
|
212
|
+
#
|
|
213
|
+
# @api public
|
|
214
|
+
# @since 1.2.0
|
|
215
|
+
def params(for: nil, keys: nil, &block)
|
|
216
|
+
raise ArgumentError, "params requires a block" unless block_given?
|
|
217
|
+
|
|
218
|
+
contract(for: binding.local_variable_get(:for), keys:) { params(&block) }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
private
|
|
222
|
+
|
|
223
|
+
def method_added(method_name)
|
|
224
|
+
return unless @_prepend_manager.registered_methods.include?(method_name)
|
|
225
|
+
|
|
226
|
+
_apply_validation(method_name)
|
|
227
|
+
super
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def inherited(subclass)
|
|
231
|
+
super
|
|
232
|
+
|
|
233
|
+
if defined?(@validation_config)
|
|
234
|
+
subclass.instance_variable_set(:@validation_config, @validation_config)
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def _apply_validation(*method_names)
|
|
239
|
+
method_names = @_prepend_manager.registered_methods if method_names.empty?
|
|
240
|
+
method_names &= @_prepend_manager.registered_methods
|
|
241
|
+
|
|
242
|
+
@_validated_methods ||= []
|
|
243
|
+
|
|
244
|
+
method_names.each do |method_name|
|
|
245
|
+
next if @_validated_methods.include?(method_name)
|
|
246
|
+
|
|
247
|
+
prepend ValidationStep.new(method_name)
|
|
248
|
+
@_validated_methods << method_name
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# @api private
|
|
254
|
+
class ValidationStep < Module
|
|
255
|
+
def initialize(method_name)
|
|
256
|
+
super()
|
|
257
|
+
@method_name = method_name
|
|
258
|
+
define_validation_method
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def name
|
|
262
|
+
"Dry::Operation::Extensions::Validation::ValidationStep[#{@method_name}]"
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
private
|
|
266
|
+
|
|
267
|
+
def define_validation_method
|
|
268
|
+
define_method(@method_name) do |*args, **kwargs, &block|
|
|
269
|
+
# With no contract there's nothing to validate, so forward all args.
|
|
270
|
+
return super(*args, **kwargs, &block) unless contract
|
|
271
|
+
|
|
272
|
+
config = self.class.validation_config
|
|
273
|
+
|
|
274
|
+
# Provide the contract's output as the input arguments for this method call.
|
|
275
|
+
if (key = config.for_key)
|
|
276
|
+
# When the key is missing, the method's own signature decides if that is allowed.
|
|
277
|
+
kwargs = kwargs.merge(key => step(validate(kwargs[key]))) if kwargs.key?(key)
|
|
278
|
+
elsif (keys = config.keys)
|
|
279
|
+
kwargs = kwargs.except(*keys).merge(step(validate(kwargs.slice(*keys))))
|
|
280
|
+
else
|
|
281
|
+
kwargs = step(validate(kwargs))
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
super(*args, **kwargs, &block)
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
end
|
data/lib/dry/operation.rb
CHANGED
|
@@ -159,15 +159,16 @@ module Dry
|
|
|
159
159
|
#
|
|
160
160
|
# Throws `:halt` with a {Dry::Monads::Result::Failure} on failure.
|
|
161
161
|
#
|
|
162
|
-
#
|
|
162
|
+
# If the given result responds to `#to_result`, this will be called before processing.
|
|
163
|
+
#
|
|
164
|
+
# @param result [Dry::Monads::Result, #to_result]
|
|
163
165
|
# @return [Object] wrapped value
|
|
164
166
|
# @see #steps
|
|
165
167
|
def step(result)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
end
|
|
168
|
+
raise InvalidStepResultError.new(result: result) unless result.respond_to?(:to_result)
|
|
169
|
+
|
|
170
|
+
result = result.to_result
|
|
171
|
+
result.value_or { throw_failure(result) }
|
|
171
172
|
end
|
|
172
173
|
|
|
173
174
|
# Invokes a callable in case of block's failure
|
metadata
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dry-operation
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hanakai team
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '1.
|
|
18
|
+
version: '1.10'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '1.
|
|
25
|
+
version: '1.10'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: zeitwerk
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -101,12 +101,12 @@ files:
|
|
|
101
101
|
- lib/dry/operation/extensions/active_record.rb
|
|
102
102
|
- lib/dry/operation/extensions/rom.rb
|
|
103
103
|
- lib/dry/operation/extensions/sequel.rb
|
|
104
|
+
- lib/dry/operation/extensions/validation.rb
|
|
104
105
|
- lib/dry/operation/version.rb
|
|
105
106
|
homepage: https://dry-rb.org/gems/dry-operation
|
|
106
107
|
licenses:
|
|
107
108
|
- MIT
|
|
108
109
|
metadata:
|
|
109
|
-
allowed_push_host: https://rubygems.org
|
|
110
110
|
changelog_uri: https://github.com/dry-rb/dry-operation/blob/main/CHANGELOG.md
|
|
111
111
|
source_code_uri: https://github.com/dry-rb/dry-operation
|
|
112
112
|
bug_tracker_uri: https://github.com/dry-rb/dry-operation/issues
|
|
@@ -118,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
118
118
|
requirements:
|
|
119
119
|
- - ">="
|
|
120
120
|
- !ruby/object:Gem::Version
|
|
121
|
-
version: 3.
|
|
121
|
+
version: '3.3'
|
|
122
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
requirements:
|
|
124
124
|
- - ">="
|