Skip to content

Commit a9ed15d

Browse files
committed
WIP: Better error message for dependentRequired
1 parent 82da92b commit a9ed15d

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/openapi_first/schema/validation_error.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def message
1414
keys = details.fetch('missing_keys', []).join(', ')
1515
"object at #{location} is missing required properties: #{keys}"
1616
when 'dependentRequired'
17-
keys = details.fetch('missing_keys').join(', ')
18-
"object at #{location} is missing required properties: #{keys}"
17+
keys = details&.fetch('missing_keys')
18+
"object at #{location} is missing required properties#{keys && " #{keys.join(', ')}"}"
1919
when 'string', 'boolean', 'number'
2020
"value at #{location} is not a #{type}"
2121
when 'array', 'object', 'integer'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
RSpec.describe OpenapiFirst::Schema::ValidationResult do
2+
def schema(hash)
3+
JSONSchemer.schema(hash)
4+
end
5+
6+
describe 'missing required field' do
7+
specify do
8+
validation = schema({ required: ['id'] }).validate({})
9+
expect(described_class.new(validation).errors.first.message).to eq 'object at root is missing required properties: id'
10+
end
11+
end
12+
13+
describe 'missing dependentRequired field' do
14+
specify do
15+
validation = schema({ 'dependentRequired' => { 'id' => ['type'] } }).validate({ 'id' => '2' })
16+
expect(described_class.new(validation).errors.first.message).to eq 'object at `/id` is missing required properties'
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)