expectant 0.3.0 → 0.3.1
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/.rspec +3 -3
- data/.standard.yml +3 -3
- data/CHANGELOG.md +9 -9
- data/LICENSE.txt +21 -21
- data/README.md +152 -151
- data/Rakefile +10 -10
- data/lib/expectant/bound_schema.rb +81 -81
- data/lib/expectant/configuration.rb +12 -12
- data/lib/expectant/dsl.rb +120 -119
- data/lib/expectant/errors.rb +8 -8
- data/lib/expectant/expectation.rb +37 -37
- data/lib/expectant/schema.rb +94 -94
- data/lib/expectant/types.rb +56 -56
- data/lib/expectant/utils.rb +46 -46
- data/lib/expectant/version.rb +5 -5
- data/lib/expectant.rb +26 -26
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 06e38c364b8d2d666a81174ddafa2b426153ce1627c155bd996c8b1a912c6212
|
|
4
|
+
data.tar.gz: 7d5d662b9971375813d280f5c129aeb0ec80d960b5520368063714c7057ce794
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 04545df86e467ffcb8bd6c48f8cb702f90527ef5f9c5bf745d265bcc95ab80f84a606dafec9b57a345058d148ad61f7e7702476cbcc749d5471ed9ca7b5e2d7f
|
|
7
|
+
data.tar.gz: 7c427ebfc893ee72957783526adba4189f23e032f08c574ae9785932f01ac783cc7d0a4ae25a64743b5529d8c324ae966e1330ff79be3f869383c6ef796d5680
|
data/.rspec
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
--format documentation
|
|
2
|
-
--color
|
|
3
|
-
--require spec_helper
|
|
1
|
+
--format documentation
|
|
2
|
+
--color
|
|
3
|
+
--require spec_helper
|
data/.standard.yml
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
# For available configuration options, see:
|
|
2
|
-
# https://github.com/standardrb/standard
|
|
3
|
-
ruby_version: 3.1
|
|
1
|
+
# For available configuration options, see:
|
|
2
|
+
# https://github.com/standardrb/standard
|
|
3
|
+
ruby_version: 3.1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
## [Unreleased]
|
|
2
|
-
|
|
3
|
-
## [0.3.0] - 2025-10-09
|
|
4
|
-
|
|
5
|
-
- Re-wrote the API for simplicity
|
|
6
|
-
|
|
7
|
-
## [0.1.0] - 2025-10-02
|
|
8
|
-
|
|
9
|
-
- Initial release
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.3.0] - 2025-10-09
|
|
4
|
+
|
|
5
|
+
- Re-wrote the API for simplicity
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2025-10-02
|
|
8
|
+
|
|
9
|
+
- Initial release
|
data/LICENSE.txt
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Hector Medina Fetterman
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hector Medina Fetterman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,151 +1,152 @@
|
|
|
1
|
-
# Expectant
|
|
2
|
-
|
|
3
|
-
[](https://rubygems.org/gems/expectant)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
input :
|
|
23
|
-
input :
|
|
24
|
-
input :
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
input :
|
|
51
|
-
input :
|
|
52
|
-
input :
|
|
53
|
-
input :
|
|
54
|
-
input :
|
|
55
|
-
input :
|
|
56
|
-
input :
|
|
57
|
-
input :
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
expects :
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
config.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
bundle
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
1
|
+
# Expectant
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/expectant)
|
|
4
|
+
[](https://github.com/expectant/actions/workflows/main.yml)</span>
|
|
5
|
+
|
|
6
|
+
A Ruby DSL for defining validation schemas with type coercion, defaults, and fallbacks. Built on [dry-validation](https://dry-rb.org/gems/dry-validation/) and [dry-types](https://dry-rb.org/gems/dry-types/).
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'expectant'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick Start
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
class InvoiceService
|
|
18
|
+
include Expectant::DSL
|
|
19
|
+
|
|
20
|
+
expects :inputs
|
|
21
|
+
|
|
22
|
+
input :customer_id, type: :integer
|
|
23
|
+
input :amount, type: :float
|
|
24
|
+
input :status, type: :string, default: "draft"
|
|
25
|
+
input :description, type: :string, optional: true
|
|
26
|
+
|
|
27
|
+
input_rule(:amount) do
|
|
28
|
+
key.failure("must be positive") if value && value <= 0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def process(data)
|
|
32
|
+
result = inputs.validate(data)
|
|
33
|
+
|
|
34
|
+
if result.success?
|
|
35
|
+
# Use validated data: result.to_h
|
|
36
|
+
else
|
|
37
|
+
# Handle errors: result.errors.to_h
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Core Features
|
|
44
|
+
|
|
45
|
+
### Types
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
expects :inputs
|
|
49
|
+
|
|
50
|
+
input :name, type: :string
|
|
51
|
+
input :age, type: :integer
|
|
52
|
+
input :price, type: :float
|
|
53
|
+
input :active, type: :boolean
|
|
54
|
+
input :date, type: :date
|
|
55
|
+
input :time, type: :datetime
|
|
56
|
+
input :data, type: :hash
|
|
57
|
+
input :tags, type: :array
|
|
58
|
+
input :user, type: User # Custom class
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Defaults and Optional Fields
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
# Optional (can be omitted)
|
|
65
|
+
input :description, type: :string, optional: true
|
|
66
|
+
|
|
67
|
+
# Static default
|
|
68
|
+
input :status, type: :string, default: "draft"
|
|
69
|
+
|
|
70
|
+
# Dynamic default (proc)
|
|
71
|
+
input :created_at, type: :datetime, default: -> { Time.now }
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Fallbacks
|
|
75
|
+
|
|
76
|
+
Automatically substitute values when validation fails:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
input :per_page, type: :integer, default: 25, fallback: 25
|
|
80
|
+
|
|
81
|
+
input_rule(:per_page) do
|
|
82
|
+
key.failure("max 100") if value && value > 100
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# If per_page: 500 is provided, validation succeeds with fallback value 25
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Validation Rules
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
# Single field
|
|
92
|
+
input_rule(:email) do
|
|
93
|
+
key.failure("invalid") unless value&.include?("@")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Multiple fields
|
|
97
|
+
input_rule(:start_date, :end_date) do
|
|
98
|
+
base.failure("start must be before end") if values[:start_date] > values[:end_date]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Global rule
|
|
102
|
+
input_rule do
|
|
103
|
+
base.failure("error") if some_condition
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Context
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
input_rule(:order) do
|
|
111
|
+
valid = context[:orderable_columns] || []
|
|
112
|
+
key.failure("invalid column") unless valid.include?(value)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Pass context when validating
|
|
116
|
+
inputs.validate(data, context: { orderable_columns: ["id", "name"] })
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Multiple Schemas
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
expects :inputs
|
|
123
|
+
expects :outputs
|
|
124
|
+
|
|
125
|
+
input :data, type: :hash
|
|
126
|
+
output :result, type: :string
|
|
127
|
+
|
|
128
|
+
inputs.validate(input_data)
|
|
129
|
+
outputs.validate(output_data)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Configuration
|
|
133
|
+
|
|
134
|
+
Customize validator method names:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
Expectant.configure do |config|
|
|
138
|
+
config.validator_suffix = "validation" # input_validation instead of input_rule
|
|
139
|
+
config.validator_prefix = "validate" # validate_input
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
bundle install
|
|
147
|
+
bundle exec rspec
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT License
|
data/Rakefile
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "bundler/gem_tasks"
|
|
4
|
-
require "rspec/core/rake_task"
|
|
5
|
-
|
|
6
|
-
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
-
|
|
8
|
-
require "standard/rake"
|
|
9
|
-
|
|
10
|
-
task default: %i[spec standard]
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
|
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
+
|
|
8
|
+
require "standard/rake"
|
|
9
|
+
|
|
10
|
+
task default: %i[spec standard]
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Expectant
|
|
4
|
-
# Instance-bound schema wrapper that provides validation with instance context
|
|
5
|
-
class BoundSchema
|
|
6
|
-
def initialize(instance, schema)
|
|
7
|
-
@instance = instance
|
|
8
|
-
@schema = schema
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def validate(data, context: {})
|
|
12
|
-
# Apply proc defaults before validation
|
|
13
|
-
data = apply_defaults(data)
|
|
14
|
-
|
|
15
|
-
# Create contract instance with context, then validate
|
|
16
|
-
contract_class = @schema.contract
|
|
17
|
-
contract = contract_class.new(instance: @instance, context: context)
|
|
18
|
-
result = contract.call(data)
|
|
19
|
-
|
|
20
|
-
# If validation failed, apply fallbacks for fields with errors and retry
|
|
21
|
-
if !result.success?
|
|
22
|
-
data_with_fallbacks = apply_fallbacks(data, result)
|
|
23
|
-
if data_with_fallbacks != data
|
|
24
|
-
# Re-validate with fallback values
|
|
25
|
-
result = contract.call(data_with_fallbacks)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
result
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def keys
|
|
33
|
-
@schema.keys
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def contract
|
|
37
|
-
@schema.contract
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
private
|
|
41
|
-
|
|
42
|
-
# Apply default values for missing fields (especially proc defaults)
|
|
43
|
-
def apply_defaults(data)
|
|
44
|
-
data = data.dup
|
|
45
|
-
|
|
46
|
-
@schema.fields.each do |field|
|
|
47
|
-
# Skip if value already provided
|
|
48
|
-
next if data.key?(field.name)
|
|
49
|
-
|
|
50
|
-
# Apply proc defaults, static defaults are handled by dry-types
|
|
51
|
-
if field.default.respond_to?(:call)
|
|
52
|
-
data[field.name] = @instance.instance_exec(&field.default)
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
data
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Apply fallback values to fields that have errors
|
|
60
|
-
def apply_fallbacks(data, result)
|
|
61
|
-
data = data.dup
|
|
62
|
-
|
|
63
|
-
@schema.fields.each do |field|
|
|
64
|
-
next unless field.has_fallback?
|
|
65
|
-
|
|
66
|
-
# Apply fallback if field has an error
|
|
67
|
-
if result.errors[field.name]&.any?
|
|
68
|
-
fallback_value = if field.fallback.respond_to?(:call)
|
|
69
|
-
# Proc fallback - evaluate in instance context
|
|
70
|
-
@instance.instance_exec(&field.fallback)
|
|
71
|
-
else
|
|
72
|
-
field.fallback
|
|
73
|
-
end
|
|
74
|
-
data[field.name] = fallback_value
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
data
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Expectant
|
|
4
|
+
# Instance-bound schema wrapper that provides validation with instance context
|
|
5
|
+
class BoundSchema
|
|
6
|
+
def initialize(instance, schema)
|
|
7
|
+
@instance = instance
|
|
8
|
+
@schema = schema
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def validate(data, context: {})
|
|
12
|
+
# Apply proc defaults before validation
|
|
13
|
+
data = apply_defaults(data)
|
|
14
|
+
|
|
15
|
+
# Create contract instance with context, then validate
|
|
16
|
+
contract_class = @schema.contract
|
|
17
|
+
contract = contract_class.new(instance: @instance, context: context)
|
|
18
|
+
result = contract.call(data)
|
|
19
|
+
|
|
20
|
+
# If validation failed, apply fallbacks for fields with errors and retry
|
|
21
|
+
if !result.success?
|
|
22
|
+
data_with_fallbacks = apply_fallbacks(data, result)
|
|
23
|
+
if data_with_fallbacks != data
|
|
24
|
+
# Re-validate with fallback values
|
|
25
|
+
result = contract.call(data_with_fallbacks)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
result
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def keys
|
|
33
|
+
@schema.keys
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def contract
|
|
37
|
+
@schema.contract
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
# Apply default values for missing fields (especially proc defaults)
|
|
43
|
+
def apply_defaults(data)
|
|
44
|
+
data = data.dup
|
|
45
|
+
|
|
46
|
+
@schema.fields.each do |field|
|
|
47
|
+
# Skip if value already provided
|
|
48
|
+
next if data.key?(field.name)
|
|
49
|
+
|
|
50
|
+
# Apply proc defaults, static defaults are handled by dry-types
|
|
51
|
+
if field.default.respond_to?(:call)
|
|
52
|
+
data[field.name] = @instance.instance_exec(&field.default)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
data
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Apply fallback values to fields that have errors
|
|
60
|
+
def apply_fallbacks(data, result)
|
|
61
|
+
data = data.dup
|
|
62
|
+
|
|
63
|
+
@schema.fields.each do |field|
|
|
64
|
+
next unless field.has_fallback?
|
|
65
|
+
|
|
66
|
+
# Apply fallback if field has an error
|
|
67
|
+
if result.errors[field.name]&.any?
|
|
68
|
+
fallback_value = if field.fallback.respond_to?(:call)
|
|
69
|
+
# Proc fallback - evaluate in instance context
|
|
70
|
+
@instance.instance_exec(&field.fallback)
|
|
71
|
+
else
|
|
72
|
+
field.fallback
|
|
73
|
+
end
|
|
74
|
+
data[field.name] = fallback_value
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
data
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Expectant
|
|
4
|
-
class Configuration
|
|
5
|
-
attr_accessor :validator_prefix, :validator_suffix
|
|
6
|
-
|
|
7
|
-
def initialize
|
|
8
|
-
@validator_prefix = nil # default: no prefix
|
|
9
|
-
@validator_suffix = "rule" # default: schema_name_rule
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Expectant
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :validator_prefix, :validator_suffix
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@validator_prefix = nil # default: no prefix
|
|
9
|
+
@validator_suffix = "rule" # default: schema_name_rule
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|