validates_some 1.0.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/LICENSE.md +7 -0
- data/README.md +79 -0
- data/lib/validates_some/version.rb +7 -0
- data/lib/validates_some.rb +63 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7c5d4bb0057fae88d1c702a54c15c265641496642534e2ae9f4691a47fb31718
|
|
4
|
+
data.tar.gz: 2c5cc03b85aea82989ed66cdcb5f7e39831e619af2bfd7ab1eb0f99cd955090a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4e46325d9e73d7448739159efec1ea9e91da73d620ca9a85ae9de85b0a8c7d816c5d74bdace203c86c2ae9fb281416ff730f4e777bc101c8dd1e8ad2c91344cb
|
|
7
|
+
data.tar.gz: 495c274b13b5c1d0e4333ef2f91a1252bb6de4aabeccdad983c60f4352711955c3f7b4f328d5c7490fb8a4e14de10ff7c9d00bac94cf097d8b3a7c420c924bda
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2026 Dimitar Apostolovski
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# validates_some
|
|
2
|
+
|
|
3
|
+
Attribute grouping for `ActiveModel` validations.
|
|
4
|
+
|
|
5
|
+
`validates_some` is an extenstion to the ActiveModel validation API. The `validates_some` method filters declared validations for the referenced attributes. ActiveModel validations are by default all or nothing: the object is `valid?` if every and all attributes pass their validations. `validates_some` expects that just _some_ (at least one) of the listed attributes are valid for the whole group of attributes to be considered validated. If one of the attributes passes its validations without errors, the rest of the attributes' errors are suppressed and the attributes are reset to default values.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
Example when using with `ActiveRecord` in Rails:
|
|
10
|
+
|
|
11
|
+
The user record should have a name, surname, and a nickname; but it's enough to have at least one of them. After you've set up the presence validations, call `validates_some` with the attributes names to specify that they can cover for one another, and that only one of them needs be valid.
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
class User < ActiveRecord::Base
|
|
15
|
+
validates :name, presence: true
|
|
16
|
+
validates :nickname, presence: true
|
|
17
|
+
validates :surname, presence: true
|
|
18
|
+
validates_some :name, :nickname, :surname
|
|
19
|
+
|
|
20
|
+
# ...
|
|
21
|
+
end
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
You can add additional validations after `validates_some`, which are again independent of any other validations. (You can, of course, call `validates_some` again to set up another dependency.)
|
|
25
|
+
|
|
26
|
+
Here the email uniqueness check isn't suppressed when the phone number is valid.
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
validates :phone_number, format: /\d{8}/
|
|
30
|
+
validates :email, format: /\w+@\w+\.\w{3}/
|
|
31
|
+
validates_some :phone_number, :email, resets: ["XXYYYZZZ", ""]
|
|
32
|
+
validates :email, uniqueness: true, allow_blank: true # check uniqueness for non-blank emails
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Invalid attributes are reset to the values passed in `resets:`.
|
|
36
|
+
|
|
37
|
+
`resets:` can take the following values:
|
|
38
|
+
- A single value - All invalid arguments are set to that value (default is `nil`).
|
|
39
|
+
```ruby
|
|
40
|
+
validates_some :name, :surname, resets: "n/a"
|
|
41
|
+
```
|
|
42
|
+
- An `Array` - Invalid attributes are set to the value attached to it by order of listing.
|
|
43
|
+
```ruby
|
|
44
|
+
validates_some :name, :surname, resets: ["n/a", "none"]
|
|
45
|
+
```
|
|
46
|
+
- A `Hash` - Invalid attributes are set to their associated value in the hash.
|
|
47
|
+
```ruby
|
|
48
|
+
validates_some :name, :surname, resets: { name: "n/a", surname: "none" }
|
|
49
|
+
```
|
|
50
|
+
- A `Proc` - Invalid attributes are set to the value returned by the proc when called for that argument.
|
|
51
|
+
```ruby
|
|
52
|
+
validates_some :name, :surname, resets: proc { |attr| "* No #{attr} *" }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
58
|
+
|
|
59
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Development
|
|
72
|
+
|
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
74
|
+
|
|
75
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
76
|
+
|
|
77
|
+
## Contributing
|
|
78
|
+
|
|
79
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/themetar/GEM_NAME.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "validates_some/version"
|
|
4
|
+
require "active_model"
|
|
5
|
+
|
|
6
|
+
# Included in `ActiveModel::Validations::ClassMethods` so that `validates_some` becomes avalilable
|
|
7
|
+
# in any class that descends from ActiveModel or includes `ActiveModel::API`.
|
|
8
|
+
module ValidatesSome
|
|
9
|
+
|
|
10
|
+
# Adds "some/any" logic to previous validations regarding the referenced attributes.
|
|
11
|
+
# At least one of them needs to pass its validations for the whole attribute group to pass.
|
|
12
|
+
#
|
|
13
|
+
# Resets any invalid attributes to the values passed in `resets:`.
|
|
14
|
+
#
|
|
15
|
+
# Example with ActiveRecord (Rails):
|
|
16
|
+
#
|
|
17
|
+
# class Person < ActiveRecord::Base
|
|
18
|
+
# validates :name, presence: true
|
|
19
|
+
# validates :surname, presence: true
|
|
20
|
+
# validates_some :name, :surname
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# # Person.new(surname: "Smith").valid? # => true
|
|
24
|
+
#
|
|
25
|
+
# `resets:` can take the following values:
|
|
26
|
+
# - A single value - All invalid arguments are set to that value (default is `nil`).
|
|
27
|
+
# validates_some :name, :surname, resets: "n/a"
|
|
28
|
+
# - An `Array` - Invalid attributes are set to the value attached to it by order of listing.
|
|
29
|
+
# validates_some :name, :surname, resets: ["n/a", "none"]
|
|
30
|
+
# - A `Hash` - Invalid attributes are set to their associated value in the hash.
|
|
31
|
+
# validates_some :name, :surname, resets: { name: "n/a", surname: "none" }
|
|
32
|
+
# - A `Proc` - Invalid attributes are set to the value returned by the proc when called for that argument.
|
|
33
|
+
# validates_some :name, :surname, resets: proc { |attr| "* No #{attr} *" }
|
|
34
|
+
def validates_some(*attributes, resets: nil)
|
|
35
|
+
reset_values = case resets
|
|
36
|
+
when Hash, Proc then resets
|
|
37
|
+
when Array then attributes.zip(resets).to_h
|
|
38
|
+
else Hash.new(resets)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
validate do |record|
|
|
42
|
+
faulty = attributes.reject { |attribute| record.errors[attribute].empty? }
|
|
43
|
+
|
|
44
|
+
next if faulty.empty? || faulty.length == attributes.length
|
|
45
|
+
|
|
46
|
+
faulty.each do |faulty_attribute|
|
|
47
|
+
record.errors.delete(faulty_attribute)
|
|
48
|
+
record.send("#{faulty_attribute}=", reset_values[faulty_attribute])
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
module ActiveModel # :nodoc: all
|
|
56
|
+
module Validations
|
|
57
|
+
module ClassMethods
|
|
58
|
+
|
|
59
|
+
include ValidatesSome
|
|
60
|
+
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: validates_some
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dimitar Apostolovski
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-16 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: '7.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '7.0'
|
|
27
|
+
description: |
|
|
28
|
+
`validates_some` is an extenstion to the ActiveModel validation API.
|
|
29
|
+
|
|
30
|
+
The `validates_some` method filters declared validations for the referenced attributes. It expects that just _some_ (at least one) of the listed attributes are valid for the whole group of attributes to be considered validated.
|
|
31
|
+
email:
|
|
32
|
+
- d.apostolovski@protonmail.com
|
|
33
|
+
executables: []
|
|
34
|
+
extensions: []
|
|
35
|
+
extra_rdoc_files: []
|
|
36
|
+
files:
|
|
37
|
+
- LICENSE.md
|
|
38
|
+
- README.md
|
|
39
|
+
- lib/validates_some.rb
|
|
40
|
+
- lib/validates_some/version.rb
|
|
41
|
+
homepage: https://github.com/themetar/validates_some
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata:
|
|
45
|
+
homepage_uri: https://github.com/themetar/validates_some
|
|
46
|
+
source_code_uri: https://github.com/themetar/validates_some
|
|
47
|
+
post_install_message:
|
|
48
|
+
rdoc_options: []
|
|
49
|
+
require_paths:
|
|
50
|
+
- lib
|
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: 3.0.0
|
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
requirements: []
|
|
62
|
+
rubygems_version: 3.5.22
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Attribute grouping for `ActiveModel` validations
|
|
66
|
+
test_files: []
|