Skip to content

Commit

Permalink
Fix UUID validation (#149)
Browse files Browse the repository at this point in the history
* Fix UUID validation regex

* Document case insensitivity

https://www.rfc-editor.org/rfc/rfc4122#section-3

* Document more invalid edge cases
  • Loading branch information
plore authored Oct 21, 2024
1 parent c239f7b commit b404a0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/openapi_parser/schema_validator/string_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def validate_email_format(value, schema)
def validate_uuid_format(value, schema)
return [value, nil] unless schema.format == 'uuid'

return [value, nil] if value.match(/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/)
return [value, nil] if value.match(/^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/)

return [nil, OpenAPIParser::InvalidUUIDFormat.new(value, schema.object_reference)]
end
Expand Down
33 changes: 24 additions & 9 deletions spec/openapi_parser/schema_validator/string_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,34 @@
end

context 'correct' do
let(:params) { { 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' } }
it { expect(subject).to eq({ 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' }) }
context 'lowercase' do
let(:params) { { 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' } }
it { expect(subject).to eq({ 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' }) }
end

context 'uppercase' do
let(:params) { { 'uuid_str' => 'FD33FB1E-B1F6-401E-994D-8A2545E1AEF7' } }
it { expect(subject).to eq({ 'uuid_str' => 'FD33FB1E-B1F6-401E-994D-8A2545E1AEF7' }) }
end
end

context 'invalid' do
context 'error pattern' do
let(:value) { 'not_uuid' }
let(:params) { { 'uuid_str' => value } }
%w[
not_uuid
zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz
204730d-fd3f5-364b-9aeb-d1372aba0d35
204730df_d3f5_364b_9aeb_d1372aba0d35
204730df-d3f5-364b-9aeb-d1372aba0d35-deadbeef
deadbeef-204730df-d3f5-364b-9aeb-d1372aba0d35
].each do |invalid_uuid|
context 'error pattern' do
let(:params) { { 'uuid_str' => invalid_uuid } }

it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::InvalidUUIDFormat)
expect(e.message).to end_with("Value: \"not_uuid\" is not conformant with UUID format")
it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::InvalidUUIDFormat)
expect(e.message).to end_with("Value: \"#{invalid_uuid}\" is not conformant with UUID format")
end
end
end
end
Expand Down

0 comments on commit b404a0a

Please sign in to comment.