Skip to content

Commit 00851d0

Browse files
committed
Better error message for dependentRequired
1 parent 82da92b commit 00851d0

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/openapi_first/schema/validation_error.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ module Schema
66
ValidationError = Data.define(:value, :data_pointer, :schema_pointer, :type, :details, :schema) do
77
# This returns an error message for this specific error.
88
# This it copied from json_schemer here to be easier to customize when passing custom data_pointers.
9-
def message
9+
def message # rubocop:disable Metrics/CyclomaticComplexity
1010
location = data_pointer.empty? ? 'root' : "`#{data_pointer}`"
1111

1212
case type
1313
when 'required'
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe OpenapiFirst::Schema::ValidationResult do
4+
def schema(hash)
5+
JSONSchemer.schema(hash)
6+
end
7+
8+
describe 'missing required field' do
9+
specify do
10+
validation = schema({ required: ['id'] }).validate({})
11+
expect(described_class.new(validation).errors.first.message).to eq 'object at root is missing required properties: id'
12+
end
13+
end
14+
15+
describe 'missing dependentRequired field' do
16+
specify do
17+
validation = schema({ 'dependentRequired' => { 'id' => ['type'] } }).validate({ 'id' => '2' })
18+
expect(described_class.new(validation).errors.first.message).to eq 'object at `/id` is missing required properties'
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)