Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/openapi_first/schema/validation_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ module Schema
ValidationError = Data.define(:value, :data_pointer, :schema_pointer, :type, :details, :schema) do
# This returns an error message for this specific error.
# This it copied from json_schemer here to be easier to customize when passing custom data_pointers.
def message
def message # rubocop:disable Metrics/CyclomaticComplexity
location = data_pointer.empty? ? 'root' : "`#{data_pointer}`"

case type
when 'required'
keys = details.fetch('missing_keys', []).join(', ')
"object at #{location} is missing required properties: #{keys}"
when 'dependentRequired'
keys = details.fetch('missing_keys').join(', ')
"object at #{location} is missing required properties: #{keys}"
keys = details&.fetch('missing_keys')
"object at #{location} is missing required properties#{keys && " #{keys.join(', ')}"}"
when 'string', 'boolean', 'number'
"value at #{location} is not a #{type}"
when 'array', 'object', 'integer'
Expand Down
21 changes: 21 additions & 0 deletions spec/schema/validation_result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

RSpec.describe OpenapiFirst::Schema::ValidationResult do
def schema(hash)
JSONSchemer.schema(hash)
end

describe 'missing required field' do
specify do
validation = schema({ required: ['id'] }).validate({})
expect(described_class.new(validation).errors.first.message).to eq 'object at root is missing required properties: id'
end
end

describe 'missing dependentRequired field' do
specify do
validation = schema({ 'dependentRequired' => { 'id' => ['type'] } }).validate({ 'id' => '2' })
expect(described_class.new(validation).errors.first.message).to eq 'object at `/id` is missing required properties'
end
end
end