Skip to content

Commit 88c93de

Browse files
committed
Made error messages more readable
1 parent 2deaf78 commit 88c93de

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/json_validator.rb

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def json(value)
3737
def translate_message(msg)
3838
# remove suffix
3939
msg.gsub!(/ in schema .*$/, '')
40+
# lowercase first letter (eg. 'The' becomes 'the')
41+
msg.gsub!(/^./) { |m| m.downcase }
42+
# prefix with 'is invalid'
43+
msg.gsub!(/^(.*)$/, 'is invalid (\1)')
4044
end
4145

4246
def check_validity!

spec/lib/json_validator_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def initialize(json_data)
5252
{}
5353
).to(
5454
{
55-
json_data: ["The property '#/' did not contain a required property of 'soup'"]
55+
json_data: ["is invalid (the property '#/' did not contain a required property of 'soup')"]
5656
}
5757
)
5858
end
@@ -90,7 +90,7 @@ def initialize(json_data)
9090
{}
9191
).to(
9292
{
93-
json_data: ["The property '#/menu' was not of a minimum string length of 200"]
93+
json_data: ["is invalid (the property '#/menu' was not of a minimum string length of 200)"]
9494
}
9595
)
9696
end
@@ -150,7 +150,7 @@ def initialize(json_data)
150150

151151
it 'translates json-schema messages to slightly more readable ones' do
152152
msg = "The property '#/menu' was not of a minimum string length of 200 in schema 40148e2f-45d6-51b7-972a-179bd9de61d6#"
153-
expect(subject.translate_message(msg)).to eq("The property '#/menu' was not of a minimum string length of 200")
153+
expect(subject.translate_message(msg)).to eq("is invalid (the property '#/menu' was not of a minimum string length of 200)")
154154
end
155155
end
156156

0 commit comments

Comments
 (0)