Skip to content

Commit dd7a1de

Browse files
Add loose tests for invariants (#149)
* Add loose tests for invariants * Fix that causes bailout in nested rule to stop validation altogether
1 parent fd5a834 commit dd7a1de

File tree

6 files changed

+35
-16
lines changed

6 files changed

+35
-16
lines changed

CHANGELOG.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# Changelog
22

3+
## 2.3.2
4+
5+
- Fix that causes bailout in nested rule to stop validation altogether
6+
37
## 2.3.1
48

5-
- Fix build issue that caused debian package to be empty
9+
- Fix build issue that caused debian package to be empty
610

711
## 2.3.0
812

9-
- Add ability to define multiple versions using one block.
10-
- Add `index` attribute type that automatically generates a link list compatible with media_types-serialization.
11-
- Add support for `collection` to automatically look up a previously defined schema when passing a view.
12-
- Add ability to mark certain attributes as optional when validating with `loose: true` and required otherwise.
13+
- Add ability to define multiple versions using one block.
14+
- Add `index` attribute type that automatically generates a link list compatible with media_types-serialization.
15+
- Add support for `collection` to automatically look up a previously defined schema when passing a view.
16+
- Add ability to mark certain attributes as optional when validating with `loose: true` and required otherwise.
1317

1418
## 2.2.0
1519

16-
- Change dependencies and build output to be debian compatible
20+
- Change dependencies and build output to be debian compatible
1721

1822
## 2.1.1
1923

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
media_types (2.1.1)
4+
media_types (2.3.2)
55

66
GEM
77
remote: https://rubygems.org/

lib/media_types/scheme.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def collection(key, scheme = nil, view: nil, allow_empty: false, expected_type:
383383
#
384384
def index(optional: false)
385385
collection(:_links, optional: optional) do
386-
link :_self
386+
link :_self
387387
end
388388
end
389389

lib/media_types/scheme/rules_exhausted_guard.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ def iterate(mark)
4848

4949
mark.call(key)
5050

51-
rules.get(key).validate!(
52-
value,
53-
options.trace(key),
54-
context: context
55-
)
51+
catch(:end) do
52+
rules.get(key).validate!(
53+
value,
54+
options.trace(key),
55+
context: context
56+
)
57+
end
5658
end
5759
end
5860

lib/media_types/version.rb

+1-1
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.1'
4+
VERSION = '2.3.2'
55
end

test/media_types/loose_validation_test.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,24 @@ def self.organisation
1616

1717
# default attribute (=hash object)
1818
validations do
19+
1920
attribute :foo do
2021
attribute :bar, Numeric, optional: :loose
2122
end
22-
assert_pass '{"foo":{}}', loose: true
23-
assert_fail '{"foo":{}}', loose: false
23+
24+
attribute :baz, String
25+
26+
# Loose keys don't need to be present in loose mode
27+
assert_pass '{"foo":{}, "baz": "hello world"}', loose: true
28+
29+
# Loose keys must be present in normal mode
30+
assert_fail '{"foo":{}, "baz": "hello world"}', loose: false
31+
32+
# All required keys must be present
33+
assert_fail '{"foo":{}}', loose: true
34+
35+
# Extra keys not allowed (unless not-strict)
36+
assert_fail '{"foo":{}, "nope": "unknown"}', loose: true
2437
end
2538
end
2639

0 commit comments

Comments
 (0)