validation_inspector 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c51fb4232f982dc74334f116cc6135a310692158de44c3a8fd5a006f1f5823b4
4
+ data.tar.gz: a0369bf8bd0ac7345137762313cc4429b80a8d69c9151582ac61bb1821481d19
5
+ SHA512:
6
+ metadata.gz: 99c510be04b0f3285a955a886d49abed0a67acced17eb6e1cd3d185b9113f3fe36e7cebfbbce3ce7918bda2160982930c6d2edf1656afc5f4316e9e5c542b037
7
+ data.tar.gz: c63ee5669eafd5e23594b503c72a807202893bb6fcc7397bda47f16fddf1d88a8c2e344bc1c2ce78d81784c5c9df2d259132d862fc2a652ec4b509bc416ef242
@@ -0,0 +1,39 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+
6
+ jobs:
7
+ rubocop:
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ fail-fast: false
11
+ matrix:
12
+ ruby: ["3.2", "3.3"]
13
+ steps:
14
+ - name: Checkout
15
+ uses: actions/checkout@v4
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ bundler-cache: true
21
+ - name: Run RuboCop
22
+ run: bundle exec rubocop
23
+
24
+ rspec:
25
+ runs-on: ubuntu-latest
26
+ strategy:
27
+ fail-fast: false
28
+ matrix:
29
+ ruby: ["3.2", "3.3"]
30
+ steps:
31
+ - name: Checkout
32
+ uses: actions/checkout@v4
33
+ - name: Set up Ruby
34
+ uses: ruby/setup-ruby@v1
35
+ with:
36
+ ruby-version: ${{ matrix.ruby }}
37
+ bundler-cache: true
38
+ - name: Run RSpec
39
+ run: bundle exec rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-02-15
4
+
5
+ - Initial release
6
+
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 tenmihi
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,54 @@
1
+ # ValidationInspector
2
+
3
+ A simple Ruby gem to list ActiveModel validation callbacks with their conditions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'validation_inspector'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'validation_inspector'
23
+
24
+ class User < ApplicationRecord
25
+ attr_accessor :name, :email, :age
26
+
27
+ validates :name, presence: true, if: :active?
28
+ validates :email, format: { with: /@/ }
29
+ validates :age, numericality: { greater_than: 0 }
30
+ validate :custom_validation
31
+ end
32
+
33
+ User.inspect_validations
34
+ =>
35
+ [
36
+ { validator: ActiveModel::Validations::PresenceValidator,
37
+ attributes: [:name],
38
+ if_conds: [":active?"] },
39
+ { validator: ActiveModel::Validations::FormatValidator,
40
+ attributes: [:email],
41
+ options: { with: "/@/" } },
42
+ { validator: ActiveModel::Validations::NumericalityValidator,
43
+ attributes: [:age],
44
+ options: { greater_than: 0 } },
45
+ { validator: :custom_validation,
46
+ attributes: nil }
47
+ ]
48
+ ```
49
+
50
+ **Note**: Options from custom validators or other unsupported validators are not included in the `options` field.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ RuboCop::RakeTask.new
10
+
11
+ task default: :rubocop
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidationInspector
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "validation_inspector/version"
4
+
5
+ # Lists ActiveModel validation callbacks with their conditions.
6
+ module ValidationInspector
7
+ SUPPORTED_OPTION_KEYS = {
8
+ # LengthValidator
9
+ minimum: true,
10
+ maximum: true,
11
+ is: true,
12
+ in: true,
13
+ within: true,
14
+ # NumericalityValidator
15
+ only_integer: true,
16
+ greater_than: true,
17
+ greater_than_or_equal_to: true,
18
+ less_than: true,
19
+ less_than_or_equal_to: true,
20
+ equal_to: true,
21
+ other_than: true,
22
+ odd: true,
23
+ even: true,
24
+ # FormatValidator
25
+ with: true,
26
+ without: true,
27
+ # ComparisonValidator
28
+ # (greater_than, less_than, etc. are shared with NumericalityValidator)
29
+ # Common options
30
+ allow_nil: true,
31
+ allow_blank: true
32
+ }.freeze
33
+
34
+ def self.all(model_class)
35
+ model_class._validate_callbacks.map do |cb|
36
+ validator = validator_name(cb.filter)
37
+
38
+ attributes = cb.filter.respond_to?(:attributes) ? cb.filter.attributes : nil
39
+
40
+ { validator:, attributes: attributes }.tap do |v|
41
+ ifs = Array(cb.instance_variable_get(:@if))
42
+ unlesss = Array(cb.instance_variable_get(:@unless))
43
+
44
+ v[:if_conds] = ifs.map { |c| format_condition(c) } unless ifs.empty?
45
+ v[:unless_conds] = unlesss.map { |c| format_condition(c) } unless unlesss.empty?
46
+
47
+ options = extract_options(cb.filter)
48
+ v[:options] = options unless options.empty?
49
+ end
50
+ end
51
+ end
52
+
53
+ def self.validator_name(filter)
54
+ case filter
55
+ when Symbol then filter
56
+ when Proc
57
+ loc = filter.source_location&.join(":")
58
+ "proc@#{loc || "unknown"}"
59
+ else
60
+ filter.class
61
+ end
62
+ end
63
+
64
+ def self.format_condition(cond)
65
+ cond.is_a?(Symbol) ? ":#{cond}" : (cond.source_location&.join(":") || cond.class.name)
66
+ end
67
+
68
+ def self.extract_options(filter)
69
+ return {} unless filter.respond_to?(:options)
70
+
71
+ options = filter.options
72
+ return {} unless options.is_a?(Hash)
73
+
74
+ options.each_with_object({}) do |(key, value), result|
75
+ next unless SUPPORTED_OPTION_KEYS.key?(key)
76
+
77
+ result[key] = value.is_a?(Regexp) ? value.inspect : value
78
+ end
79
+ end
80
+
81
+ private_class_method :format_condition, :validator_name, :extract_options
82
+ end
83
+
84
+ if defined?(ActiveModel::Validations)
85
+ module ValidationInspector
86
+ module ClassMethodsExtension
87
+ def inspect_validations
88
+ ValidationInspector.all(self)
89
+ end
90
+ end
91
+ end
92
+
93
+ ActiveModel::Validations::ClassMethods.prepend ValidationInspector::ClassMethodsExtension
94
+ else
95
+ warn "Warning: ActiveModel is not available. ValidationInspector will not work without it."
96
+ end
@@ -0,0 +1,4 @@
1
+ module ValidationInspector
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
4
+
5
+ require "active_model"
6
+ require "validation_inspector"
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with :rspec do |c|
10
+ c.syntax = :expect
11
+ end
12
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe ValidationInspector do
6
+ class SampleModel
7
+ include ActiveModel::Validations
8
+
9
+ attr_accessor :name, :email, :active, :skip
10
+
11
+ validates :name, presence: true, if: :active?
12
+ validates :email, format: { with: /@/ }, unless: -> { skip? }
13
+ validate :custom_check
14
+ validate -> { errors.add(:base, "invalid") }
15
+
16
+ def active?
17
+ !!active
18
+ end
19
+
20
+ def skip?
21
+ !!skip
22
+ end
23
+
24
+ def custom_check
25
+ errors.add(:base, "custom")
26
+ end
27
+ end
28
+
29
+ class ModelWithOptions
30
+ include ActiveModel::Validations
31
+
32
+ attr_accessor :name, :age, :status, :role
33
+
34
+ validates :name, length: { minimum: 2, maximum: 255 }
35
+ validates :age, numericality: { greater_than_or_equal_to: 0, less_than: 150, only_integer: true }
36
+ validates :status, inclusion: { in: %w[active inactive] }
37
+ validates :role, exclusion: { in: %w[admin superuser], allow_nil: true }
38
+ end
39
+
40
+ describe ".all" do
41
+ it "lists ActiveModel validations" do
42
+ result = described_class.all(SampleModel)
43
+
44
+ presence = result.find { |v| v[:validator] == ActiveModel::Validations::PresenceValidator }
45
+ expect(presence).not_to be_nil
46
+ expect(presence[:attributes]).to eq([:name])
47
+ expect(presence[:if_conds]).to eq([":active?"])
48
+
49
+ format = result.find { |v| v[:validator] == ActiveModel::Validations::FormatValidator }
50
+ expect(format).not_to be_nil
51
+ expect(format[:attributes]).to eq([:email])
52
+ end
53
+
54
+ it "lists custom validation methods" do
55
+ result = described_class.all(SampleModel)
56
+
57
+ custom = result.find { |v| v[:validator] == :custom_check }
58
+ expect(custom).not_to be_nil
59
+ expect(custom[:attributes]).to be_nil
60
+ end
61
+
62
+ it "lists proc validations" do
63
+ result = described_class.all(SampleModel)
64
+
65
+ proc_entry = result.find { |v| v[:validator].is_a?(String) && v[:validator].start_with?("proc@") }
66
+ expect(proc_entry).not_to be_nil
67
+ end
68
+
69
+ it "extracts options from LengthValidator" do
70
+ result = described_class.all(ModelWithOptions)
71
+
72
+ length = result.find { |v| v[:validator] == ActiveModel::Validations::LengthValidator }
73
+ expect(length).not_to be_nil
74
+ expect(length[:options]).to include(minimum: 2, maximum: 255)
75
+ end
76
+
77
+ it "extracts options from NumericalityValidator" do
78
+ result = described_class.all(ModelWithOptions)
79
+
80
+ numericality = result.find { |v| v[:validator] == ActiveModel::Validations::NumericalityValidator }
81
+ expect(numericality).not_to be_nil
82
+ expect(numericality[:options]).to include(
83
+ greater_than_or_equal_to: 0,
84
+ less_than: 150,
85
+ only_integer: true
86
+ )
87
+ end
88
+
89
+ it "extracts options from InclusionValidator" do
90
+ result = described_class.all(ModelWithOptions)
91
+
92
+ inclusion = result.find { |v| v[:validator] == ActiveModel::Validations::InclusionValidator }
93
+ expect(inclusion).not_to be_nil
94
+ expect(inclusion[:options]).to include(in: %w[active inactive])
95
+ end
96
+
97
+ it "extracts allow_nil option from ExclusionValidator" do
98
+ result = described_class.all(ModelWithOptions)
99
+
100
+ exclusion = result.find { |v| v[:validator] == ActiveModel::Validations::ExclusionValidator }
101
+ expect(exclusion).not_to be_nil
102
+ expect(exclusion[:options]).to include(in: %w[admin superuser], allow_nil: true)
103
+ end
104
+
105
+ it "extracts regex pattern from FormatValidator as inspected string" do
106
+ result = described_class.all(SampleModel)
107
+
108
+ format = result.find { |v| v[:validator] == ActiveModel::Validations::FormatValidator }
109
+ expect(format).not_to be_nil
110
+ expect(format[:options][:with]).to eq("/@/")
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validation_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tenmihi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-02-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ValidationInspector lists ActiveModel validation callbacks and their
14
+ conditions (if/unless), including attributes and custom/proc validators.
15
+ email:
16
+ - tenmihi@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/ci.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/validation_inspector.rb
28
+ - lib/validation_inspector/version.rb
29
+ - sig/validation_inspector.rbs
30
+ - spec/spec_helper.rb
31
+ - spec/validation_inspector_spec.rb
32
+ homepage: https://github.com/tenmihi/validation_inspector
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ rubygems_mfa_required: 'true'
37
+ allowed_push_host: https://rubygems.org
38
+ source_code_uri: https://github.com/tenmihi/validation_inspector
39
+ changelog_uri: https://github.com/tenmihi/validation_inspector/blob/main/CHANGELOG.md
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.2.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.5.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: List ActiveModel validation callbacks with conditions.
59
+ test_files: []