Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add predicates for checking V1, V2, V3, V5 UUIDs. #75

Merged
merged 1 commit into from
Aug 13, 2020

Conversation

jamesbrauman
Copy link
Contributor

@jamesbrauman jamesbrauman commented Aug 13, 2020

I have a need to validate other versions of UUIDs than just V4. Because the uuid_v4? predicate exists, can we also add predicates for other UUID versions?

Should I also add a uuid? predicate that checks for V1-V5 UUIDs?

Reference: https://stackoverflow.com/a/38191104

@dry-bot
Copy link
Contributor

dry-bot commented Aug 13, 2020

Codacy Here is an overview of what got changed by this pull request:

Issues
======
- Added 44
           

Clones added
============
- spec/unit/predicates/uuid_v3_spec.rb  1
- spec/unit/predicates/uuid_v1_spec.rb  1
- spec/unit/predicates/uuid_v5_spec.rb  1
- spec/unit/predicates/uuid_v2_spec.rb  1
         

See the complete overview on Codacy

[['f2d26c57-e07c-1416-a749-57e937930e04']]
end

it_behaves_like 'a passing predicate'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -206,11 +206,31 @@ def case?(pattern, input)
pattern === input
end

def uuid_v1?(input)
uuid_v1_format = /\A[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\z/i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found an issue: Line is too long. [101/100]


context 'when value is a valid V5 UUID' do
let(:arguments_list) do
[['f2d26c57-e07c-5416-a749-57e937930e04']]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it_behaves_like 'a passing predicate'
end

context 'with value is not a valid V1 UUID' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it_behaves_like 'a passing predicate'
end

context 'with value is not a valid V4 UUID' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe '#uuid_v2?' do
let(:predicate_name) { :uuid_v2? }

context 'when value is a valid V1 UUID' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require 'dry/logic/predicates'

RSpec.describe Dry::Logic::Predicates do
describe '#uuid_v3?' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe '#uuid_v1?' do
let(:predicate_name) { :uuid_v1? }

context 'when value is a valid V1 UUID' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'dry/logic/predicates'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[
["not-a-uuid-at-all\nf2d26c57-e07c-2416-a749-57e937930e04"], # V2 with invalid prefix
["f2d26c57-e07c-2416-a749-57e937930e04\nnot-a-uuid-at-all"], # V2 with invalid suffix
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[['f2d26c57-e07c-2416-a749-57e937930e04']]
end

it_behaves_like 'a passing predicate'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'dry/logic/predicates'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def uuid_v4?(input)
uuid_v4_format = /\A[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\z/i
format?(uuid_v4_format, input)
end

def uuid_v5?(input)
uuid_v5_format = /\A[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\z/i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy found an issue: Line is too long. [101/100]

require 'dry/logic/predicates'

RSpec.describe Dry::Logic::Predicates do
describe '#uuid_v5?' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe '#uuid_v3?' do
let(:predicate_name) { :uuid_v3? }

context 'when value is a valid V3 UUID' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require 'dry/logic/predicates'

RSpec.describe Dry::Logic::Predicates do
describe '#uuid_v2?' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

]
end

it_behaves_like 'a failing predicate'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

["f2d26c57-e07c-5416-a749-57e937930e04\nnot-a-uuid-at-all"], # V4 with invalid suffix
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 5)
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
['not-a-uuid-at-all']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


context 'when value is a valid V3 UUID' do
let(:arguments_list) do
[['f2d26c57-e07c-3416-a749-57e937930e04']]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

["not-a-uuid-at-all\nf2d26c57-e07c-3416-a749-57e937930e04"], # V3 with invalid prefix
["f2d26c57-e07c-3416-a749-57e937930e04\nnot-a-uuid-at-all"], # V3 with invalid suffix
['f2d26c57-e07c-4416-a749-57e937930e04'], # wrong version number (4, not 3)
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

["f2d26c57-e07c-2416-a749-57e937930e04\nnot-a-uuid-at-all"], # V2 with invalid suffix
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 2)
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
['not-a-uuid-at-all']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

["f2d26c57-e07c-3416-a749-57e937930e04\nnot-a-uuid-at-all"], # V3 with invalid suffix
['f2d26c57-e07c-4416-a749-57e937930e04'], # wrong version number (4, not 3)
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
['not-a-uuid-at-all']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

["not-a-uuid-at-all\nf2d26c57-e07c-2416-a749-57e937930e04"], # V2 with invalid prefix
["f2d26c57-e07c-2416-a749-57e937930e04\nnot-a-uuid-at-all"], # V2 with invalid suffix
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 2)
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'dry/logic/predicates'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[
["not-a-uuid-at-all\nf2d26c57-e07c-5416-a749-57e937930e04"], # V4 with invalid prefix
["f2d26c57-e07c-5416-a749-57e937930e04\nnot-a-uuid-at-all"], # V4 with invalid suffix
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solnic solnic merged commit d9938d9 into dry-rb:master Aug 13, 2020
@solnic solnic added this to the 1.0.7 milestone Aug 13, 2020
@solnic
Copy link
Member

solnic commented Aug 13, 2020

@jamesbrauman thank you James, you can now enjoy your new predicates in the freshly released dry-logic 1.0.7 😄

@jamesbrauman
Copy link
Contributor Author

@solnic Thank-you for responsiveness, and the dry-rb suite of gems!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants