composite_validator 0.1.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 +7 -0
- data/CHANGELOG.md +18 -0
- data/LICENSE.txt +21 -0
- data/README.md +165 -0
- data/lib/active_model/validations/composite_validator.rb +91 -0
- data/lib/composite_validator/version.rb +5 -0
- data/lib/composite_validator.rb +4 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 728406d0c72c5784491933ad9691c991a6f00f2c0addf33fb2ca6b6b4b1549b7
|
4
|
+
data.tar.gz: c3d833672c2e2e9a8c9c65d4cd0cc463ff0372a7f4a15f3fb6846a3e11714486
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2bada78d9a5b84295d95dbadbccf0af42a3ef46e5c8689293700685a4db4265a42a1a1d05228839cb1967b997e0c39eef054f977ea38f11faa5447c1289ecaa8
|
7
|
+
data.tar.gz: 9da9513d7b8d44cd1780fb0d878ae37aa51ac058f045e88d18943263192b003f06634529fb65ebf75aead2413d715a8c9d003ddd03eb9183231598d906101325
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.1.0] - 2024-01-01
|
11
|
+
|
12
|
+
### Added
|
13
|
+
- Initial release of CompositeValidator
|
14
|
+
- Support for validating single associated models
|
15
|
+
- Support for validating collections of models
|
16
|
+
- Support for validation contexts
|
17
|
+
- Automatic nil handling
|
18
|
+
- Error message aggregation from associated models
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 sho-work
|
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
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
# CompositeValidator
|
2
|
+
|
3
|
+
CompositeValidator is an ActiveModel validator that allows you to validate associated models and compose their validation errors to the parent model. It supports both single associations and collections, making it perfect for form objects that aggregate multiple models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'composite_validator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install composite_validator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User
|
27
|
+
include ActiveModel::Model
|
28
|
+
include ActiveModel::Validations
|
29
|
+
|
30
|
+
attr_accessor :name, :email
|
31
|
+
|
32
|
+
validates :name, presence: true
|
33
|
+
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
34
|
+
end
|
35
|
+
|
36
|
+
class UserCredentials
|
37
|
+
include ActiveModel::Model
|
38
|
+
include ActiveModel::Validations
|
39
|
+
|
40
|
+
attr_accessor :password, :password_confirmation
|
41
|
+
|
42
|
+
validates :password, presence: true, length: { minimum: 8 }
|
43
|
+
validates :password_confirmation, presence: true
|
44
|
+
validate :passwords_match
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def passwords_match
|
49
|
+
return if password == password_confirmation
|
50
|
+
errors.add(:password_confirmation, "doesn't match Password")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class UserRegistration
|
55
|
+
include ActiveModel::Model
|
56
|
+
include ActiveModel::Validations
|
57
|
+
|
58
|
+
attr_accessor :user, :user_credentials
|
59
|
+
|
60
|
+
validates :user, composite: true
|
61
|
+
validates :user_credentials, composite: true
|
62
|
+
end
|
63
|
+
|
64
|
+
# Usage
|
65
|
+
user = User.new(name: '', email: 'invalid-email')
|
66
|
+
credentials = UserCredentials.new(password: 'short', password_confirmation: 'different')
|
67
|
+
registration = UserRegistration.new(user: user, user_credentials: credentials)
|
68
|
+
|
69
|
+
registration.valid? # => false
|
70
|
+
registration.errors[:user]
|
71
|
+
# => ["Name can't be blank and Email is invalid"]
|
72
|
+
registration.errors[:user_credentials]
|
73
|
+
# => ["Password is too short (minimum is 8 characters) and Password confirmation doesn't match Password"]
|
74
|
+
```
|
75
|
+
|
76
|
+
### Working with Collections
|
77
|
+
|
78
|
+
CompositeValidator automatically handles collections of objects:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class OrderItem
|
82
|
+
include ActiveModel::Model
|
83
|
+
include ActiveModel::Validations
|
84
|
+
|
85
|
+
attr_accessor :product_name, :quantity
|
86
|
+
|
87
|
+
validates :product_name, presence: true
|
88
|
+
validates :quantity, numericality: { greater_than: 0 }
|
89
|
+
end
|
90
|
+
|
91
|
+
class Order
|
92
|
+
include ActiveModel::Model
|
93
|
+
include ActiveModel::Validations
|
94
|
+
|
95
|
+
attr_accessor :items
|
96
|
+
|
97
|
+
validates :items, composite: true
|
98
|
+
end
|
99
|
+
|
100
|
+
# Usage
|
101
|
+
items = [
|
102
|
+
OrderItem.new(product_name: 'Widget', quantity: 5),
|
103
|
+
OrderItem.new(product_name: '', quantity: -1)
|
104
|
+
]
|
105
|
+
order = Order.new(items: items)
|
106
|
+
|
107
|
+
order.valid? # => false
|
108
|
+
order.errors[:items]
|
109
|
+
# => ["Product name can't be blank and Quantity must be greater than 0"]
|
110
|
+
```
|
111
|
+
|
112
|
+
### Using Validation Contexts
|
113
|
+
|
114
|
+
You can specify a validation context to use when validating the associated models:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class Article
|
118
|
+
include ActiveModel::Model
|
119
|
+
include ActiveModel::Validations
|
120
|
+
|
121
|
+
attr_accessor :title, :content
|
122
|
+
|
123
|
+
validates :title, presence: true
|
124
|
+
validates :content, presence: true, length: { minimum: 100 }, on: :publish
|
125
|
+
end
|
126
|
+
|
127
|
+
class ArticleForm
|
128
|
+
include ActiveModel::Model
|
129
|
+
include ActiveModel::Validations
|
130
|
+
|
131
|
+
attr_accessor :article
|
132
|
+
|
133
|
+
validates :article, composite: { context: :publish }
|
134
|
+
end
|
135
|
+
|
136
|
+
# Usage
|
137
|
+
article = Article.new(title: 'My Article', content: 'Short content')
|
138
|
+
form = ArticleForm.new(article: article)
|
139
|
+
|
140
|
+
form.valid? # => false
|
141
|
+
form.errors[:article]
|
142
|
+
# => ["Content is too short (minimum is 100 characters)"]
|
143
|
+
```
|
144
|
+
|
145
|
+
### Features
|
146
|
+
|
147
|
+
- **Automatic nil handling**: If the associated object is nil, validation is skipped
|
148
|
+
- **Collection support**: Automatically detects and validates collections (arrays, etc.)
|
149
|
+
- **Struct support**: Properly handles Struct objects (treats them as single objects, not collections)
|
150
|
+
- **Context support**: Pass validation contexts to associated models
|
151
|
+
- **Clear error messages**: Aggregates all error messages from associated models
|
152
|
+
|
153
|
+
## Development
|
154
|
+
|
155
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
156
|
+
|
157
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
158
|
+
|
159
|
+
## Contributing
|
160
|
+
|
161
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sho-work/composite_validator.
|
162
|
+
|
163
|
+
## License
|
164
|
+
|
165
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
# Composite Validator. Inherits from ActiveModel::EachValidator.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
# class UserRegistration
|
9
|
+
# include ActiveModel::Validations
|
10
|
+
#
|
11
|
+
# attr_accessor :user, :user_credentials
|
12
|
+
#
|
13
|
+
# validates :user, composite: true
|
14
|
+
# validates :user_credentials, composite: true
|
15
|
+
# end
|
16
|
+
class CompositeValidator < ActiveModel::EachValidator
|
17
|
+
def validate_each(record, attribute, value)
|
18
|
+
Validator
|
19
|
+
.call(parent: self, record: record, attribute: attribute, value: value)
|
20
|
+
end
|
21
|
+
|
22
|
+
class Validator
|
23
|
+
private_class_method :new
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def call(parent:, record:, attribute:, value:)
|
27
|
+
new(
|
28
|
+
parent: parent, record: record, attribute: attribute, value: value
|
29
|
+
).call
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(parent:, record:, attribute:, value:)
|
34
|
+
@parent = parent
|
35
|
+
@record = record
|
36
|
+
@attribute = attribute
|
37
|
+
@value = value
|
38
|
+
end
|
39
|
+
|
40
|
+
def call
|
41
|
+
return if value.nil? || valid?
|
42
|
+
|
43
|
+
add_error!
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
attr_reader :parent, :record, :attribute, :value
|
49
|
+
|
50
|
+
delegate :options, to: :parent
|
51
|
+
|
52
|
+
def associations
|
53
|
+
@associations ||= \
|
54
|
+
if value.class.include?(Enumerable) && !value.is_a?(Struct)
|
55
|
+
value
|
56
|
+
else
|
57
|
+
[value]
|
58
|
+
end.select { _1.respond_to?(:valid?) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def valid?
|
62
|
+
invalid_associations.empty?
|
63
|
+
end
|
64
|
+
|
65
|
+
def invalid_associations
|
66
|
+
@invalid_associations ||= \
|
67
|
+
associations.select { _1.invalid?(context) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def invalid_message
|
71
|
+
invalid_associations
|
72
|
+
.map { _1.errors.full_messages.to_sentence }
|
73
|
+
.to_sentence
|
74
|
+
end
|
75
|
+
|
76
|
+
def context = options.try(:[], :context)
|
77
|
+
|
78
|
+
def add_error!
|
79
|
+
record
|
80
|
+
.errors
|
81
|
+
.add(
|
82
|
+
attribute,
|
83
|
+
:invalid_association,
|
84
|
+
association: invalid_message
|
85
|
+
)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
private_constant :Validator
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: composite_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sho-work
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-06-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.21'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.21'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
97
|
+
description: CompositeValidator allows you to validate associated models and compose
|
98
|
+
their validation errors to the parent model. It supports both single associations
|
99
|
+
and collections.
|
100
|
+
email:
|
101
|
+
- sho-work@example.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- CHANGELOG.md
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- lib/active_model/validations/composite_validator.rb
|
110
|
+
- lib/composite_validator.rb
|
111
|
+
- lib/composite_validator/version.rb
|
112
|
+
homepage: https://github.com/sho-work/composite_validator
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata:
|
116
|
+
allowed_push_host: https://rubygems.org
|
117
|
+
homepage_uri: https://github.com/sho-work/composite_validator
|
118
|
+
source_code_uri: https://github.com/sho-work/composite_validator
|
119
|
+
changelog_uri: https://github.com/sho-work/composite_validator/blob/main/CHANGELOG.md
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 2.7.0
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.5.9
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: ActiveModel validator for composing validations from associated models
|
139
|
+
test_files: []
|