From 18f9f758e1224ca5dcabae10f7ec3d2f1e67f0c4 Mon Sep 17 00:00:00 2001 From: moznion Date: Fri, 28 Feb 2025 01:11:55 +0900 Subject: [PATCH] Skip the validation against the binary string (#180) Ref #148 Signed-off-by: moznion --- .../schema_validator/string_validator.rb | 13 +++++++++++- .../schema_validator/string_validator_spec.rb | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/openapi_parser/schema_validator/string_validator.rb b/lib/openapi_parser/schema_validator/string_validator.rb index 16c8701..7c3bc8d 100644 --- a/lib/openapi_parser/schema_validator/string_validator.rb +++ b/lib/openapi_parser/schema_validator/string_validator.rb @@ -8,7 +8,18 @@ def initialize(validator, coerce_value, datetime_coerce_class) end def coerce_and_validate(value, schema, **_keyword_args) - return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(String) + unless value.kind_of?(String) + # Skip validation if the format is `binary`, even if the value is not an actual string. + # ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types + if schema.format == 'binary' + # TODO: + # It would be better to check whether the value is an instance of `Rack::Multipart::UploadFile`, + # `ActionDispatch::Http::UploadedFile`, or another similar class. + return [value, nil] + end + + return OpenAPIParser::ValidateError.build_error_result(value, schema) + end value, err = check_enum_include(value, schema) return [nil, err] if err diff --git a/spec/openapi_parser/schema_validator/string_validator_spec.rb b/spec/openapi_parser/schema_validator/string_validator_spec.rb index 36e8c36..5e0bc2d 100644 --- a/spec/openapi_parser/schema_validator/string_validator_spec.rb +++ b/spec/openapi_parser/schema_validator/string_validator_spec.rb @@ -378,4 +378,24 @@ end end end + + describe 'validate binary format' do + subject { OpenAPIParser::SchemaValidator.validate(params, target_schema, options) } + + let(:replace_schema) do + { + binary: { + type: 'string', + format: 'binary', + }, + } + end + + context 'correct' do + context 'arbitrary object except string' do + let(:params) { { 'binary' => ['b', 'i', 'n', 'a', 'r', 'y'] } } + it { expect(subject).to eq({ 'binary' => ['b', 'i', 'n', 'a', 'r', 'y'] }) } + end + end + end end