Skip to content

Commit b404a0a

Browse files
authored
Fix UUID validation (#149)
* Fix UUID validation regex * Document case insensitivity https://www.rfc-editor.org/rfc/rfc4122#section-3 * Document more invalid edge cases
1 parent c239f7b commit b404a0a

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/openapi_parser/schema_validator/string_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def validate_email_format(value, schema)
6363
def validate_uuid_format(value, schema)
6464
return [value, nil] unless schema.format == 'uuid'
6565

66-
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}/)
66+
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}$/)
6767

6868
return [nil, OpenAPIParser::InvalidUUIDFormat.new(value, schema.object_reference)]
6969
end

spec/openapi_parser/schema_validator/string_validator_spec.rb

+24-9
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,34 @@
176176
end
177177

178178
context 'correct' do
179-
let(:params) { { 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' } }
180-
it { expect(subject).to eq({ 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' }) }
179+
context 'lowercase' do
180+
let(:params) { { 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' } }
181+
it { expect(subject).to eq({ 'uuid_str' => 'fd33fb1e-b1f6-401e-994d-8a2545e1aef7' }) }
182+
end
183+
184+
context 'uppercase' do
185+
let(:params) { { 'uuid_str' => 'FD33FB1E-B1F6-401E-994D-8A2545E1AEF7' } }
186+
it { expect(subject).to eq({ 'uuid_str' => 'FD33FB1E-B1F6-401E-994D-8A2545E1AEF7' }) }
187+
end
181188
end
182189

183190
context 'invalid' do
184-
context 'error pattern' do
185-
let(:value) { 'not_uuid' }
186-
let(:params) { { 'uuid_str' => value } }
191+
%w[
192+
not_uuid
193+
zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz
194+
204730d-fd3f5-364b-9aeb-d1372aba0d35
195+
204730df_d3f5_364b_9aeb_d1372aba0d35
196+
204730df-d3f5-364b-9aeb-d1372aba0d35-deadbeef
197+
deadbeef-204730df-d3f5-364b-9aeb-d1372aba0d35
198+
].each do |invalid_uuid|
199+
context 'error pattern' do
200+
let(:params) { { 'uuid_str' => invalid_uuid } }
187201

188-
it do
189-
expect { subject }.to raise_error do |e|
190-
expect(e).to be_kind_of(OpenAPIParser::InvalidUUIDFormat)
191-
expect(e.message).to end_with("Value: \"not_uuid\" is not conformant with UUID format")
202+
it do
203+
expect { subject }.to raise_error do |e|
204+
expect(e).to be_kind_of(OpenAPIParser::InvalidUUIDFormat)
205+
expect(e.message).to end_with("Value: \"#{invalid_uuid}\" is not conformant with UUID format")
206+
end
192207
end
193208
end
194209
end

0 commit comments

Comments
 (0)