Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
This gem adds a soft validator for use with ActiveModel/ActiveRecord. It is intended to solve issues that can arise when adding new validations to an existing model. Once your model is running in production and taking real world inputs, it can be risky to add a new validation since it might break some of those inputs. This gem allows you to soft launch a new validation by running it in a non-enforcing mode. This will allow you to see what would have failed without actually failing the validation. You can then fix the issues and turn on the validation.
The easiest way to use the gem is simply to wrap your existing validations with a :soft
validation.
class Thing < ApplicationRecord
validates :units, presence: true, soft: {inclusion: {in: ["feet", "meters"]}}
end
Each of the validations in the :soft
validation will still be run when the record is validated. However, if there are any errors, they will not be added to the record errors
object and the record will still be considered valid.
Soft validation errors are published via ActiveSupport notifications. You can handle errors generted from soft validations by subscribing to the validation_error.soft_validator
event. For instance, you could log the errors to the Rails logger by adding this code to an initializer:
ActiveSupport::Notifications.subscribe("validation_error.soft_validator") do |event|
error = event.payload[:error]
message = "Soft validation error on {error.base.class.name}(#{error.base.id}): #{error.full_message}"
Rails.logger.warn(message)
end
You can also the SoftValidator.subscribe
helper method to set up subscriptions. This code will do the same thing as the code above:
SoftValidator.subscribe do |error|
message = "Soft validation error on {error.base.class.name}(#{error.base.id}): #{error.full_message}"
Rails.logger.warn(message)
end
If you just want to log the errors, you can use the built in log subscriber instead (it does the same thing as the above subscription). You do not need to do this in a Rails application; it will be done for you automatically.
SoftValidator::LogSubscriber.attach
You can disable the log subscriber by calling SoftValidator::LogSubscriber.detach
.
You can turn a soft validation into a hard validation by setting the :enforce
option to true
. This will cause the validation to generate errors as normal. You can use this option to enable validation with a feature flag.
class Thing < ApplicationRecord
validates :units,
presence: true,
soft: {
inclusion: {in: ["feet", "meters"]},
enforce: ENV.fetch("ENFORCE_UNITS", !Rails.env.production?.to_s) == "true"
}
end
The global default for enforcing soft validations can be changed by setting SoftValidator.enforce
. If it is set to true
, soft validations will be enforced by default. In Rails applications this is set automatically in the development and test environments since you would typically want to see the errors in those environments so that you can fix them.
If you want to add any of the standard conditional options for the validator (i.e. :if
, :unless
, :on
, :prepend
), you need to add it to the soft validator and not the wrapped validator.
class Thing < ApplicationRecord
validates :units,
soft: {inclusion: {in: ["feet", "meters"]}, if: :units_changed?}
# This won't work; the `if` option cannot be on the wrapped validator:
# validates :units,
# soft: {inclusion: {in: ["feet", "meters"], if: :units_changed?}}
end
Add this line to your application's Gemfile:
gem "soft_validator"
Then execute:
$ bundle
Or install it yourself as:
$ gem install gem "soft_validator"
Open a pull request on GitHub.
Please use the standardrb syntax and lint your code with standardrb --fix
before submitting.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that soft_validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.