Skip to content

Commit 2a8a68b

Browse files
authored
Fix crash when defining multiple versions (#151)
* Fix crash when defining multiple validation versions * Add versions test * Run in correct scope * Fix missing method in test
1 parent dd7a1de commit 2a8a68b

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.3.3
4+
5+
- Fix crash when defining multiple versions of a validator
6+
37
## 2.3.2
48

59
- Fix that causes bailout in nested rule to stop validation altogether

lib/media_types/validations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def version(version, &block)
9797
def versions(versions, &block)
9898
versions.each do |v|
9999
Validations.new(media_type.version(v), registry) do
100-
block(v)
100+
instance_exec(v, &block)
101101
end
102102
end
103103
end

lib/media_types/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module MediaTypes
4-
VERSION = '2.3.2'
4+
VERSION = '2.3.3'
55
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../test_helper'
4+
5+
module MediaTypes
6+
module Dsl
7+
class VersionsTestTest < Minitest::Test
8+
9+
class AttributeType
10+
include MediaTypes::Dsl
11+
12+
def self.organisation
13+
'acme'
14+
end
15+
16+
use_name 'AttributeType'
17+
18+
validations do
19+
versions [1,2] do |v|
20+
attribute :foo, Numeric
21+
attribute :bar, Numeric if v == 2
22+
end
23+
end
24+
end
25+
26+
def test_versions
27+
assert AttributeType.version(1).valid?({ foo: 1 }), 'Version 1 should validate'
28+
29+
assert AttributeType.version(2).valid?({ foo: 1, bar: 42 }), 'Version 2 should validate'
30+
refute AttributeType.version(1).valid?({ foo: 1, bar: 42 }), 'bar should only exist in version 2'
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)