Skip to content

Commit 7784337

Browse files
BuonOmocjlarose
andauthored
Remove dry-validation from dependencies (#333)
* dry-validation only as a runtime dependency * Fix config requirement * Various fixes * rework to satisfy usage of validation_contract --------- Co-authored-by: Chris LaRose <[email protected]>
1 parent e33a58e commit 7784337

11 files changed

+51
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ tmp
1212
.keep
1313
.rvmrc
1414
.rakeTasks
15+
.ruby-version
1516
.sass-cache
1617
Gemfile.lock
1718
*.gem

.ruby-version

-1
This file was deleted.

config.gemspec

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
$:.push File.expand_path('../lib', __FILE__)
2-
3-
require 'config/version'
1+
require_relative 'lib/config/version'
2+
require_relative 'lib/config/dry_validation_requirements'
43

54
Gem::Specification.new do |s|
65
s.name = 'config'
@@ -32,8 +31,8 @@ Donate: \e[34mhttps://opencollective.com/rubyconfig/donate\e[0m\n"
3231
s.required_ruby_version = '>= 2.6.0'
3332

3433
s.add_dependency 'deep_merge', '~> 1.2', '>= 1.2.1'
35-
s.add_dependency 'dry-validation', '~> 1.0', '>= 1.0.0'
3634

35+
s.add_development_dependency 'dry-validation', *Config::DryValidationRequirements::VERSIONS
3736
s.add_development_dependency 'rake', '~> 12.0', '>= 12.0.0'
3837

3938
# Testing

lib/config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require 'config/compatibility'
21
require 'config/options'
32
require 'config/configuration'
3+
require 'config/dry_validation_requirements'
44
require 'config/version'
55
require 'config/sources/yaml_source'
66
require 'config/sources/hash_source'

lib/config/compatibility.rb

-3
This file was deleted.
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Config
4+
module DryValidationRequirements
5+
VERSIONS = ['~> 1.0', '>= 1.0.0'].freeze
6+
7+
def self.load_dry_validation!
8+
return if defined?(@load_dry_validation)
9+
10+
begin
11+
require 'dry/validation/version'
12+
version = Gem::Version.new(Dry::Validation::VERSION)
13+
unless VERSIONS.all? { |req| Gem::Requirement.new(req).satisfied_by?(version) }
14+
raise LoadError
15+
end
16+
rescue LoadError
17+
raise ::Config::Error, 'Could not find a dry-validation version' \
18+
' matching requirements' \
19+
" (#{VERSIONS.map(&:inspect) * ','})"
20+
end
21+
22+
require 'dry/validation'
23+
@load_dry_validation = true
24+
nil
25+
end
26+
end
27+
end

lib/config/error.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Config
2+
class Error < StandardError
3+
end
4+
end

lib/config/validation/error.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
require_relative "../error"
2+
13
module Config
24
module Validation
3-
class Error < StandardError
5+
class Error < ::Config::Error
46

57
def self.format(v_res)
68
v_res.errors.group_by(&:path).map do |path, messages|

lib/config/validation/schema.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require_relative '../dry_validation_requirements'
2+
require_relative '../error'
3+
14
module Config
25
module Validation
36
module Schema
@@ -9,7 +12,7 @@ def schema=(value)
912
def schema(&block)
1013
if block_given?
1114
# Delay require until optional schema validation is requested
12-
require 'dry-validation'
15+
Config::DryValidationRequirements.load_dry_validation!
1316
@schema = Dry::Schema.define(&block)
1417
else
1518
@schema

lib/config/validation/validate.rb

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module Config
44
module Validation
55
module Validate
66
def validate!
7+
return unless Config.validation_contract || Config.schema
8+
9+
Config::DryValidationRequirements.load_dry_validation!
10+
711
validate_using!(Config.validation_contract)
812
validate_using!(Config.schema)
913
end

spec/spec_helper.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
##
1515
# Load Rspec supporting files
1616
#
17-
Dir['./spec/support/**/*.rb'].each { |f| require f }
17+
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
1818

1919
##
2020
# Detect Rails/Sinatra dummy application based on gemfile name substituted by Appraisal
2121
#
2222
if ENV['APPRAISAL_INITIALIZED'] || ENV['GITHUB_ACTIONS']
23-
app_name = Pathname.new(ENV['BUNDLE_GEMFILE']).basename.sub('.gemfile', '')
23+
app_name = File.basename(ENV['BUNDLE_GEMFILE'], '.gemfile')
2424
else
2525
/.*?(?<app_name>rails.*?)\.gemfile/ =~ Dir["gemfiles/rails*.gemfile"].sort.last
2626
end
@@ -33,7 +33,7 @@
3333
case app_framework
3434
when 'rails'
3535
# Load Rails
36-
require File.expand_path("../app/#{app_name}/config/environment", __FILE__)
36+
require_relative "app/#{app_name}/config/environment"
3737

3838
APP_RAKEFILE = File.expand_path("../app/#{app_name}/Rakefile", __FILE__)
3939

@@ -47,7 +47,7 @@
4747

4848
when 'sinatra'
4949
# Load Sinatra
50-
require File.expand_path("../app/#{app_name}/app", __FILE__)
50+
require_relative "app/#{app_name}/app"
5151

5252
# Load Rspec
5353
require 'rspec'

0 commit comments

Comments
 (0)