
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
validation_examples_matcher
Advanced tools
ValidationExamplesMatcher supports writing rails model's validation specs.
e.g.
RSpec.describe MyModel do
...
it { is_expected.to be_invalid_on(:name).with(nil) }
it { is_expected.to be_invalid_on(:name).with('') }
it { is_expected.to be_valid_on(:name).with('my name') }
end
ValidationExamplesMatcher sets a particular value ― nil, empty string and 'my name' ― actually.
Then the matcher calls valid?
or invalid?
method and checks results of validation.
Add this line to your application's Gemfile:
gem 'validation_examples_matcher'
And then execute:
$ bundle
Let me show how to use ValidationExamplesMatcher in case of following model.
class MyModel < ActiveRecord::Base
validates :name, presence: true
validates :name, length: { maximum: 4 }, on: :create
end
First, you have to define subject
as target model object.
It's good to define it as valid object.
Creating object with factory_girl is very nice.
RSpec.define MyModel do
subject { FactoryGirl.build(:my_model) }
# subject { MyModel.new } => also ok but not recommended
end
Second, write invalid case examples and valid case examples using ValidationExamplesMatcher.
In case of MyModel
, :name
attribute is invalid when its' value is nil or empty string.
'my name'
is typical valid example.
RSpec.define MyModel do
subject { FactoryGirl.build(:my_model) }
it { is_expected.to be_invalid_on(:name).with(nil) }
it { is_expected.to be_invalid_on(:name).with('') }
it { is_expected.to be_valid_on(:name).with('my name') }
end
Arguments of be_invalid_on
or be_valid_on
indicate attribute name.
And arguments of with
chain is a value which will be set to the attribute.
ActiveModel::Validations
can define validations on specific contexts.
MyModel
has length
validation only on :create
context.
You can also define validation spec for such case using on_context
chain.
RSpec.define MyModel do
subject { FactoryGirl.build(:my_model) }
it { is_expected.to be_invalid_on(:name).with(nil) }
it { is_expected.to be_invalid_on(:name).with('') }
it { is_expected.to be_valid_on(:name).with('my name') }
it { is_expected.to be_valid_on(:name).with('4cha').on_context(:create) }
it { is_expected.to be_invalid_on(:name).with('5char').on_context(:create) }
end
see CHANGELOG.md
Bug reports and pull requests are welcome on GitHub at https://github.com/speee/validation_examples_matcher.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that validation_examples_matcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.