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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cd084f7dbdd4fa98ec53ec0cfd58a5c2f9ade3a0eeebec5ea992cb30f581482
4
- data.tar.gz: 49f931b7b2ee7633406a87f165691245428c8542de9f90f7278f72cd711e04c6
3
+ metadata.gz: 06e38c364b8d2d666a81174ddafa2b426153ce1627c155bd996c8b1a912c6212
4
+ data.tar.gz: 7d5d662b9971375813d280f5c129aeb0ec80d960b5520368063714c7057ce794
5
5
  SHA512:
6
- metadata.gz: 763ab6b831344ba33a679f32ba5df7469bc2d9d985a8e75483a1077ba1f65f5a1abe5e1af571a76b2af1aca7fcd9b4601e1fee066ffbd5627f0c3c2f69904577
7
- data.tar.gz: f91ca026879484b537c166883e55e4011856812baa38f59e3f2830f6fc7d761a06d13b538efc3271a662313b199036ec5e85b3196de0a595f990b555b4570055
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
- [![Gem Version](https://img.shields.io/gem/v/expectant?style=flat&label=Expectant)](https://rubygems.org/gems/expectant)
4
-
5
- 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/).
6
-
7
- ## Installation
8
-
9
- ```ruby
10
- gem 'expectant'
11
- ```
12
-
13
- ## Quick Start
14
-
15
- ```ruby
16
- class InvoiceService
17
- include Expectant::DSL
18
-
19
- expects :inputs
20
-
21
- input :customer_id, type: :integer
22
- input :amount, type: :float
23
- input :status, type: :string, default: "draft"
24
- input :description, type: :string, optional: true
25
-
26
- input_rule(:amount) do
27
- key.failure("must be positive") if value && value <= 0
28
- end
29
-
30
- def process(data)
31
- result = inputs.validate(data)
32
-
33
- if result.success?
34
- # Use validated data: result.to_h
35
- else
36
- # Handle errors: result.errors.to_h
37
- end
38
- end
39
- end
40
- ```
41
-
42
- ## Core Features
43
-
44
- ### Types
45
-
46
- ```ruby
47
- expects :inputs
48
-
49
- input :name, type: :string
50
- input :age, type: :integer
51
- input :price, type: :float
52
- input :active, type: :boolean
53
- input :date, type: :date
54
- input :time, type: :datetime
55
- input :data, type: :hash
56
- input :tags, type: :array
57
- input :user, type: User # Custom class
58
- ```
59
-
60
- ### Defaults and Optional Fields
61
-
62
- ```ruby
63
- # Optional (can be omitted)
64
- input :description, type: :string, optional: true
65
-
66
- # Static default
67
- input :status, type: :string, default: "draft"
68
-
69
- # Dynamic default (proc)
70
- input :created_at, type: :datetime, default: -> { Time.now }
71
- ```
72
-
73
- ### Fallbacks
74
-
75
- Automatically substitute values when validation fails:
76
-
77
- ```ruby
78
- input :per_page, type: :integer, default: 25, fallback: 25
79
-
80
- input_rule(:per_page) do
81
- key.failure("max 100") if value && value > 100
82
- end
83
-
84
- # If per_page: 500 is provided, validation succeeds with fallback value 25
85
- ```
86
-
87
- ### Validation Rules
88
-
89
- ```ruby
90
- # Single field
91
- input_rule(:email) do
92
- key.failure("invalid") unless value&.include?("@")
93
- end
94
-
95
- # Multiple fields
96
- input_rule(:start_date, :end_date) do
97
- base.failure("start must be before end") if values[:start_date] > values[:end_date]
98
- end
99
-
100
- # Global rule
101
- input_rule do
102
- base.failure("error") if some_condition
103
- end
104
- ```
105
-
106
- ### Context
107
-
108
- ```ruby
109
- input_rule(:order) do
110
- valid = context[:orderable_columns] || []
111
- key.failure("invalid column") unless valid.include?(value)
112
- end
113
-
114
- # Pass context when validating
115
- inputs.validate(data, context: { orderable_columns: ["id", "name"] })
116
- ```
117
-
118
- ### Multiple Schemas
119
-
120
- ```ruby
121
- expects :inputs
122
- expects :outputs
123
-
124
- input :data, type: :hash
125
- output :result, type: :string
126
-
127
- inputs.validate(input_data)
128
- outputs.validate(output_data)
129
- ```
130
-
131
- ## Configuration
132
-
133
- Customize validator method names:
134
-
135
- ```ruby
136
- Expectant.configure do |config|
137
- config.validator_suffix = "validation" # input_validation instead of input_rule
138
- config.validator_prefix = "validate" # validate_input
139
- end
140
- ```
141
-
142
- ## Development
143
-
144
- ```bash
145
- bundle install
146
- bundle exec rspec
147
- ```
148
-
149
- ## License
150
-
151
- MIT License
1
+ # Expectant
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/expectant?style=flat&label=Expectant)](https://rubygems.org/gems/expectant)
4
+ [![Ruby Test](https://github.com/DigitalHospital/expectant/actions/workflows/main.yml/badge.svg?branch=master)](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