Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.