Skip to content

Commit b28668d

Browse files
authored
Drop dry-v compatibility < 1.x and fix memory leak (#514)
* Drop dry-v compatibility < 1.x * remove minitest deprecation warnings * Test added to reproduce issue #511 * fix memoization issue
1 parent 927116a commit b28668d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+615
-3397
lines changed

.travis.yml

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ rvm:
77
- 2.6
88
- 2.5
99
- 2.4
10-
gemfile:
11-
- gemfiles/1.5.0.gemfile
12-
- gemfiles/0.13.0.gemfile
1310
matrix:
1411
fast_finish: true
1512
allow_failures:

Appraisals

-8
This file was deleted.

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 3.0.0
22

3+
* [BREAKING] Dropping compatibility of dry-validation < 1.x
34
[* Removed `Reform::Contract` ?]
45
[* Move Form#deserializer to Form::deserializer]
56

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ source "https://rubygems.org"
22

33
gemspec
44

5-
gem "appraisal", "~> 2.2"
5+
gem 'dry-monads', "1.3.5"
6+
gem 'dry-validation', '~> 1.5.0'

Rakefile

+1-11
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,9 @@ require "dry/types/version"
55

66
task default: %i[test]
77

8-
TEST_WITH_OLD_AND_NEW_API = %w[
9-
validation/dry_validation call composition contract errors inherit module reform
10-
save skip_if populate validate form
11-
].freeze
12-
13-
def dry_v_test_files
14-
api = Gem::Version.new(Dry::Types::VERSION).to_s.split('.').first.to_i >= 1 ? "new" : "old"
15-
TEST_WITH_OLD_AND_NEW_API.map { |file| "test/#{file}_#{api}_api.rb" }
16-
end
17-
188
Rake::TestTask.new(:test) do |test|
199
test.libs << "test"
20-
test.test_files = FileList["test/*_test.rb"] + FileList["test/validation/*_test.rb"] + dry_v_test_files
10+
test.test_files = FileList["test/*_test.rb"] + FileList["test/validation/*_test.rb"]
2111
test.verbose = true
2212
end
2313

gemfiles/0.13.0.gemfile

-8
This file was deleted.

gemfiles/1.5.0.gemfile

-9
This file was deleted.

lib/reform/contract/validate.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def validate
1111
validate!(nil).success?
1212
end
1313

14-
# The #errors method will be removed in Reform 2.4/3.0 core.
14+
# The #errors method will be removed in Reform 3.0 core.
1515
def errors(*args)
1616
Result::Errors.new(@result, self)
1717
end

lib/reform/form/dry.rb

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
require "dry-validation"
2-
require "dry/validation/version"
32
require "reform/validation"
43
require "reform/form/dry/input_hash"
54

5+
::Dry::Validation.load_extensions(:hints)
6+
67
module Reform::Form::Dry
8+
class Contract < Dry::Validation::Contract
9+
end
10+
711
def self.included(includer)
8-
if Gem::Version.new(Dry::Validation::VERSION) > Gem::Version.new("0.13.3")
9-
require "reform/form/dry/new_api"
10-
validations = Reform::Form::Dry::NewApi::Validations
11-
else
12-
require "reform/form/dry/old_api"
13-
validations = Reform::Form::Dry::OldApi::Validations
12+
includer.send :include, Validations
13+
includer.extend Validations::ClassMethods
14+
end
15+
16+
module Validations
17+
module ClassMethods
18+
def validation_group_class
19+
Group
20+
end
21+
end
22+
23+
def self.included(includer)
24+
includer.extend(ClassMethods)
1425
end
1526

16-
includer.send :include, validations
17-
includer.extend validations::ClassMethods
27+
class Group
28+
include InputHash
29+
30+
def initialize(options = {})
31+
@validator = options.fetch(:schema, Contract)
32+
@schema_inject_params = options.fetch(:with, {})
33+
end
34+
35+
attr_reader :validator, :schema_inject_params, :block
36+
37+
def instance_exec(&block)
38+
@block = block
39+
end
40+
41+
def call(form)
42+
# when passing options[:schema] the class instance is already created so we just need to call
43+
# "call"
44+
return validator.call(input_hash(form)) unless validator.is_a?(Class) && @validator <= ::Dry::Validation::Contract
45+
46+
dynamic_options = { form: form }
47+
inject_options = schema_inject_params.merge(dynamic_options)
48+
49+
validator.build(inject_options, &block).call(input_hash(form))
50+
end
51+
end
1852
end
1953
end

lib/reform/form/dry/new_api.rb

-46
This file was deleted.

lib/reform/form/dry/old_api.rb

-61
This file was deleted.

lib/reform/validation/groups.rb

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def self.call(groups, form)
4343

4444
groups.collect do |(name, group, options)|
4545
next unless evaluate?(options[:if], results, form)
46-
4746
results[name] = group.(form) # run validation for group. store and collect <Result>.
4847
end
4948
end

test/call_new_api.rb

-23
This file was deleted.

test/call_old_api.rb

-23
This file was deleted.

test/call_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "test_helper"
2+
3+
class CallTest < Minitest::Spec
4+
Song = Struct.new(:title)
5+
6+
class SongForm < TestForm
7+
property :title
8+
9+
validation do
10+
params { required(:title).filled }
11+
end
12+
end
13+
14+
let(:form) { SongForm.new(Song.new) }
15+
16+
it { assert form.(title: "True North").success? }
17+
it { refute form.(title: "True North").failure? }
18+
it { refute form.(title: "").success? }
19+
it { assert form.(title: "").failure? }
20+
21+
it { assert_equal form.(title: "True North").errors.messages, {} }
22+
it { assert_equal form.(title: "").errors.messages, title: ["must be filled"] }
23+
end

test/changed_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ class AlbumForm < TestForm
2626

2727
# nothing changed after setup.
2828
it do
29-
form.changed?(:name).must_equal false
30-
form.songs[0].changed?(:title).must_equal false
31-
form.songs[0].composer.changed?(:name).must_equal false
29+
refute form.changed?(:name)
30+
refute form.songs[0].changed?(:title)
31+
refute form.songs[0].composer.changed?(:name)
3232
end
3333

3434
# after validate, things might have changed.
3535
it do
3636
form.validate("name" => "Out Of Bounds", "songs" => [{"composer" => {"name" => "Ingemar Jansson & Mikael Danielsson"}}])
37-
form.changed?(:name).must_equal true
38-
form.songs[0].changed?(:title).must_equal false
39-
form.songs[0].composer.changed?(:name).must_equal true
37+
assert form.changed?(:name)
38+
refute form.songs[0].changed?(:title)
39+
assert form.songs[0].composer.changed?(:name)
4040
end
4141
end

0 commit comments

Comments
 (0)