Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added suppress_required_validations option #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## master

## 2.6.5 (2024-05-30)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not update version numbers/dates; it's only done on release


- Added `suppress_required_validations` configuration for skipping validation in CI. ([@OlegChuev][])
- Support for the Rails built-in environment variable `SECRET_KEY_BASE_DUMMY`. If this variable is set, all validations for required attributes are skipped. This is particularly useful for executing `bundle exec rails assets:precompile` in CI/CD pipelines.

- Introduced a global setting `Anyway::Settings.suppress_required_validations = true` to disable validations. This can be toggled via the environment variable `ANYWAY_SUPPRESS_VALIDATIONS`.

- For Rails applications, `suppress_required_validations` is automatically set to `true` if `SECRET_KEY_BASE_DUMMY` is defined.

## 2.6.4 (2024-04-30)

- Fix RBS manifest file name (`.yml` -> `.yaml`). ([@carlqt][])
Expand Down Expand Up @@ -579,3 +588,4 @@ No we're dependency-free!
[@tagirahmad]: https://github.com/tagirahmad
[@bessey]: https://github.com/bessey
[@carlqt]: https://github.com/carlqt
[@OlegChuev]: https://github.com/OlegChuev
63 changes: 40 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,43 @@ For version 1.x see the [1-4-stable branch](https://github.com/palkan/anyway_con

## Table of contents

- [Main concepts](#main-concepts)
- [Installation](#installation)
- [Usage](#usage)
- [Configuration classes](#configuration-classes)
- [Dynamic configuration](#dynamic-configuration)
- [Validation & Callbacks](#validation-and-callbacks)
- [Using with Rails applications](#using-with-rails)
- [Data population](#data-population)
- [Organizing configs](#organizing-configs)
- [Generators](#generators)
- [Using with Ruby applications](#using-with-ruby)
- [Environment variables](#environment-variables)
- [Type coercion](#type-coercion)
- [Local configuration](#local-files)
- [Data loaders](#data-loaders)
- [Doppler integration](#doppler-integration)
- [EJSON support](#ejson-support)
- [Custom loaders](#custom-loaders)
- [Source tracing](#tracing)
- [Pattern matching](#pattern-matching)
- [Test helpers](#test-helpers)
- [OptionParser integration](#optionparser-integration)
- [RBS support](#rbs-support)
- [Anyway Config](#anyway-config)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is irrelevant and not needed; please, revert.

- [Links](#links)
- [Table of contents](#table-of-contents)
- [Main concepts](#main-concepts)
- [Installation](#installation)
- [Supported Ruby versions](#supported-ruby-versions)
- [Usage](#usage)
- [Configuration classes](#configuration-classes)
- [Config name](#config-name)
- [Customize env variable names prefix](#customize-env-variable-names-prefix)
- [Explicit values](#explicit-values)
- [Reload configuration](#reload-configuration)
- [Dynamic configuration](#dynamic-configuration)
- [Validation and callbacks](#validation-and-callbacks)
- [Using with Rails](#using-with-rails)
- [Data population](#data-population)
- [Multi-env configuration](#multi-env-configuration)
- [Organizing configs](#organizing-configs)
- [Generators](#generators)
- [Loading Anyway Config before Rails](#loading-anyway-config-before-rails)
- [Using with Ruby](#using-with-ruby)
- [Environment variables](#environment-variables)
- [Type coercion](#type-coercion)
- [Local files](#local-files)
- [Data loaders](#data-loaders)
- [Doppler integration](#doppler-integration)
- [EJSON support](#ejson-support)
- [Custom loaders](#custom-loaders)
- [Tracing](#tracing)
- [Pretty print](#pretty-print)
- [Pattern matching](#pattern-matching)
- [Test helpers](#test-helpers)
- [OptionParser integration](#optionparser-integration)
- [RBS support](#rbs-support)
- [Handling `on_load`](#handling-on_load)
- [Contributing](#contributing)
- [License](#license)

## Main concepts

Expand Down Expand Up @@ -304,6 +318,9 @@ MyConfig.new(api_secret: "") #=> raises Anyway::Config::ValidationError
`Required` method supports additional `env` parameter which indicates necessity to run validations under specified
environments. `Env` parameter could be present in symbol, string, array or hash formats:

**NOTE:** You can suppress the validation of the required parameters (it can be useful for CI) via the `ANYWAY_SUPPRESS_VALIDATIONS` environment variable or by setting it explicitly in the code: `Anyway::Settings.suppress_required_validations = true`.
If you are using Anyway Config with Rails and have already specified `SECRET_KEY_BASE_DUMMY` for asset precompilation, validation will be skipped by default.

```ruby
class EnvConfig < Anyway::Config
required :password, env: "production"
Expand Down
2 changes: 2 additions & 0 deletions lib/anyway/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ def as_env
attr_reader :values, :__trace__

def validate_required_attributes!
return if Settings.suppress_required_validations

self.class.required_attributes.select do |name|
val = values.dig(*name.to_s.split(".").map(&:to_sym))
val.nil? || (val.is_a?(String) && val.empty?)
Expand Down
2 changes: 2 additions & 0 deletions lib/anyway/rails/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@ def app_root
self.use_local_files ||= ::Rails.env.development?
# Don't try read defaults when no key defined
self.default_environmental_key = nil

self.suppress_required_validations = ENV["SECRET_KEY_BASE_DUMMY"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway-specific env var must have precedence:

Suggested change
self.suppress_required_validations = ENV["SECRET_KEY_BASE_DUMMY"]
self.suppress_required_validations = !!ENV["SECRET_KEY_BASE_DUMMY"] unless ENV.key?("ANYWAY_SUPPRESS_VALIDATIONS")

end
end
6 changes: 6 additions & 0 deletions lib/anyway/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class << self
:default_environmental_key,
:known_environments

# Suppress required validations for CI/CD pipelines
attr_accessor :suppress_required_validations

# A proc returning a path to YML config file given the config name
attr_reader :default_config_path

Expand Down Expand Up @@ -107,5 +110,8 @@ def matching_env?(env)

# Tracing is enabled by default
self.tracing_enabled = true

# By default, use ANYWAY_SUPPRESS_VALIDATIONS
self.suppress_required_validations = ENV["ANYWAY_SUPPRESS_VALIDATIONS"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more specific here:

Suggested change
self.suppress_required_validations = ENV["ANYWAY_SUPPRESS_VALIDATIONS"]
self.suppress_required_validations = %w[1 t true y yes].include?(ENV["ANYWAY_SUPPRESS_VALIDATIONS"])

end
end
1 change: 1 addition & 0 deletions sig/anyway_config.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Anyway
def self.future: -> Future
def self.current_environment: -> String?
def self.default_environmental_key: -> String?
def self.suppress_required_validations: -> bool?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's either true or false

Suggested change
def self.suppress_required_validations: -> bool?
def self.suppress_required_validations: -> bool

def self.known_environments: -> Array[String]?

class Future
Expand Down
20 changes: 20 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,26 @@ def self.name
.to raise_error(ArgumentError, /unknown config param: test/i)
end

context "suppress validation" do
let(:config_values) { {} }

context "with setting" do
before { allow(Anyway::Settings).to receive(:suppress_required_validations).and_return(true) }

it "not to raise ValidationError" do
expect { subject }.to_not raise_error
end
end

context "with env" do
it "not to raise ValidationError" do
with_env("ANYWAY_SUPPRESS_VALIDATIONS" => "1") do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add another test case with the falsey value (e.g., "ANYWAY_SUPPRESS_VALIDATIONS=0").

expect { subject }.to_not raise_error
end
end
end
end

specify "inheritance" do
subconfig = Class.new(config) { required :debug }

Expand Down
14 changes: 14 additions & 0 deletions spec/rails/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,19 @@
expect { MyAppConfig.new }
.to raise_error(Anyway::Config::ValidationError, /missing or empty: name, mode/)
end

context "when suppress validation" do
context "manually with setting" do
around do |ex|
Anyway::Settings.suppress_required_validations = true
ex.run
Anyway::Settings.suppress_required_validations = false
end

it "skips validation" do
expect { MyAppConfig.new }.to_not raise_error
end
end
end
end
end
Loading