File tree 2 files changed +36
-2
lines changed
lib/json_skooma/keywords/validation
2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,20 @@ class Required < Base
8
8
self . instance_types = "object"
9
9
10
10
def evaluate ( instance , result )
11
- return if json . value . all? { |val | instance . key? ( val ) }
11
+ missing = required_keys . reject { |key | instance . key? ( key ) }
12
+ return if missing . none?
12
13
13
- result . failure ( "The object is missing required properties #{ json . value . join ( ", " ) } " )
14
+ result . failure ( missing_keys_message ( missing ) )
15
+ end
16
+
17
+ private
18
+
19
+ def required_keys
20
+ json . value
21
+ end
22
+
23
+ def missing_keys_message ( missing )
24
+ "The object requires the following keys: #{ required_keys . join ( ", " ) } . Missing keys: #{ missing . join ( ", " ) } "
14
25
end
15
26
end
16
27
end
Original file line number Diff line number Diff line change 37
37
38
38
it { is_expected . to be_valid }
39
39
40
+ context "with required keys" do
41
+ before do
42
+ schema_data [ "required" ] = %w[ foo bar ]
43
+ schema_data [ "properties" ] [ "bar" ] = { "type" => "integer" }
44
+ end
45
+
46
+ it { is_expected . not_to be_valid }
47
+
48
+ it "informs us which key is missing" do
49
+ errors = subject . output ( :detailed ) [ "errors" ] . map { |e | e [ "error" ] }
50
+
51
+ expect ( errors ) . to contain_exactly (
52
+ "The object requires the following keys: foo, bar. Missing keys: bar"
53
+ )
54
+ end
55
+
56
+ context "with valid instance" do
57
+ let ( :instance ) { { foo : "baz" , bar : 123 } }
58
+
59
+ it { is_expected . to be_valid }
60
+ end
61
+ end
62
+
40
63
context "with ref option" do
41
64
let ( :ref ) { "#/$defs/FooBaz" }
42
65
You can’t perform that action at this time.
0 commit comments